44
55use App \Http \Controllers \Controller ;
66use Illuminate \Http \Request ;
7- use Illuminate \Support \Str ;
87use Nette \PhpGenerator \ClassType ;
98use Nette \PhpGenerator \PhpNamespace ;
109use Shetabit \ModuleGenerator \Helpers \Helper ;
@@ -28,14 +27,11 @@ class ControllerGenerator
2827 */
2928 protected $ baseRelationName ;
3029 protected $ attributes ;
31- protected $ config ;
3230
3331 public function __construct ($ module , $ models )
3432 {
3533 $ this ->models = $ models ['Models ' ];
3634 $ this ->module = $ module ;
37- $ this ->config = \config ()->get ('moduleConfig ' );
38-
3935 }
4036
4137 public function generate (): string
@@ -78,16 +74,16 @@ public function generateControllerTemplates($option): PhpNamespace
7874
7975 public function setMethodToController ($ class , $ option , $ namespace )
8076 {
81- if (str_contains ($ option , 'R ' )) {
77+ if (strpos ($ option , 'R ' ) == true ) {
8278 $ this ->indexAndShowMethodGenerator ($ class );
8379 }
84- if (str_contains ($ option , 'C ' )) {
80+ if (strpos ($ option , 'C ' ) == true ) {
8581 $ this ->createAndStoreMethodGenerator ($ class );
8682 }
87- if (str_contains ($ option , 'U ' )) {
88- $ this ->editAndUpdateMethodGenerator ($ class , $ namespace );
83+ if (strpos ($ option , 'U ' ) == true ) {
84+ $ this ->editAndUpdateMethodGenerator ($ class , $ namespace );
8985 }
90- if (str_contains ($ option , 'D ' )) {
86+ if (strpos ($ option , 'D ' ) == true ) {
9187 $ this ->destroyMethodGenerator ($ class );
9288 }
9389 }
@@ -97,14 +93,14 @@ public function indexAndShowMethodGenerator(classType $class)
9793 $ method = $ class ->addMethod ('index ' );
9894 if (key_exists ('Relations ' , $ this ->attributes )) {
9995 $ method ->addBody ('$ ' . strtolower ($ this ->modelName ) . 's = ' . ucfirst ($ this ->modelName ) . '::withCommonRelations()->get(); ' . PHP_EOL )
100- ->addBody ($ this ->config [ ' return ' ] );
96+ ->addBody (' return response()->json($ ' . strtolower ( $ this ->modelName ) . ' s); ' );
10197 } else {
10298 $ method ->addBody ('$ ' . strtolower ($ this ->modelName ) . 's = ' . ucfirst ($ this ->modelName ) . '::query()->get(); ' . PHP_EOL )
103- ->addBody ($ this ->config [ ' return ' ] );
99+ ->addBody (' return response()->json($ ' . strtolower ( $ this ->modelName ) . ' s); ' );
104100 }
105101 $ class ->addMethod ('show ' )
106102 ->addBody ('$ ' . strtolower ($ this ->modelName ) . ' = ' . ucfirst ($ this ->modelName ) . '::query()->findOrFail($id); ' . PHP_EOL )
107- ->addBody ($ this ->config [ ' return ' ] )
103+ ->addBody (' return response()->json($ ' . strtolower ( $ this ->modelName ) . ' ); ' )
108104 ->addParameter ('id ' )->setType ('Int ' );
109105 }
110106
@@ -133,9 +129,6 @@ public function associateInStore($method)
133129 {
134130 if (key_exists ('Relations ' , $ this ->attributes )) {
135131 foreach ($ this ->attributes ['Relations ' ] as $ typeRelation => $ relations ) {
136- if (!is_array ($ relations ) && Str::camel ($ relations ) == 'morphTo ' ){
137- return '' ;
138- }
139132 foreach ($ relations as $ value ) {
140133 $ this ->baseRelationName = explode (':: ' , $ value )[1 ];
141134 $ this ->relationName = Helper::configurationRelationsName ($ this ->baseRelationName , $ typeRelation );
@@ -145,21 +138,21 @@ public function associateInStore($method)
145138 }
146139 }
147140
148- public function editAndUpdateMethodGenerator (ClassType $ class , $ namespace )
141+ public function editAndUpdateMethodGenerator (ClassType $ class , $ namespace )
149142 {
150143 $ method = $ class ->addMethod ('edit ' );
151144 if (key_exists ('Relations ' , $ this ->attributes )) {
152145 $ method ->addBody ('$ ' . strtolower ($ this ->modelName ) . ' = ' . ucfirst ($ this ->modelName ) . '::withCommonRelations()->findOrFail($id); ' . PHP_EOL )
153- ->addBody ($ this ->config [ ' return ' ] );
146+ ->addBody (' return response()->json($ ' . strtolower ( $ this ->modelName ) . ' ); ' );
154147 } else {
155148 $ method ->addBody ('$ ' . strtolower ($ this ->modelName ) . ' = ' . ucfirst ($ this ->modelName ) . '::query()->findOrFail($id); ' . PHP_EOL )
156- ->addBody ($ this ->config [ ' return ' ] );
149+ ->addBody (' return response()->json($ ' . strtolower ( $ this ->modelName ) . ' ); ' );
157150 };
158151 $ method ->addParameter ('id ' )->setType ('Int ' );
159152
160153 $ method = $ class ->addMethod ('update ' )
161154 ->addBody ('$ ' . strtolower ($ this ->modelName ) . ' = ' . ucfirst ($ this ->modelName ) . '::query()->findOrFail($id); ' );
162- $ this ->UpdateMethodFindIntoRelation ($ method , $ namespace );
155+ $ this ->UpdateMethodFindIntoRelation ($ method , $ namespace );
163156 $ this ->associateInUpdate ($ method );
164157 $ method ->addBody ('$ ' . strtolower ($ this ->modelName ) . '->fill($request->all()); ' )
165158 ->addBody ('$ ' . strtolower ($ this ->modelName ) . '->save(); ' .PHP_EOL )
@@ -171,13 +164,10 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
171164 $ method ->addParameter ('id ' )->setType ('Int ' );
172165 }
173166
174- public function UpdateMethodFindIntoRelation ($ method , $ namespace )
167+ public function UpdateMethodFindIntoRelation ($ method, $ namespace )
175168 {
176169 if (key_exists ('Relations ' , $ this ->attributes )) {
177170 foreach ($ this ->attributes ['Relations ' ] as $ typeRelation => $ relations ) {
178- if (!is_array ($ relations ) && Str::camel ($ relations ) == 'morphTo ' ){
179- return '' ;
180- }
181171 foreach ($ relations as $ value ) {
182172 $ this ->baseRelationName = explode (':: ' , $ value )[1 ];
183173 $ method ->addBody ('$ ' . strtolower ($ this ->baseRelationName ) . ' = ' . ucfirst ($ this ->baseRelationName ) . '::query()->findOrFail($request-> ' . strtolower ($ this ->baseRelationName ) . '_id); ' );
@@ -191,9 +181,6 @@ public function associateInUpdate($method)
191181 {
192182 if (key_exists ('Relations ' , $ this ->attributes )) {
193183 foreach ($ this ->attributes ['Relations ' ] as $ typeRelation => $ relations ) {
194- if (!is_array ($ relations ) && Str::camel ($ relations ) == 'morphTo ' ){
195- return '' ;
196- }
197184 foreach ($ relations as $ value ) {
198185 $ this ->baseRelationName = explode (':: ' , $ value )[1 ];
199186 $ this ->relationName = Helper::configurationRelationsName ($ this ->baseRelationName , $ typeRelation );
@@ -207,7 +194,7 @@ public function destroyMethodGenerator(ClassType $class)
207194 {
208195 $ class ->addMethod ('destroy ' )
209196 ->addBody ('$ ' . strtolower ($ this ->modelName ) . ' = ' . ucfirst ($ this ->modelName ) . '::destroy($id); ' . PHP_EOL )
210- ->addBody ($ this ->config [ ' return ' ] )
197+ ->addBody (' return response()->json($ ' . strtolower ( $ this ->modelName ) . ' ); ' )
211198 ->addParameter ('id ' )->setType ('Int ' );
212199 }
213200
0 commit comments