Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
parsagholipour committed Jun 5, 2021
1 parent 5aa9eda commit 9d7d191
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?php

namespace Shetabit\ModuleGenerator\Classes;
namespace Shetabit\ModuleGenerator\Classes\Generators;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpNamespace;
use Shetabit\ModuleGenerator\Classes\ModelName;
use Shetabit\ModuleGenerator\Helpers\Helper;

class ControllerGenerator
{

public string $message = '';
protected $models;
protected $modelName;
protected ModelName $modelName;
protected $module;
protected $pathOfController;
protected $CRUD;
protected $nameController;
/**
* @var mixed|string
*/
Expand All @@ -40,58 +40,65 @@ public function __construct($module, $models)

public function generate(): string
{
$message = '';
foreach ($this->models as $model => $this->attributes) {
$this->modelName = $model;
$this->modelName = new ModelName($model);
if (!key_exists('CRUD', $this->attributes)) {
return '';
}
$this->CRUD = $this->attributes['CRUD'];

return $this->controllerGenerator($this->module);
$message .= $this->controllerGenerator($this->module);
}

return '';
return $message;
}

public function controllerGenerator($module): string
{
$message = '';
foreach ($this->CRUD as $name => $option) {
$this->nameController = $name;
$this->pathOfController = module_path($module) . "/Http/Controllers/" . $this->nameController . "/";
$template = $this->generateControllerTemplates($option[0]);
$this->pathOfController = module_path($module) . "/Http/Controllers/" . $name . '/';
$template = $this->generateControllerTemplates($option[0], $name);
$template = '<?php' . PHP_EOL . $template;
$this->createDirectory();
$this->touchAndPutContent($template);
$this->message .= "|-- " . $this->nameController . "Controller successfully generated" . PHP_EOL;
$message .= "|-- " . $this->modelName . "Controller ({$name}) successfully generated" . PHP_EOL;
}

return $this->message;
return $message;
}

public function generateControllerTemplates($option): PhpNamespace
public function generateControllerTemplates($option, $name): PhpNamespace
{
$namespace = new PhpNamespace('Modules\\' . $this->module . '\Http\Controllers\\' . $this->nameController);
$namespace = new PhpNamespace('Modules\\' . $this->module . '\Http\Controllers\\' . $name);
$namespace->addUse(Controller::class)
->addUse(Request::class)
->addUse('Modules\\' . $this->module . '\Entities\\' . $this->modelName);
$class = $namespace->addClass($this->nameController . "Controller");
if ($this->hasCreate($option)) {
$namespace->addUse($this->getStoreRequestNamespace($name));
}
if ($this->hasUpdate($option)) {
$namespace->addUse($this->getUpdateRequestNamespace($name));
}
$class = $namespace->addClass($this->modelName . "Controller");
$class->setExtends(Controller::class);

$this->setMethodToController($class, $option, $namespace);
$this->setMethodToController($class, $option, $namespace, $name);

return $namespace;
}

public function setMethodToController($class, $option, $namespace)
public function setMethodToController($class, $option, $namespace, $userName)
{
if (str_contains($option , 'R')) {
$this->indexAndShowMethodGenerator($class);
}
if (str_contains($option, 'C')) {
$this->createAndStoreMethodGenerator($class);
if ($this->hasCreate($option)) {
$this->storeMethodGenerator($class, $userName);
}
if (str_contains($option, 'U')) {
$this->editAndUpdateMethodGenerator($class , $namespace);
if ($this->hasUpdate($option)) {
$this->updateMethodGenerator($class , $namespace, $userName);
}
if (str_contains($option, 'D')) {
$this->destroyMethodGenerator($class);
Expand All @@ -101,84 +108,71 @@ public function setMethodToController($class, $option, $namespace)
public function indexAndShowMethodGenerator(classType $class)
{
$method = $class->addMethod('index');
if (key_exists('Relations', $this->attributes)) {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();')
if (key_exists('Relations', $this->attributes) && !empty($this->attributes['Relations'])) {
$method->addBody('$' . $this->modelName->getPluralForController() . ' = ' . $this->modelName . '::withCommonRelations()->get();')
->addBody($this->getReturnStatement(true));
} else {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();')
$method->addBody('$' . $this->modelName->getPluralForController() . ' = ' . $this->modelName . '::query()->get();')
->addBody($this->getReturnStatement(true));
}
$class->addMethod('show')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);')
->addBody($this->getReturnStatement())
->addParameter('id')->setType('Int');
$method = $class->addMethod('show');
if (key_exists('Relations', $this->attributes) && !empty($this->attributes['Relations'])) {
$method->addBody('$' . $this->modelName->getSingularForController() . ' = ' . $this->modelName . '::withCommonRelations()->findOrFail($id);');
} else {
$method->addBody('$' . $this->modelName->getSingularForController() . ' = ' . $this->modelName . '::findOrFail($id);');
}
$method->addBody($this->getReturnStatement())
->addParameter('id');
}


public function createAndStoreMethodGenerator(ClassType $class): void
public function storeMethodGenerator(ClassType $class, $userName): void
{
$class->addMethod('create');
if (!key_exists('Relations', $this->attributes)) {
$method = $class->addMethod('store')
->addComment('Store a newly created resource in storage')
->addComment('@param Request $request')
->addBody($this->getReturnStatement())
->addParameter('request')->setType(Request::class);
return;
}
$method = $class->addMethod('store')
->addBody('$' . strtolower($this->modelName) . ' = new ' . ucfirst($this->modelName) . '();')
->addBody('$' . strtolower($this->modelName) . '->fill($request->all());');
->addBody('$' . $this->modelName->getSingularForController() . ' = new ' . $this->modelName . '();')
->addBody('$' . $this->modelName->getSingularForController() . '->fill($request->all());');
$this->associateInStore($method);
$method->addBody('$' . strtolower($this->modelName) . '->save();')
$method->addBody('$' . $this->modelName->getSingularForController() . '->save();')
->addComment('Store a newly created resource in storage')
->addComment('@param Request $request')
->addBody($this->getReturnStatement())
->addParameter('request')->setType(Request::class);
->addParameter('request')
->setType($this->getStoreRequestNamespace($userName));
}

public function associateInStore($method): void
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
if ((!is_array($relations) && Str::camel($relations) == 'morphTo') || $this->doesRelationHaveAssociate($relations)){
return;
}
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
$method->addBody('$' . strtolower($this->modelName) . '->' . strtolower($this->relationName) . '()->associate($request->' . strtolower($this->baseRelationName) . '_id);');
$method->addBody('$' . $this->modelName->getSingularForController() . '->' . Str::camel($this->relationName) . '()->associate($request->' . strtolower($this->baseRelationName) . '_id);');
}
}
}
}


public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
public function updateMethodGenerator(ClassType $class , $namespace, $userName)
{
$method = $class->addMethod('edit');
if (key_exists('Relations', $this->attributes)) {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);')
->addBody($this->getReturnStatement());
} else {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);')
->addBody($this->getReturnStatement());
};
$method->addParameter('id')->setType('Int');

