Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
komeylkalbali committed Mar 13, 2021
1 parent e62700b commit 35da9fd
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 70 deletions.
71 changes: 71 additions & 0 deletions src/Classes/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ class ControllerGenerator
*/
protected $baseRelationName;
protected $attributes;
<<<<<<< HEAD
=======
protected $config;
>>>>>>> 73a07f9... first commit

public function __construct($module, $models)
{
$this->models = $models['Models'];
$this->module = $module;
$this->config = \config()->get('moduleConfig');

}

public function generate(): string
Expand Down Expand Up @@ -74,6 +80,7 @@ public function generateControllerTemplates($option): PhpNamespace

public function setMethodToController($class, $option, $namespace)
{
<<<<<<< HEAD
if (strpos($option , 'R') == true) {
$this->indexAndShowMethodGenerator($class);
}
Expand All @@ -84,6 +91,18 @@ public function setMethodToController($class, $option, $namespace)
$this->editAndUpdateMethodGenerator($class, $namespace);
}
if (strpos($option, 'D') == true) {
=======
if (str_contains($option , 'R')) {
$this->indexAndShowMethodGenerator($class);
}
if (str_contains($option, 'C')) {
$this->createAndStoreMethodGenerator($class);
}
if (str_contains($option, 'U')) {
$this->editAndUpdateMethodGenerator($class , $namespace);
}
if (str_contains($option, 'D')) {
>>>>>>> 73a07f9... first commit
$this->destroyMethodGenerator($class);
}
}
Expand All @@ -93,6 +112,7 @@ 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();' . PHP_EOL)
<<<<<<< HEAD
->addBody('return response()->json($' . strtolower($this->modelName) . 's);');
} else {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
Expand All @@ -101,6 +121,16 @@ public function indexAndShowMethodGenerator(classType $class)
$class->addMethod('show')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
=======
->addBody($this->config['return']);
} else {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
->addBody($this->config['return']);
}
$class->addMethod('show')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody($this->config['return'])
>>>>>>> 73a07f9... first commit
->addParameter('id')->setType('Int');
}

Expand Down Expand Up @@ -129,6 +159,12 @@ public function associateInStore($method)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
<<<<<<< HEAD
=======
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
return '';
}
>>>>>>> 73a07f9... first commit
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
Expand All @@ -138,21 +174,36 @@ public function associateInStore($method)
}
}

<<<<<<< HEAD
public function editAndUpdateMethodGenerator(ClassType $class, $namespace)
=======
public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
>>>>>>> 73a07f9... first commit
{
$method = $class->addMethod('edit');
if (key_exists('Relations', $this->attributes)) {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);' . PHP_EOL)
<<<<<<< HEAD
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
} else {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
=======
->addBody($this->config['return']);
} else {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody($this->config['return']);
>>>>>>> 73a07f9... first commit
};
$method->addParameter('id')->setType('Int');

$method = $class->addMethod('update')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
<<<<<<< HEAD
$this->UpdateMethodFindIntoRelation($method, $namespace);
=======
$this->UpdateMethodFindIntoRelation($method , $namespace);
>>>>>>> 73a07f9... first commit
$this->associateInUpdate($method);
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
->addBody('$' . strtolower($this->modelName) . '->save();'.PHP_EOL)
Expand All @@ -164,10 +215,20 @@ public function editAndUpdateMethodGenerator(ClassType $class, $namespace)
$method->addParameter('id')->setType('Int');
}

<<<<<<< HEAD
public function UpdateMethodFindIntoRelation($method, $namespace)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
=======
public function UpdateMethodFindIntoRelation($method ,$namespace)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
return '';
}
>>>>>>> 73a07f9... first commit
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$method->addBody('$' . strtolower($this->baseRelationName) . ' = ' . ucfirst($this->baseRelationName) . '::query()->findOrFail($request->' . strtolower($this->baseRelationName) . '_id);');
Expand All @@ -181,6 +242,12 @@ public function associateInUpdate($method)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
<<<<<<< HEAD
=======
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
return '';
}
>>>>>>> 73a07f9... first commit
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
Expand All @@ -194,7 +261,11 @@ public function destroyMethodGenerator(ClassType $class)
{
$class->addMethod('destroy')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);' . PHP_EOL)
<<<<<<< HEAD
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
=======
->addBody($this->config['return'])
>>>>>>> 73a07f9... first commit
->addParameter('id')->setType('Int');
}

Expand Down
85 changes: 44 additions & 41 deletions src/Classes/ForeignKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace Shetabit\ModuleGenerator\Classes;

use Carbon\Carbon;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpNamespace;
use Shetabit\ModuleGenerator\Helpers\Helper;
use Shetabit\ModuleGenerator\Traits\CreatePivotTable;
use Symfony\Component\Finder\Finder;

