@@ -28,24 +28,29 @@ class ControllerGenerator
28
28
*/
29
29
protected $ baseRelationName ;
30
30
protected $ attributes ;
31
- protected $ config ;
31
+ protected $ return ;
32
32
33
33
public function __construct ($ module , $ models )
34
34
{
35
35
$ this ->models = $ models ['Models ' ];
36
36
$ this ->module = $ module ;
37
- $ this -> config = \config ()-> get ( ' moduleConfig ' );
38
-
37
+ $ config = \config (' generator ' );
38
+ $ this -> return = $ config [ ' return_statement ' ];
39
39
}
40
40
41
41
public function generate (): string
42
42
{
43
43
foreach ($ this ->models as $ model => $ this ->attributes ) {
44
44
$ this ->modelName = $ model ;
45
- if (!key_exists ('CRUD ' , $ this ->attributes )) return '' ;
45
+ if (!key_exists ('CRUD ' , $ this ->attributes )) {
46
+ return '' ;
47
+ }
46
48
$ this ->CRUD = $ this ->attributes ['CRUD ' ];
49
+
47
50
return $ this ->controllerGenerator ($ this ->module );
48
51
}
52
+
53
+ return '' ;
49
54
}
50
55
51
56
public function controllerGenerator ($ module ): string
@@ -57,8 +62,9 @@ public function controllerGenerator($module): string
57
62
$ template = '<?php ' . PHP_EOL . $ template ;
58
63
$ this ->createDirectory ();
59
64
$ this ->touchAndPutContent ($ template );
60
- $ this ->message .= "|-- " . $ this ->nameController . "Controller successfully generate " . PHP_EOL ;
65
+ $ this ->message .= "|-- " . $ this ->nameController . "Controller successfully generated " . PHP_EOL ;
61
66
}
67
+
62
68
return $ this ->message ;
63
69
}
64
70
@@ -96,45 +102,47 @@ public function indexAndShowMethodGenerator(classType $class)
96
102
{
97
103
$ method = $ class ->addMethod ('index ' );
98
104
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 ) );
101
107
} 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 ) );
104
110
}
105
111
$ 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 () )
108
114
->addParameter ('id ' )->setType ('Int ' );
109
115
}
110
116
111
- public function createAndStoreMethodGenerator (ClassType $ class )
117
+
118
+ public function createAndStoreMethodGenerator (ClassType $ class ): void
112
119
{
113
120
$ class ->addMethod ('create ' );
114
121
if (!key_exists ('Relations ' , $ this ->attributes )) {
115
122
$ method = $ class ->addMethod ('store ' )
116
123
->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 ;
120
128
}
121
129
$ method = $ class ->addMethod ('store ' )
122
130
->addBody ('$ ' . strtolower ($ this ->modelName ) . ' = new ' . ucfirst ($ this ->modelName ) . '(); ' )
123
131
->addBody ('$ ' . strtolower ($ this ->modelName ) . '->fill($request->all()); ' );
124
132
$ this ->associateInStore ($ method );
125
133
$ method ->addBody ('$ ' . strtolower ($ this ->modelName ) . '->save(); ' )
126
134
->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);
130
138
}
131
139
132
- public function associateInStore ($ method )
140
+ public function associateInStore ($ method ): void
133
141
{
134
142
if (key_exists ('Relations ' , $ this ->attributes )) {
135
143
foreach ($ this ->attributes ['Relations ' ] as $ typeRelation => $ relations ) {
136
144
if (!is_array ($ relations ) && Str::camel ($ relations ) == 'morphTo ' ){
137
- return '' ;
145
+ return ;
138
146
}
139
147
foreach ($ relations as $ value ) {
140
148
$ this ->baseRelationName = explode (':: ' , $ value )[1 ];
@@ -150,11 +158,11 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
150
158
{
151
159
$ method = $ class ->addMethod ('edit ' );
152
160
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 () );
155
163
} 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 () );
158
166
};
159
167
$ method ->addParameter ('id ' )->setType ('Int ' );
160
168
@@ -164,8 +172,8 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
164
172
$ this ->UpdateMethodFindIntoRelation ($ method , $ namespace );
165
173
$ this ->associateInUpdate ($ method );
166
174
$ 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 () )
169
177
->addComment ('Update the specified resource in storage. ' )
170
178
->addComment ('@param Request $request ' )
171
179
->addComment ('@param int $id ' );
@@ -174,12 +182,12 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
174
182
}
175
183
176
184
177
- public function UpdateMethodFindIntoRelation ($ method ,$ namespace )
185
+ public function UpdateMethodFindIntoRelation ($ method ,$ namespace ): void
178
186
{
179
187
if (key_exists ('Relations ' , $ this ->attributes )) {
180
188
foreach ($ this ->attributes ['Relations ' ] as $ typeRelation => $ relations ) {
181
189
if (!is_array ($ relations ) && Str::camel ($ relations ) == 'morphTo ' ){
182
- return '' ;
190
+ return ;
183
191
}
184
192
foreach ($ relations as $ value ) {
185
193
$ this ->baseRelationName = explode (':: ' , $ value )[1 ];
@@ -190,12 +198,12 @@ public function UpdateMethodFindIntoRelation($method ,$namespace)
190
198
}
191
199
}
192
200
193
- public function associateInUpdate ($ method )
201
+ public function associateInUpdate ($ method ): void
194
202
{
195
203
if (key_exists ('Relations ' , $ this ->attributes )) {
196
204
foreach ($ this ->attributes ['Relations ' ] as $ typeRelation => $ relations ) {
197
205
if (!is_array ($ relations ) && Str::camel ($ relations ) == 'morphTo ' ){
198
- return '' ;
206
+ return ;
199
207
}
200
208
foreach ($ relations as $ value ) {
201
209
$ this ->baseRelationName = explode (':: ' , $ value )[1 ];
@@ -209,15 +217,15 @@ public function associateInUpdate($method)
209
217
public function destroyMethodGenerator (ClassType $ class )
210
218
{
211
219
$ 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 () )
214
222
->addParameter ('id ' )->setType ('Int ' );
215
223
}
216
224
217
225
public function createDirectory ()
218
226
{
219
227
if (!is_dir ($ this ->pathOfController )) {
220
- mkdir ($ this ->pathOfController , 0777 , true );
228
+ mkdir ($ this ->pathOfController , 0775 , true );
221
229
}
222
230
}
223
231
@@ -228,6 +236,23 @@ public function touchAndPutContent($template): bool
228
236
return true ;
229
237
}
230
238
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
+
231
256
public function __toString (): string
232
257
{
233
258
return $ this ->message ;
0 commit comments