Skip to content

Commit 9d7d191

Browse files
Init
1 parent 5aa9eda commit 9d7d191

File tree

10 files changed

+198
-125
lines changed

10 files changed

+198
-125
lines changed

src/Classes/ControllerGenerator.php renamed to src/Classes/Generators/ControllerGenerator.php

Lines changed: 83 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?php
22

3-
namespace Shetabit\ModuleGenerator\Classes;
3+
namespace Shetabit\ModuleGenerator\Classes\Generators;
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Str;
88
use Nette\PhpGenerator\ClassType;
99
use Nette\PhpGenerator\PhpNamespace;
10+
use Shetabit\ModuleGenerator\Classes\ModelName;
1011
use Shetabit\ModuleGenerator\Helpers\Helper;
1112

1213
class ControllerGenerator
1314
{
1415

1516
public string $message = '';
1617
protected $models;
17-
protected $modelName;
18+
protected ModelName $modelName;
1819
protected $module;
1920
protected $pathOfController;
2021
protected $CRUD;
21-
protected $nameController;
2222
/**
2323
* @var mixed|string
2424
*/
@@ -40,58 +40,65 @@ public function __construct($module, $models)
4040

4141
public function generate(): string
4242
{
43+
$message = '';
4344
foreach ($this->models as $model => $this->attributes) {
44-
$this->modelName = $model;
45+
$this->modelName = new ModelName($model);
4546
if (!key_exists('CRUD', $this->attributes)) {
4647
return '';
4748
}
4849
$this->CRUD = $this->attributes['CRUD'];
4950

50-
return $this->controllerGenerator($this->module);
51+
$message .= $this->controllerGenerator($this->module);
5152
}
5253

53-
return '';
54+
return $message;
5455
}
5556

5657
public function controllerGenerator($module): string
5758
{
59+
$message = '';
5860
foreach ($this->CRUD as $name => $option) {
59-
$this->nameController = $name;
60-
$this->pathOfController = module_path($module) . "/Http/Controllers/" . $this->nameController . "/";
61-
$template = $this->generateControllerTemplates($option[0]);
61+
$this->pathOfController = module_path($module) . "/Http/Controllers/" . $name . '/';
62+
$template = $this->generateControllerTemplates($option[0], $name);
6263
$template = '<?php' . PHP_EOL . $template;
6364
$this->createDirectory();
6465
$this->touchAndPutContent($template);
65-
$this->message .= "|-- " . $this->nameController . "Controller successfully generated" . PHP_EOL;
66+
$message .= "|-- " . $this->modelName . "Controller ({$name}) successfully generated" . PHP_EOL;
6667
}
6768

68-
return $this->message;
69+
return $message;
6970
}
7071

71-
public function generateControllerTemplates($option): PhpNamespace
72+
public function generateControllerTemplates($option, $name): PhpNamespace
7273
{
73-
$namespace = new PhpNamespace('Modules\\' . $this->module . '\Http\Controllers\\' . $this->nameController);
74+
$namespace = new PhpNamespace('Modules\\' . $this->module . '\Http\Controllers\\' . $name);
7475
$namespace->addUse(Controller::class)
7576
->addUse(Request::class)
7677
->addUse('Modules\\' . $this->module . '\Entities\\' . $this->modelName);
77-
$class = $namespace->addClass($this->nameController . "Controller");
78+
if ($this->hasCreate($option)) {
79+
$namespace->addUse($this->getStoreRequestNamespace($name));
80+
}
81+
if ($this->hasUpdate($option)) {
82+
$namespace->addUse($this->getUpdateRequestNamespace($name));
83+
}
84+
$class = $namespace->addClass($this->modelName . "Controller");
7885
$class->setExtends(Controller::class);
7986

80-
$this->setMethodToController($class, $option, $namespace);
87+
$this->setMethodToController($class, $option, $namespace, $name);
8188

8289
return $namespace;
8390
}
8491

85-
public function setMethodToController($class, $option, $namespace)
92+
public function setMethodToController($class, $option, $namespace, $userName)
8693
{
8794
if (str_contains($option , 'R')) {
8895
$this->indexAndShowMethodGenerator($class);
8996
}
90-
if (str_contains($option, 'C')) {
91-
$this->createAndStoreMethodGenerator($class);
97+
if ($this->hasCreate($option)) {
98+
$this->storeMethodGenerator($class, $userName);
9299
}
93-
if (str_contains($option, 'U')) {
94-
$this->editAndUpdateMethodGenerator($class , $namespace);
100+
if ($this->hasUpdate($option)) {
101+
$this->updateMethodGenerator($class , $namespace, $userName);
95102
}
96103
if (str_contains($option, 'D')) {
97104
$this->destroyMethodGenerator($class);
@@ -101,84 +108,71 @@ public function setMethodToController($class, $option, $namespace)
101108
public function indexAndShowMethodGenerator(classType $class)
102109
{
103110
$method = $class->addMethod('index');
104-
if (key_exists('Relations', $this->attributes)) {
105-
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();')
111+
if (key_exists('Relations', $this->attributes) && !empty($this->attributes['Relations'])) {
112+
$method->addBody('$' . $this->modelName->getPluralForController() . ' = ' . $this->modelName . '::withCommonRelations()->get();')
106113
->addBody($this->getReturnStatement(true));
107114
} else {
108-
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();')
115+
$method->addBody('$' . $this->modelName->getPluralForController() . ' = ' . $this->modelName . '::query()->get();')
109116
->addBody($this->getReturnStatement(true));
110117
}
111-
$class->addMethod('show')
112-
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);')
113-
->addBody($this->getReturnStatement())
114-
->addParameter('id')->setType('Int');
118+
$method = $class->addMethod('show');
119+
if (key_exists('Relations', $this->attributes) && !empty($this->attributes['Relations'])) {
120+
$method->addBody('$' . $this->modelName->getSingularForController() . ' = ' . $this->modelName . '::withCommonRelations()->findOrFail($id);');
121+
} else {
122+
$method->addBody('$' . $this->modelName->getSingularForController() . ' = ' . $this->modelName . '::findOrFail($id);');
123+
}
124+
$method->addBody($this->getReturnStatement())
125+
->addParameter('id');
115126
}
116127

117128

118-
public function createAndStoreMethodGenerator(ClassType $class): void
129+
public function storeMethodGenerator(ClassType $class, $userName): void
119130
{
120-
$class->addMethod('create');
121-
if (!key_exists('Relations', $this->attributes)) {
122-
$method = $class->addMethod('store')
123-
->addComment('Store a newly created resource in storage')
124-
->addComment('@param Request $request')
125-
->addBody($this->getReturnStatement())
126-
->addParameter('request')->setType(Request::class);
127-
return;
128-
}
129131
$method = $class->addMethod('store')
130-
->addBody('$' . strtolower($this->modelName) . ' = new ' . ucfirst($this->modelName) . '();')
131-
->addBody('$' . strtolower($this->modelName) . '->fill($request->all());');
132+
->addBody('$' . $this->modelName->getSingularForController() . ' = new ' . $this->modelName . '();')
133+
->addBody('$' . $this->modelName->getSingularForController() . '->fill($request->all());');
132134
$this->associateInStore($method);
133-
$method->addBody('$' . strtolower($this->modelName) . '->save();')
135+
$method->addBody('$' . $this->modelName->getSingularForController() . '->save();')
134136
->addComment('Store a newly created resource in storage')
135137
->addComment('@param Request $request')
136138
->addBody($this->getReturnStatement())
137-
->addParameter('request')->setType(Request::class);
139+
->addParameter('request')
140+
->setType($this->getStoreRequestNamespace($userName));
138141
}
139142

140143
public function associateInStore($method): void
141144
{
142145
if (key_exists('Relations', $this->attributes)) {
143146
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
144-
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
147+
if ((!is_array($relations) && Str::camel($relations) == 'morphTo') || $this->doesRelationHaveAssociate($relations)){
145148
return;
146149
}
147150
foreach ($relations as $value) {
148151
$this->baseRelationName = explode('::', $value)[1];
149152
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
150-
$method->addBody('$' . strtolower($this->modelName) . '->' . strtolower($this->relationName) . '()->associate($request->' . strtolower($this->baseRelationName) . '_id);');
153+
$method->addBody('$' . $this->modelName->getSingularForController() . '->' . Str::camel($this->relationName) . '()->associate($request->' . strtolower($this->baseRelationName) . '_id);');
151154
}
152155
}
153156
}
154157
}
155158

156159

157-
public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
160+
public function updateMethodGenerator(ClassType $class , $namespace, $userName)
158161
{
159-
$method = $class->addMethod('edit');
160-
if (key_exists('Relations', $this->attributes)) {
161-
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);')
162-
->addBody($this->getReturnStatement());
163-
} else {
164-
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);')
165-
->addBody($this->getReturnStatement());
166-
};
167-
$method->addParameter('id')->setType('Int');
168-
169162
$method = $class->addMethod('update')
170-
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
163+
->addBody('$' . $this->modelName->getSingularForController() . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
171164

172165
$this->UpdateMethodFindIntoRelation($method , $namespace);
173166
$this->associateInUpdate($method);
174-
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
175-
->addBody('$' . strtolower($this->modelName) . '->save();')
167+
$method->addBody('$' . $this->modelName->getSingularForController() . '->fill($request->all());')
168+
->addBody('$' . $this->modelName->getSingularForController() . '->save();')
176169
->addBody($this->getReturnStatement())
177170
->addComment('Update the specified resource in storage.')
178171
->addComment('@param Request $request')
179-
->addComment('@param int $id');
180-
$method->addParameter('request')->setType(Request::class);
181-
$method->addParameter('id')->setType('Int');
172+
->addComment('@param $id');
173+
$method->addParameter('request')
174+
->setType($this->getUpdateRequestNamespace($userName));
175+
$method->addParameter('id');
182176
}
183177

184178

@@ -208,7 +202,7 @@ public function associateInUpdate($method): void
208202
foreach ($relations as $value) {
209203
$this->baseRelationName = explode('::', $value)[1];
210204
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
211-
$method->addBody('$' . strtolower($this->modelName) . '->' . strtolower($this->relationName) . '()->associate($' . strtolower($this->baseRelationName) . ');');
205+
$method->addBody('$' . $this->modelName->getSingularForController() . '->' . strtolower($this->relationName) . '()->associate($' . strtolower($this->baseRelationName) . ');');
212206
}
213207
}
214208
}
@@ -217,9 +211,9 @@ public function associateInUpdate($method): void
217211
public function destroyMethodGenerator(ClassType $class)
218212
{
219213
$class->addMethod('destroy')
220-
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);')
214+
->addBody('$' . $this->modelName->getSingularForController() . ' = ' . ucfirst($this->modelName) . '::findOrFail($id)->destroy();')
221215
->addBody($this->getReturnStatement())
222-
->addParameter('id')->setType('Int');
216+
->addParameter('id');
223217
}
224218

