Skip to content

Commit 705572d

Browse files
first
1 parent 99e98a6 commit 705572d

File tree

173 files changed

+260
-13463
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+260
-13463
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"php": ">=7.4",
15-
"illuminate/support": "^8.0|8.*",
15+
"illuminate/support": "^7.0|^8.0",
1616
"nwidart/laravel-modules": "^8.2",
1717
"nette/php-generator": "v3.5.x-dev"
1818
},
@@ -30,7 +30,7 @@
3030
"extra": {
3131
"laravel": {
3232
"providers": [
33-
"Shetabit\\ModuleGenerator\\Provider\\GeneratorServiceProvider"
33+
"ModuleGeneratorServiceProvider"
3434
]
3535
}
3636
},

src/Classes/ControllerGenerator.php

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,29 @@ class ControllerGenerator
2828
*/
2929
protected $baseRelationName;
3030
protected $attributes;
31-
protected $config;
31+
protected $return;
3232

3333
public function __construct($module, $models)
3434
{
3535
$this->models = $models['Models'];
3636
$this->module = $module;
37-
$this->config = \config()->get('moduleConfig');
38-
37+
$config = \config('generator');
38+
$this->return = $config['return_statement'];
3939
}
4040

4141
public function generate(): string
4242
{
4343
foreach ($this->models as $model => $this->attributes) {
4444
$this->modelName = $model;
45-
if (!key_exists('CRUD', $this->attributes)) return '';
45+
if (!key_exists('CRUD', $this->attributes)) {
46+
return '';
47+
}
4648
$this->CRUD = $this->attributes['CRUD'];
49+
4750
return $this->controllerGenerator($this->module);
4851
}
52+
53+
return '';
4954
}
5055

5156
public function controllerGenerator($module): string
@@ -57,8 +62,9 @@ public function controllerGenerator($module): string
5762
$template = '<?php' . PHP_EOL . $template;
5863
$this->createDirectory();
5964
$this->touchAndPutContent($template);
60-
$this->message .= "|-- " . $this->nameController . "Controller successfully generate" . PHP_EOL;
65+
$this->message .= "|-- " . $this->nameController . "Controller successfully generated" . PHP_EOL;
6166
}
67+
6268
return $this->message;
6369
}
6470

@@ -96,45 +102,47 @@ public function indexAndShowMethodGenerator(classType $class)
96102
{
97103
$method = $class->addMethod('index');
98104
if (key_exists('Relations', $this->attributes)) {
99-
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();' . PHP_EOL)
100-
->addBody($this->config['return']);
105+
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();')
106+
->addBody($this->getReturnStatement(true));
101107
} else {
102-
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
103-
->addBody($this->config['return']);
108+
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();')
109+
->addBody($this->getReturnStatement(true));
104110
}
105111
$class->addMethod('show')
106-
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
107-
->addBody($this->config['return'])
112+
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);')
113+
->addBody($this->getReturnStatement())
108114
->addParameter('id')->setType('Int');
109115
}
110116

111-
public function createAndStoreMethodGenerator(ClassType $class)
117+
118+
public function createAndStoreMethodGenerator(ClassType $class): void
112119
{
113120
$class->addMethod('create');
114121
if (!key_exists('Relations', $this->attributes)) {
115122
$method = $class->addMethod('store')
116123
->addComment('Store a newly created resource in storage')
117-
->addComment('@param Request $request');
118-
$method->addParameter('request')->setType(Request::class);
119-
return '';
124+
->addComment('@param Request $request')
125+
->addBody($this->getReturnStatement())
126+
->addParameter('request')->setType(Request::class);
127+
return;
120128
}
121129
$method = $class->addMethod('store')
122130
->addBody('$' . strtolower($this->modelName) . ' = new ' . ucfirst($this->modelName) . '();')
123131
->addBody('$' . strtolower($this->modelName) . '->fill($request->all());');
124132
$this->associateInStore($method);
125133
$method->addBody('$' . strtolower($this->modelName) . '->save();')
126134
->addComment('Store a newly created resource in storage')
127-
->addComment('@param Request $request');
128-
$method->addParameter('request')->setType(Request::class);
129-
135+
->addComment('@param Request $request')
136+
->addBody($this->getReturnStatement())
137+
->addParameter('request')->setType(Request::class);
130138
}
131139

