Skip to content

Commit 35da9fd

Browse files
committed
first commit
1 parent e62700b commit 35da9fd

File tree

11 files changed

+260
-70
lines changed

11 files changed

+260
-70
lines changed

src/Classes/ControllerGenerator.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ class ControllerGenerator
2727
*/
2828
protected $baseRelationName;
2929
protected $attributes;
30+
<<<<<<< HEAD
31+
=======
32+
protected $config;
33+
>>>>>>> 73a07f9... first commit
3034

3135
public function __construct($module, $models)
3236
{
3337
$this->models = $models['Models'];
3438
$this->module = $module;
39+
$this->config = \config()->get('moduleConfig');
40+
3541
}
3642

3743
public function generate(): string
@@ -74,6 +80,7 @@ public function generateControllerTemplates($option): PhpNamespace
7480

7581
public function setMethodToController($class, $option, $namespace)
7682
{
83+
<<<<<<< HEAD
7784
if (strpos($option , 'R') == true) {
7885
$this->indexAndShowMethodGenerator($class);
7986
}
@@ -84,6 +91,18 @@ public function setMethodToController($class, $option, $namespace)
8491
$this->editAndUpdateMethodGenerator($class, $namespace);
8592
}
8693
if (strpos($option, 'D') == true) {
94+
=======
95+
if (str_contains($option , 'R')) {
96+
$this->indexAndShowMethodGenerator($class);
97+
}
98+
if (str_contains($option, 'C')) {
99+
$this->createAndStoreMethodGenerator($class);
100+
}
101+
if (str_contains($option, 'U')) {
102+
$this->editAndUpdateMethodGenerator($class , $namespace);
103+
}
104+
if (str_contains($option, 'D')) {
105+
>>>>>>> 73a07f9... first commit
87106
$this->destroyMethodGenerator($class);
88107
}
89108
}
@@ -93,6 +112,7 @@ public function indexAndShowMethodGenerator(classType $class)
93112
$method = $class->addMethod('index');
94113
if (key_exists('Relations', $this->attributes)) {
95114
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();' . PHP_EOL)
115+
<<<<<<< HEAD
96116
->addBody('return response()->json($' . strtolower($this->modelName) . 's);');
97117
} else {
98118
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
@@ -101,6 +121,16 @@ public function indexAndShowMethodGenerator(classType $class)
101121
$class->addMethod('show')
102122
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
103123
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
124+
=======
125+
->addBody($this->config['return']);
126+
} else {
127+
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
128+
->addBody($this->config['return']);
129+
}
130+
$class->addMethod('show')
131+
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
132+
->addBody($this->config['return'])
133+
>>>>>>> 73a07f9... first commit
104134
->addParameter('id')->setType('Int');
105135
}
106136

@@ -129,6 +159,12 @@ public function associateInStore($method)
129159
{
130160
if (key_exists('Relations', $this->attributes)) {
131161
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
162+
<<<<<<< HEAD
163+
=======
164+
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
165+
return '';
166+
}
167+
>>>>>>> 73a07f9... first commit
132168
foreach ($relations as $value) {
133169
$this->baseRelationName = explode('::', $value)[1];
134170
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
@@ -138,21 +174,36 @@ public function associateInStore($method)
138174
}
139175
}
140176

177+
<<<<<<< HEAD
141178
public function editAndUpdateMethodGenerator(ClassType $class, $namespace)
179+
=======
180+
public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
181+
>>>>>>> 73a07f9... first commit
142182
{
143183
$method = $class->addMethod('edit');
144184
if (key_exists('Relations', $this->attributes)) {
145185
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);' . PHP_EOL)
186+
<<<<<<< HEAD
146187
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
147188
} else {
148189
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
149190
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
191+
=======
192+
->addBody($this->config['return']);
193+
} else {
194+
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
195+
->addBody($this->config['return']);
196+
>>>>>>> 73a07f9... first commit
150197
};
151198
$method->addParameter('id')->setType('Int');
152199

153200
$method = $class->addMethod('update')
154201
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
202+
<<<<<<< HEAD
155203
$this->UpdateMethodFindIntoRelation($method, $namespace);
204+
=======
205+
$this->UpdateMethodFindIntoRelation($method , $namespace);
206+
>>>>>>> 73a07f9... first commit
156207
$this->associateInUpdate($method);
157208
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
158209
->addBody('$' . strtolower($this->modelName) . '->save();'.PHP_EOL)
@@ -164,10 +215,20 @@ public function editAndUpdateMethodGenerator(ClassType $class, $namespace)
164215
$method->addParameter('id')->setType('Int');
165216
}
166217