225219
public function createDirectory()
@@ -231,26 +225,46 @@ public function createDirectory()
231225

232226
public function touchAndPutContent($template): bool
233227
{
234-
touch($this->pathOfController . $this->nameController . 'Controller.php');
235-
file_put_contents($this->pathOfController . $this->nameController . 'Controller.php', $template);
228+
touch($this->pathOfController . $this->modelName . 'Controller.php');
229+
file_put_contents($this->pathOfController . $this->modelName . 'Controller.php', $template);
230+
236231
return true;
237232
}
238233

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

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

247242
return $this->return;
248243
}
249244

250-
// It comes before return statement to initialize $data
251-
public function getDataStatement(): string
245+
public function getStoreRequestNamespace($userName)
246+
{
247+
return 'Modules\\' . $this->module . '\Http\Requests\\' . $userName . '\\' . "{$this->modelName}StoreRequest";
248+
}
249+
250+
public function getUpdateRequestNamespace($userName)
251+
{
252+
return 'Modules\\' . $this->module . '\Http\Requests\\' . $userName . '\\' . "{$this->modelName}UpdateRequest";
253+
}
254+
255+
public function hasCreate($option)
256+
{
257+
return str_contains($option, 'C');
258+
}
259+
260+
public function hasUpdate($option)
261+
{
262+
return str_contains($option, 'U');
263+
}
264+
265+
public function doesRelationHaveAssociate($relation)
252266
{
253-
return '$data = $' . $this->modelName . ';';
267+
return !in_array(Str::camel($relation), ['hasOne', 'hasMany', 'morphTo']);
254268
}
255269