$method = $class->addMethod('update')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
->addBody('$' . $this->modelName->getSingularForController() . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');

$this->UpdateMethodFindIntoRelation($method , $namespace);
$this->associateInUpdate($method);
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
->addBody('$' . strtolower($this->modelName) . '->save();')
$method->addBody('$' . $this->modelName->getSingularForController() . '->fill($request->all());')
->addBody('$' . $this->modelName->getSingularForController() . '->save();')
->addBody($this->getReturnStatement())
->addComment('Update the specified resource in storage.')
->addComment('@param Request $request')
->addComment('@param int $id');
$method->addParameter('request')->setType(Request::class);
$method->addParameter('id')->setType('Int');
->addComment('@param $id');
$method->addParameter('request')
->setType($this->getUpdateRequestNamespace($userName));
$method->addParameter('id');
}


Expand Down Expand Up @@ -208,7 +202,7 @@ public function associateInUpdate($method): void
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
$method->addBody('$' . strtolower($this->modelName) . '->' . strtolower($this->relationName) . '()->associate($' . strtolower($this->baseRelationName) . ');');
$method->addBody('$' . $this->modelName->getSingularForController() . '->' . strtolower($this->relationName) . '()->associate($' . strtolower($this->baseRelationName) . ');');
}
}
}
Expand All @@ -217,9 +211,9 @@ public function associateInUpdate($method): void
public function destroyMethodGenerator(ClassType $class)
{
$class->addMethod('destroy')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);')
->addBody('$' . $this->modelName->getSingularForController() . ' = ' . ucfirst($this->modelName) . '::findOrFail($id)->destroy();')
->addBody($this->getReturnStatement())
->addParameter('id')->setType('Int');
->addParameter('id');
}

public function createDirectory()
Expand All @@ -231,26 +225,46 @@ public function createDirectory()

public function touchAndPutContent($template): bool
{
touch($this->pathOfController . $this->nameController . 'Controller.php');
file_put_contents($this->pathOfController . $this->nameController . 'Controller.php', $template);
touch($this->pathOfController . $this->modelName . 'Controller.php');
file_put_contents($this->pathOfController . $this->modelName . 'Controller.php', $template);

return true;
}