class ForeignKeyGenerator
{

use CreatePivotTable;

protected string $message = '';
protected $models;
protected $module;
protected $fields;
protected $nameOfMigration;
protected $pathOfMigration;
protected $nameOfModel;

Expand All @@ -31,16 +33,18 @@ public function generate(): string
{
if (!key_exists('Models', $this->models)) return '';
$namespace = new PhpNamespace('');
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
$class = $namespace->addClass('CreateForeignKeyTable');
$class->setExtends(Migration::class);


foreach ($this->models as $key => $model) {
foreach ($model as $fields) {
if (!key_exists('Relations', $fields)) return '';
}
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
$class = $namespace->addClass('CreateForeignKeysTable');
$class->setExtends(Migration::class);
$this->pathOfMigration = module_path($this->module) . "/Database/Migrations/";
$this->nameOfMigration .= $this->foreignKeyGenerator($model , $class);
$this->foreignKeyGenerator($model , $class);
}
echo $this->nameOfMigration;
die();
$template = '<?php' . PHP_EOL . $namespace;
$this->touchAndPutContent($template);
$this->message .= "|-- ForeignKey successfully generate" . PHP_EOL;
Expand All @@ -50,62 +54,59 @@ public function generate(): string

public function foreignKeyGenerator($model , $class)
{

$methodUp = $class->addMethod('up');
foreach ($model as $key => $fields) {
$this->nameOfModel = $key;
if (!key_exists('Relations', $fields)) return '';
return $this->generateModelTemplates($class , $fields['Relations']);
$this->generateModelTemplates($methodUp , $fields['Relations']);

}

}

public function generateModelTemplates($class, $fields)
public function generateModelTemplates($methodUp, $fields)
{
foreach ($fields as $key => $field) {
if (!is_array($field) && Str::camel($field) == 'morphTo'){
return '';
}
foreach ($field as $item) {
return $this->addMethodsInMigration($class , $item , $key);
$this->addMethodsInMigration($item , $methodUp);
if(Str::camel($key) == 'belongsToMany'){
$this->createPivot($this->nameOfModel , $field);
}
}
// if($key == 'belongsToMany'){
////povit
// }

// if ($key == 'morphToMany'){
//povit
////pivot
// }
//
// (new Helper)->manyToManyTableName('blog' , 'category');
}
}

public function addMethodsInMigration(ClassType $class , $fields , $model)
{
$methodUp = $class->addMethod('up')
->addBody("Schema::create('foreign_key', function (Blueprint \$table) {".PHP_EOL."\t \$table->id();");
$methodUp->addBody($this->addFieldsInMethod($fields , $model));
$methodUp->addBody(PHP_EOL."});");

$class->addMethod('down')
->addBody("Schema::dropIfExists('foreign_key');");

return $class;
}

}

public function addFieldsInMethod($fields , $model)
public function addMethodsInMigration($fields , $methodUp)
{
$model = explode('::', $fields)[0];
$model2 = explode('::', $fields)[1];
// $table->foreignId('user_id')->constrained('users');
$field = "\t \$table->foreignId('".strtolower($model)."_id')->constrained('".Str::plural(Str::snake($model2))."');";
return $field;

$methodUp->addBody("Schema::table('".Str::plural(Str::snake($this->nameOfModel))."', function (Blueprint \$table) {");
$methodUp->addBody("\t \$table->foreignId('".strtolower($model2)."_id')->constrained('".Str::plural(Str::snake($model2))."');");
$methodUp->addBody("});");
}



public function touchAndPutContent($template): bool
{
// foreach (Finder::create()->files()
// ->name("*create_".Str::plural(Str::snake($this->nameOfMigration))."_table.php")
// ->in($this->pathOfMigration) as $file) {
// unlink($file->getPathname());
// }
$tableFileName = "create_foreign_key_table.php";
foreach (Finder::create()->files()
->name("*create_foreign_keys_table.php")
->in($this->pathOfMigration) as $file) {
unlink($file->getPathname());
}
$tableFileName = Carbon::now()->addYears(1)->format('Y_m_d_His_') ."create_foreign_keys_table.php";

$pathOfFile = $this->pathOfMigration.$tableFileName;
touch($pathOfFile);
file_put_contents($pathOfFile, $template);
Expand All @@ -116,4 +117,6 @@ public function __toString(): string
{
return $this->message;
}


}
19 changes: 16 additions & 3 deletions src/Classes/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,39 @@ public function generate(): string
public function migrationsGenerator($model): string
{
foreach ($model as $key => $fields) {
$relation = null;
if (key_exists('Relations', $fields)) {
$relation = $fields['Relations'];
}
$this->nameOfMigration = $key;
$namespace = new PhpNamespace('');
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
$class = $namespace->addClass('Create' . Str::plural($this->nameOfMigration) . 'Table');
$class->setExtends(Migration::class);
$this->addMethodsInMigration($class, $fields['Fields']);
$this->addMethodsInMigration($class, $fields['Fields'] , $relation);
$template = '<?php' . PHP_EOL . $namespace;
$this->touchAndPutContent($template);
$this->message .= "|-- Migration " . $this->nameOfMigration . " successfully generate" . PHP_EOL;
}
return $this->message;
}

public function addMethodsInMigration(ClassType $class , $fields)
public function addMethodsInMigration(ClassType $class , $fields , $relation)
{

$methodUp = $class->addMethod('up')
->addBody("Schema::create('".Str::plural(Str::snake($this->nameOfMigration))."', function (Blueprint \$table) {".PHP_EOL."\t \$table->id();");
$methodUp->addBody($this->addFieldsInMethod($fields));
$methodUp->addBody("\t \$table->timestamps();".PHP_EOL."});");
if ($relation != null) {
foreach ($relation as $key => $item) {
if (!is_array($item) && Str::camel($item) == 'morphTo') {
$methodUp->addBody("\t \$table->integer('" . strtolower($this->nameOfMigration) . "able_id');");
$methodUp->addBody("\t \$table->string('" . strtolower($this->nameOfMigration) . "able_type');");

}
}
}
$methodUp->addBody("\t \$table->timestamps();".PHP_EOL."});");
$class->addMethod('down')
->addBody("Schema::dropIfExists('".Str::plural(Str::snake($this->nameOfMigration))."');");
return $class;
Expand Down
Loading

0 comments on commit 35da9fd

Please sign in to comment.