218+
<<<<<<< HEAD
167219
public function UpdateMethodFindIntoRelation($method, $namespace)
168220
{
169221
if (key_exists('Relations', $this->attributes)) {
170222
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
223+
=======
224+
public function UpdateMethodFindIntoRelation($method ,$namespace)
225+
{
226+
if (key_exists('Relations', $this->attributes)) {
227+
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
228+
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
229+
return '';
230+
}
231+
>>>>>>> 73a07f9... first commit
171232
foreach ($relations as $value) {
172233
$this->baseRelationName = explode('::', $value)[1];
173234
$method->addBody('$' . strtolower($this->baseRelationName) . ' = ' . ucfirst($this->baseRelationName) . '::query()->findOrFail($request->' . strtolower($this->baseRelationName) . '_id);');
@@ -181,6 +242,12 @@ public function associateInUpdate($method)
181242
{
182243
if (key_exists('Relations', $this->attributes)) {
183244
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
245+
<<<<<<< HEAD
246+
=======
247+
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
248+
return '';
249+
}
250+
>>>>>>> 73a07f9... first commit
184251
foreach ($relations as $value) {
185252
$this->baseRelationName = explode('::', $value)[1];
186253
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
@@ -194,7 +261,11 @@ public function destroyMethodGenerator(ClassType $class)
194261
{
195262
$class->addMethod('destroy')
196263
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);' . PHP_EOL)
264+
<<<<<<< HEAD
197265
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
266+
=======
267+
->addBody($this->config['return'])
268+
>>>>>>> 73a07f9... first commit
198269
->addParameter('id')->setType('Int');
199270
}
200271

src/Classes/ForeignKeyGenerator.php

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
namespace Shetabit\ModuleGenerator\Classes;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Migrations\Migration;
67
use Illuminate\Database\Schema\Blueprint;
78
use Illuminate\Support\Facades\Schema;
89
use Illuminate\Support\Str;
9-
use Nette\PhpGenerator\ClassType;
1010
use Nette\PhpGenerator\PhpNamespace;
11-
use Shetabit\ModuleGenerator\Helpers\Helper;
11+
use Shetabit\ModuleGenerator\Traits\CreatePivotTable;
1212
use Symfony\Component\Finder\Finder;
1313

1414
class ForeignKeyGenerator
1515
{
16+
17+
use CreatePivotTable;
18+
1619
protected string $message = '';
1720
protected $models;
1821
protected $module;
1922
protected $fields;
20-
protected $nameOfMigration;
2123
protected $pathOfMigration;
2224
protected $nameOfModel;
2325

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

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

5155
public function foreignKeyGenerator($model , $class)
5256
{
57+
58+
$methodUp = $class->addMethod('up');
5359
foreach ($model as $key => $fields) {
60+
$this->nameOfModel = $key;
5461
if (!key_exists('Relations', $fields)) return '';
55-
return $this->generateModelTemplates($class , $fields['Relations']);
62+
$this->generateModelTemplates($methodUp , $fields['Relations']);
63+
5664
}
5765

5866
}
5967

60-
public function generateModelTemplates($class, $fields)
68+
public function generateModelTemplates($methodUp, $fields)
6169
{
6270
foreach ($fields as $key => $field) {
71+
if (!is_array($field) && Str::camel($field) == 'morphTo'){
72+
return '';
73+
}
6374
foreach ($field as $item) {
64-
return $this->addMethodsInMigration($class , $item , $key);
75+
$this->addMethodsInMigration($item , $methodUp);
76+
if(Str::camel($key) == 'belongsToMany'){
77+
$this->createPivot($this->nameOfModel , $field);
78+
}
6579
}
66-
// if($key == 'belongsToMany'){
67-
////povit
68-
// }
80+
6981
// if ($key == 'morphToMany'){
70-
//povit
82+
////pivot
7183
// }
72-
//
73-
// (new Helper)->manyToManyTableName('blog' , 'category');
74-
}
75-
}
7684

77-
public function addMethodsInMigration(ClassType $class , $fields , $model)
78-
{
79-
$methodUp = $class->addMethod('up')
80-
->addBody("Schema::create('foreign_key', function (Blueprint \$table) {".PHP_EOL."\t \$table->id();");
81-
$methodUp->addBody($this->addFieldsInMethod($fields , $model));
82-
$methodUp->addBody(PHP_EOL."});");
83-
84-
$class->addMethod('down')
85-
->addBody("Schema::dropIfExists('foreign_key');");
86-
87-
return $class;
85+
}
8886

8987
}
9088

91-
public function addFieldsInMethod($fields , $model)
89+
public function addMethodsInMigration($fields , $methodUp)
9290
{
9391
$model = explode('::', $fields)[0];
9492
$model2 = explode('::', $fields)[1];
95-
// $table->foreignId('user_id')->constrained('users');
96-
$field = "\t \$table->foreignId('".strtolower($model)."_id')->constrained('".Str::plural(Str::snake($model2))."');";
97-
return $field;
93+
94+
$methodUp->addBody("Schema::table('".Str::plural(Str::snake($this->nameOfModel))."', function (Blueprint \$table) {");
95+
$methodUp->addBody("\t \$table->foreignId('".strtolower($model2)."_id')->constrained('".Str::plural(Str::snake($model2))."');");
96+
$methodUp->addBody("});");
9897
}
9998

10099

100+
101101
public function touchAndPutContent($template): bool
102102
{
103-
// foreach (Finder::create()->files()
104-
// ->name("*create_".Str::plural(Str::snake($this->nameOfMigration))."_table.php")
105-
// ->in($this->pathOfMigration) as $file) {
106-
// unlink($file->getPathname());
107-
// }
108-
$tableFileName = "create_foreign_key_table.php";
103+
foreach (Finder::create()->files()
104+
->name("*create_foreign_keys_table.php")
105+
->in($this->pathOfMigration) as $file) {
106+
unlink($file->getPathname());
107+
}
108+
$tableFileName = Carbon::now()->addYears(1)->format('Y_m_d_His_') ."create_foreign_keys_table.php";
109+
109110
$pathOfFile = $this->pathOfMigration.$tableFileName;
110111
touch($pathOfFile);
111112
file_put_contents($pathOfFile, $template);
@@ -116,4 +117,6 @@ public function __toString(): string
116117
{
117118
return $this->message;
118119
}
120+
121+
119122
}

src/Classes/MigrationGenerator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,39 @@ public function generate(): string
3838
public function migrationsGenerator($model): string
3939
{
4040
foreach ($model as $key => $fields) {
41+
$relation = null;
42+
if (key_exists('Relations', $fields)) {
43+
$relation = $fields['Relations'];
44+
}
4145
$this->nameOfMigration = $key;
4246
$namespace = new PhpNamespace('');
4347
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
4448
$class = $namespace->addClass('Create' . Str::plural($this->nameOfMigration) . 'Table');
4549
$class->setExtends(Migration::class);
46-
$this->addMethodsInMigration($class, $fields['Fields']);
50+
$this->addMethodsInMigration($class, $fields['Fields'] , $relation);
4751
$template = '<?php' . PHP_EOL . $namespace;
4852
$this->touchAndPutContent($template);
4953
$this->message .= "|-- Migration " . $this->nameOfMigration . " successfully generate" . PHP_EOL;
5054
}
5155
return $this->message;
5256
}
5357

54-
public function addMethodsInMigration(ClassType $class , $fields)
58+
public function addMethodsInMigration(ClassType $class , $fields , $relation)
5559
{
60+
5661
$methodUp = $class->addMethod('up')
5762
->addBody("Schema::create('".Str::plural(Str::snake($this->nameOfMigration))."', function (Blueprint \$table) {".PHP_EOL."\t \$table->id();");
5863
$methodUp->addBody($this->addFieldsInMethod($fields));
59-
$methodUp->addBody("\t \$table->timestamps();".PHP_EOL."});");
64+
if ($relation != null) {
65+
foreach ($relation as $key => $item) {
66+
if (!is_array($item) && Str::camel($item) == 'morphTo') {
67+
$methodUp->addBody("\t \$table->integer('" . strtolower($this->nameOfMigration) . "able_id');");
68+
$methodUp->addBody("\t \$table->string('" . strtolower($this->nameOfMigration) . "able_type');");
6069

70+
}
71+
}
72+
}
73+
$methodUp->addBody("\t \$table->timestamps();".PHP_EOL."});");
6174
$class->addMethod('down')
6275
->addBody("Schema::dropIfExists('".Str::plural(Str::snake($this->nameOfMigration))."');");
6376
return $class;

0 commit comments

Comments
 (0)