public function getReturnStatement($plural = false): string
{
if (str_contains($this->return, ':data')) {
$modelNameInReturn = $plural ? Str::plural(Str::camel($this->modelName)) : Str::camel($this->modelName);
$modelNameInReturn = $plural ? $this->modelName->getPluralForController() : $this->modelName->getSingularForController();

return PHP_EOL . str_replace(':data', '$' . $modelNameInReturn, $this->return);
}

return $this->return;
}

// It comes before return statement to initialize $data
public function getDataStatement(): string
public function getStoreRequestNamespace($userName)
{
return 'Modules\\' . $this->module . '\Http\Requests\\' . $userName . '\\' . "{$this->modelName}StoreRequest";
}

public function getUpdateRequestNamespace($userName)
{
return 'Modules\\' . $this->module . '\Http\Requests\\' . $userName . '\\' . "{$this->modelName}UpdateRequest";
}

public function hasCreate($option)
{
return str_contains($option, 'C');
}

public function hasUpdate($option)
{
return str_contains($option, 'U');
}

public function doesRelationHaveAssociate($relation)
{
return '$data = $' . $this->modelName . ';';
return !in_array(Str::camel($relation), ['hasOne', 'hasMany', 'morphTo']);
}

public function __toString(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Shetabit\ModuleGenerator\Classes;
namespace Shetabit\ModuleGenerator\Classes\Generators;

use Carbon\Carbon;
use Illuminate\Database\Migrations\Migration;
Expand Down Expand Up @@ -36,11 +36,11 @@ public function generate(): string
}
$namespace = new PhpNamespace('');

foreach ($this->models as $key => $model) {
foreach ($this->models as $model) {
$continue = false;
foreach ($model as $key2 => $relation) {
$this->modelName = $key2;
if (!key_exists('Relations', $relation)) {
foreach ($model as $key => $relation) {
$this->modelName = $key;
if (!key_exists('Relations', $relation) && empty($relation['Relations'])) {
$continue = true;
}
}
Expand All @@ -52,9 +52,11 @@ public function generate(): string
$class->setExtends(Migration::class);
$this->foreignKeyGenerator($model, $class);
}
$template = '<?php' . PHP_EOL . $namespace;
$this->touchAndPutContent($template);
$this->message .= "|-- Foreign keys successfully generated" . PHP_EOL;
if (count($namespace->getClasses()) !== 0) {
$template = '<?php' . PHP_EOL . $namespace;
$this->touchAndPutContent($template);
$this->message .= "|-- Foreign keys successfully generated" . PHP_EOL;
}

return $this->message;
}
Expand Down Expand Up @@ -101,7 +103,7 @@ public function addMethodsInMigration($fields , $methodUp)
public function touchAndPutContent($template): bool
{
foreach (Finder::create()->files()
->name("*create_foreign_keys_table.php")
->name("*add_foreign_keys.php")
->in($this->migrationPath) as $file) {
unlink($file->getPathname());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Shetabit\ModuleGenerator\Classes;
namespace Shetabit\ModuleGenerator\Classes\Generators;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
Expand Down Expand Up @@ -43,15 +43,13 @@ public function migrationsGenerator($model): string
{
foreach ($model as $key => $fields) {
$relation = null;
if (key_exists('Relations', $fields)) {
$relation = $fields['Relations'];
}
$relations = $fields['Relations'] ?? [];
$this->migrationName = $key;
$namespace = new PhpNamespace('');
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
$class = $namespace->addClass('Create' . Str::plural($this->migrationName) . 'Table');
$class->setExtends(Migration::class);
$this->addMethodsInMigration($class, $fields['Fields'] , $relation);
$this->addMethodsInMigration($class, $fields['Fields'] , $relations);
$template = '<?php' . PHP_EOL . $namespace;
$this->touchAndPutContent($template);
$this->message .= "|-- Migration " . $this->migrationName . " successfully generated" . PHP_EOL;
Expand Down Expand Up @@ -84,12 +82,17 @@ public function addMethodsInMigration(ClassType $class , $fields , $relation)

public function addFieldsInMethod($fields)
{
$fieldsString = '';
foreach($fields as $key => $infoField){
$field = " \$table->".$infoField['type']."('".$key."')";
if (!key_exists('options', $infoField)) return $field.";";
return $this->addOptionsInFields($field ,$infoField['options']);
if (!key_exists('options', $infoField)) {
$field .= ";";
} else {
$field = $this->addOptionsInFields($field ,$infoField['options']);
}
$fieldsString .= $field . PHP_EOL;
}
return $fields;
return $fieldsString;
}

public function addOptionsInFields($field , $options)
Expand Down
Loading

0 comments on commit 9d7d191

Please sign in to comment.