256270
public function __toString(): string

src/Classes/ForeignKeyGenerator.php renamed to src/Classes/Generators/ForeignKeyGenerator.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Shetabit\ModuleGenerator\Classes;
3+
namespace Shetabit\ModuleGenerator\Classes\Generators;
44

55
use Carbon\Carbon;
66
use Illuminate\Database\Migrations\Migration;
@@ -36,11 +36,11 @@ public function generate(): string
3636
}
3737
$namespace = new PhpNamespace('');
3838

39-
foreach ($this->models as $key => $model) {
39+
foreach ($this->models as $model) {
4040
$continue = false;
41-
foreach ($model as $key2 => $relation) {
42-
$this->modelName = $key2;
43-
if (!key_exists('Relations', $relation)) {
41+
foreach ($model as $key => $relation) {
42+
$this->modelName = $key;
43+
if (!key_exists('Relations', $relation) && empty($relation['Relations'])) {
4444
$continue = true;
4545
}
4646
}
@@ -52,9 +52,11 @@ public function generate(): string
5252
$class->setExtends(Migration::class);
5353
$this->foreignKeyGenerator($model, $class);
5454
}
55-
$template = '<?php' . PHP_EOL . $namespace;
56-
$this->touchAndPutContent($template);
57-
$this->message .= "|-- Foreign keys successfully generated" . PHP_EOL;
55+
if (count($namespace->getClasses()) !== 0) {
56+
$template = '<?php' . PHP_EOL . $namespace;
57+
$this->touchAndPutContent($template);
58+
$this->message .= "|-- Foreign keys successfully generated" . PHP_EOL;
59+
}
5860

5961
return $this->message;
6062
}
@@ -101,7 +103,7 @@ public function addMethodsInMigration($fields , $methodUp)
101103
public function touchAndPutContent($template): bool
102104
{
103105
foreach (Finder::create()->files()
104-
->name("*create_foreign_keys_table.php")
106+
->name("*add_foreign_keys.php")
105107
->in($this->migrationPath) as $file) {
106108
unlink($file->getPathname());
107109
}

src/Classes/MigrationGenerator.php renamed to src/Classes/Generators/MigrationGenerator.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Shetabit\ModuleGenerator\Classes;
3+
namespace Shetabit\ModuleGenerator\Classes\Generators;
44

55
use Illuminate\Database\Migrations\Migration;
66
use Illuminate\Database\Schema\Blueprint;
@@ -43,15 +43,13 @@ public function migrationsGenerator($model): string
4343
{
4444
foreach ($model as $key => $fields) {
4545
$relation = null;
46-
if (key_exists('Relations', $fields)) {
47-
$relation = $fields['Relations'];
48-
}
46+
$relations = $fields['Relations'] ?? [];
4947
$this->migrationName = $key;
5048
$namespace = new PhpNamespace('');
5149
$namespace->addUse(Migration::class)->addUse(Blueprint::class)->addUse(Schema::class);
5250
$class = $namespace->addClass('Create' . Str::plural($this->migrationName) . 'Table');
5351
$class->setExtends(Migration::class);
54-
$this->addMethodsInMigration($class, $fields['Fields'] , $relation);
52+
$this->addMethodsInMigration($class, $fields['Fields'] , $relations);
5553
$template = '<?php' . PHP_EOL . $namespace;
5654
$this->touchAndPutContent($template);
5755
$this->message .= "|-- Migration " . $this->migrationName . " successfully generated" . PHP_EOL;
@@ -84,12 +82,17 @@ public function addMethodsInMigration(ClassType $class , $fields , $relation)
8482

8583
public function addFieldsInMethod($fields)
8684
{
85+
$fieldsString = '';
8786
foreach($fields as $key => $infoField){
8887
$field = " \$table->".$infoField['type']."('".$key."')";
89-
if (!key_exists('options', $infoField)) return $field.";";
90-
return $this->addOptionsInFields($field ,$infoField['options']);
88+
if (!key_exists('options', $infoField)) {
89+
$field .= ";";
90+
} else {
91+
$field = $this->addOptionsInFields($field ,$infoField['options']);
92+
}
93+
$fieldsString .= $field . PHP_EOL;
9194
}
92-
return $fields;
95+
return $fieldsString;
9396
}
9497

9598
public function addOptionsInFields($field , $options)

0 commit comments

Comments
 (0)