11<?php
22
3- namespace Shetabit \ModuleGenerator \Classes ;
3+ namespace Shetabit \ModuleGenerator \Classes \ Generators ;
44
55use App \Http \Controllers \Controller ;
66use Illuminate \Http \Request ;
77use Illuminate \Support \Str ;
88use Nette \PhpGenerator \ClassType ;
99use Nette \PhpGenerator \PhpNamespace ;
10+ use Shetabit \ModuleGenerator \Classes \ModelName ;
1011use Shetabit \ModuleGenerator \Helpers \Helper ;
1112
1213class 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
0 commit comments