132-
public function associateInStore($method)
140+
public function associateInStore($method): void
133141
{
134142
if (key_exists('Relations', $this->attributes)) {
135143
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
136144
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
137-
return '';
145+
return;
138146
}
139147
foreach ($relations as $value) {
140148
$this->baseRelationName = explode('::', $value)[1];
@@ -150,11 +158,11 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
150158
{
151159
$method = $class->addMethod('edit');
152160
if (key_exists('Relations', $this->attributes)) {
153-
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);' . PHP_EOL)
154-
->addBody($this->config['return']);
161+
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);')
162+
->addBody($this->getReturnStatement());
155163
} else {
156-
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
157-
->addBody($this->config['return']);
164+
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);')
165+
->addBody($this->getReturnStatement());
158166
};
159167
$method->addParameter('id')->setType('Int');
160168

@@ -164,8 +172,8 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
164172
$this->UpdateMethodFindIntoRelation($method , $namespace);
165173
$this->associateInUpdate($method);
166174
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
167-
->addBody('$' . strtolower($this->modelName) . '->save();'.PHP_EOL)
168-
->addBody('return response()->json($' . strtolower($this->modelName).');')
175+
->addBody('$' . strtolower($this->modelName) . '->save();')
176+
->addBody($this->getReturnStatement())
169177
->addComment('Update the specified resource in storage.')
170178
->addComment('@param Request $request')
171179
->addComment('@param int $id');
@@ -174,12 +182,12 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
174182
}
175183

176184

177-
public function UpdateMethodFindIntoRelation($method ,$namespace)
185+
public function UpdateMethodFindIntoRelation($method ,$namespace): void
178186
{
179187
if (key_exists('Relations', $this->attributes)) {
180188
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
181189
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
182-
return '';
190+
return;
183191
}
184192
foreach ($relations as $value) {
185193
$this->baseRelationName = explode('::', $value)[1];
@@ -190,12 +198,12 @@ public function UpdateMethodFindIntoRelation($method ,$namespace)
190198
}
191199
}
192200

193-
public function associateInUpdate($method)
201+
public function associateInUpdate($method): void
194202
{
195203
if (key_exists('Relations', $this->attributes)) {
196204
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
197205
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
198-
return '';
206+
return;
199207
}
200208
foreach ($relations as $value) {
201209
$this->baseRelationName = explode('::', $value)[1];
@@ -209,15 +217,15 @@ public function associateInUpdate($method)
209217
public function destroyMethodGenerator(ClassType $class)
210218
{
211219
$class->addMethod('destroy')
212-
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);' . PHP_EOL)
213-
->addBody($this->config['return'])
220+
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);')
221+
->addBody($this->getReturnStatement())
214222
->addParameter('id')->setType('Int');
215223
}
216224

217225
public function createDirectory()
218226
{
219227
if (!is_dir($this->pathOfController)) {
220-
mkdir($this->pathOfController, 0777, true);
228+
mkdir($this->pathOfController, 0775, true);
221229
}
222230
}
223231

@@ -228,6 +236,23 @@ public function touchAndPutContent($template): bool
228236
return true;
229237
}
230238

239+
public function getReturnStatement($plural = false): string
240+
{
241+
if (str_contains($this->return, ':data')) {
242+
$modelNameInReturn = $plural ? Str::plural(Str::camel($this->modelName)) : Str::camel($this->modelName);
243+
244+
return PHP_EOL . str_replace(':data', '$' . $modelNameInReturn, $this->return);
245+
}
246+
247+
return $this->return;
248+
}
249+
250+
// It comes before return statement to initialize $data
251+
public function getDataStatement(): string
252+
{
253+
return '$data = $' . $this->modelName . ';';
254+
}
255+
231256
public function __toString(): string
232257
{
233258
return $this->message;

0 commit comments

Comments
 (0)