@@ -49,6 +49,13 @@ class CreateControllerCommand extends Command
4949 */
5050 protected $ type = 'Controller ' ;
5151
52+ /**
53+ * The request variable to use in the controller.
54+ *
55+ * @var string
56+ */
57+ protected $ requestVariable = '$request ' ;
58+
5259 /**
5360 * Get the stub file for the generator.
5461 *
@@ -80,7 +87,7 @@ public function handle()
8087 $ requestName = 'Request ' ;
8188 $ requestNameSpace = 'Illuminate\Http\Request ' ;
8289
83- if ($ input ->formRequest ) {
90+ if ($ input ->withFormRequest ) {
8491 $ requestName = $ input ->formRequestName ;
8592 $ requestNameSpace = $ this ->getRequestsNamespace ($ requestName );
8693 $ this ->makeFormRequest ($ input );
@@ -91,27 +98,25 @@ public function handle()
9198 $ viewVariablesForShow = $ this ->getCompactVariablesFor ($ fields , $ this ->getModelName ($ input ->modelName ), 'show ' );
9299 $ viewVariablesForEdit = $ this ->getCompactVariablesFor ($ fields , $ this ->getModelName ($ input ->modelName ), 'form ' );
93100 $ modelFullName = $ this ->getModelFullName ($ input ->modelDirectory , $ input ->modelName );
94- $ affirmMethod = $ this ->getAffirmMethod ($ input ->formRequest , $ fields , $ requestNameSpace );
101+ $ affirmMethod = $ this ->getAffirmMethod ($ input ->withFormRequest , $ fields , $ requestNameSpace );
95102 $ classToExtendFullname = $ this ->getFullClassToExtend ($ input ->extends );
96103 $ namespacesToUse = $ this ->getRequiredUseClasses ($ fields , [$ modelFullName , $ requestNameSpace , $ classToExtendFullname ]);
104+ $ dataMethod = $ this ->getDataMethod ($ fields , $ requestNameSpace . '\\' . $ requestName , $ input ->withFormRequest );
97105 $ stub = $ this ->getStubContent ('controller ' );
98106
99107 return $ this ->replaceViewNames ($ stub , $ input ->viewDirectory , $ input ->prefix )
108+ ->replaceGetDataMethod ($ stub , $ dataMethod )
109+ ->replaceCallDataMethod ($ stub , $ this ->getCallDataMethod ($ input ->withFormRequest ))
100110 ->replaceModelName ($ stub , $ input ->modelName )
101111 ->replaceNamespace ($ stub , $ this ->getControllersNamespace ())
102112 ->replaceControllerExtends ($ stub , $ this ->getControllerExtends ($ classToExtendFullname ))
103113 ->replaceUseCommandPlaceholder ($ stub , $ namespacesToUse )
104114 ->replaceRouteNames ($ stub , $ input ->modelName , $ input ->prefix )
105- ->replaceRequestName ($ stub , $ requestName )
106- ->replaceRequestFullName ($ stub , $ requestNameSpace )
107115 ->replaceConstructor ($ stub , $ this ->getConstructor ($ input ->withAuth ))
108- ->replaceCallAffirm ($ stub , $ this ->getCallAffirm ($ input ->formRequest ))
116+ ->replaceCallAffirm ($ stub , $ this ->getCallAffirm ($ input ->withFormRequest ))
109117 ->replaceAffirmMethod ($ stub , $ affirmMethod )
110118 ->replacePaginationNumber ($ stub , $ input ->perPage )
111- ->replaceFileSnippet ($ stub , $ this ->getFileSnippet ($ fields ))
112- ->replaceBooleadSnippet ($ stub , $ this ->getBooleanSnippet ($ fields ))
113- ->replaceStringToNullSnippet ($ stub , $ this ->getStringToNullSnippet ($ fields ))
114- ->replaceFileMethod ($ stub , $ this ->getUploadFileMethod ($ fields ))
119+ ->replaceFileMethod ($ stub , $ this ->getUploadFileMethod ($ fields , $ input ->withFormRequest ))
115120 ->replaceViewVariablesForIndex ($ stub , $ viewVariablesForIndex )
116121 ->replaceViewVariablesForShow ($ stub , $ viewVariablesForShow )
117122 ->replaceViewVariablesForCreate ($ stub , $ this ->getCompactVariablesFor ($ fields , null , 'form ' ))
@@ -124,10 +129,80 @@ public function handle()
124129 ->replaceAppName ($ stub , $ this ->getAppName ())
125130 ->replaceControllerName ($ stub , $ input ->controllerName )
126131 ->replaceDataVariable ($ stub , 'data ' )
132+ ->replaceRequestName ($ stub , $ requestName )
133+ ->replaceRequestFullName ($ stub , $ requestNameSpace )
134+ ->replaceRequestVariable ($ stub , ' ' . $ this ->requestVariable )
135+ ->replaceTypeHintedRequestName ($ stub , $ this ->getTypeHintedRequestName ($ requestName ))
127136 ->createFile ($ destenationFile , $ stub )
128137 ->info ('A controller was crafted successfully. ' );
129138 }
130139
140+ /**
141+ * Gets the signature of the getData method.
142+ *
143+ * @param array $fields
144+ * @param string $requestFullname
145+ * @param bool $withFormRequest
146+ *
147+ * @return string
148+ */
149+ protected function getDataMethod (array $ fields , $ requestFullname , $ withFormRequest )
150+ {
151+ if ($ withFormRequest ) {
152+ return '' ;
153+ }
154+
155+ $ stub = $ this ->getStubContent ('controller-getdata-method ' );
156+
157+ $ this ->replaceFileSnippet ($ stub , $ this ->getFileSnippet ($ fields ))
158+ ->replaceBooleadSnippet ($ stub , $ this ->getBooleanSnippet ($ fields ))
159+ ->replaceStringToNullSnippet ($ stub , $ this ->getStringToNullSnippet ($ fields ))
160+ ->replaceRequestNameComment ($ stub , $ this ->getRequestNameComment ($ requestFullname ))
161+ ->replaceMethodVisibilityLevel ($ stub , 'protected ' );
162+
163+ return $ stub ;
164+ }
165+
166+ /**
167+ * Gets the type hinted request name
168+ *
169+ * @param string $name
170+ *
171+ * @return string
172+ */
173+ protected function getTypeHintedRequestName ($ name )
174+ {
175+ return sprintf ('%s %s ' , $ name , $ this ->requestVariable );
176+ }
177+
178+ /**
179+ * Gets the comment for the request name
180+ *
181+ * @param string $name
182+ *
183+ * @return string
184+ */
185+ protected function getRequestNameComment ($ name )
186+ {
187+ return sprintf ('@param %s %s ' , $ name , $ this ->requestVariable );
188+ }
189+
190+ /**
191+ * Gets the method that calls the getData()
192+ *
193+ * @param bool $withFormRequest
194+ *
195+ * @return string
196+ */
197+ protected function getCallDataMethod ($ withFormRequest )
198+ {
199+ if ($ withFormRequest ) {
200+ return sprintf ('%s->getData() ' , $ this ->requestVariable );
201+ }
202+
203+ return sprintf ('$this->getData(%s) ' , $ this ->requestVariable );
204+ }
205+
131206 /**
132207 * Gets controller's constructor.
133208 *
@@ -146,11 +221,10 @@ protected function getConstructor($withAuth)
146221 $ starts = strpos ($ stub , '{ ' );
147222 $ ends = strrpos ($ stub , '} ' );
148223
149- if ($ starts !== false && $ ends !== false )
150- {
224+ if ($ starts !== false && $ ends !== false ) {
151225 $ content = trim (substr ($ stub , $ starts +1 , $ ends -$ starts -1 ));
152- if (!empty ($ content )) {
153- return $ stub ;
226+ if (!empty ($ content )) {
227+ return $ stub ;
154228 }
155229 }
156230
@@ -428,10 +502,9 @@ protected function getUseClassCommand($name)
428502 */
429503 protected function getRelationAccessor (ForeignRelationship $ collection )
430504 {
431-
432- return sprintf ('$%s = %s::pluck( \'%s \', \'%s \')->all(); ' ,
433- $ collection ->getCollectionName (),
434- $ collection ->getForeignModel (),
505+ return sprintf ('$%s = %s::pluck( \'%s \', \'%s \')->all(); ' ,
506+ $ collection ->getCollectionName (),
507+ $ collection ->getForeignModel (),
435508 $ collection ->getField (),
436509 $ collection ->getPrimaryKeyForForeignModel ()
437510 );
@@ -519,12 +592,13 @@ protected function getWithRelationsStatement(array $relations)
519592 * Gets the method's stub that handels the file uploading.
520593 *
521594 * @param array $fields
595+ * @param bool $withFormRequest
522596 *
523597 * @return string
524598 */
525- protected function getUploadFileMethod (array $ fields )
599+ protected function getUploadFileMethod (array $ fields, $ withFormRequest )
526600 {
527- if ($ this ->containsfile ($ fields )) {
601+ if (! $ withFormRequest && $ this ->containsfile ($ fields )) {
528602 return $ this ->getStubContent ('controller-upload-method ' , $ this ->getTemplateName ());
529603 }
530604
@@ -538,7 +612,7 @@ protected function getUploadFileMethod(array $fields)
538612 */
539613 protected function getDestenationFile ($ name )
540614 {
541- $ path = base_path ( $ this -> getAppNamespace () . Config::getControllersPath ());
615+ $ path = app_path ( Config::getControllersPath ());
542616
543617 return Helpers::postFixWith ($ path , '/ ' ) . $ name . '.php ' ;
544618 }
@@ -635,7 +709,7 @@ protected function getCommandInput()
635709 $ fields = $ this ->option ('fields ' );
636710 $ fieldsFile = $ this ->option ('fields-file ' );
637711 $ langFile = $ this ->option ('lang-file-name ' ) ?: strtolower (str_plural ($ modelName ));
638- $ formRequest = $ this ->option ('with-form-request ' );
712+ $ withFormRequest = $ this ->option ('with-form-request ' );
639713 $ force = $ this ->option ('force ' );
640714 $ modelDirectory = $ this ->option ('model-directory ' );
641715 $ formRequestName = $ plainControllerName . 'FormRequest ' ;
@@ -644,8 +718,8 @@ protected function getCommandInput()
644718 $ withAuth = $ this ->option ('with-auth ' );
645719
646720 return (object ) compact ('viewDirectory ' , 'viewName ' , 'modelName ' , 'prefix ' , 'perPage ' , 'fileSnippet ' , 'modelDirectory ' ,
647- 'langFile ' , 'fields ' , 'formRequest ' , 'formRequestName ' , 'force ' , 'fieldsFile ' , 'template ' ,
648- 'controllerName ' , 'extends ' ,'withAuth ' );
721+ 'langFile ' , 'fields ' , 'withFormRequest ' , 'formRequestName ' , 'force ' , 'fieldsFile ' , 'template ' ,
722+ 'controllerName ' , 'extends ' , 'withAuth ' );
649723 }
650724
651725 /**
@@ -663,6 +737,96 @@ protected function replaceOnStoreAction(&$stub, $commands)
663737 return $ this ;
664738 }
665739
740+ /**
741+ * Replaces call_get_data
742+ *
743+ * @param string $stub
744+ * @param string $code
745+ *
746+ * @return $this
747+ */
748+ protected function replaceCallDataMethod (&$ stub , $ code )
749+ {
750+ $ stub = $ this ->strReplace ('call_get_data ' , $ code , $ stub );
751+
752+ return $ this ;
753+ }
754+
755+ /**
756+ * Replaces request variable.
757+ *
758+ * @param string $stub
759+ * @param string $variable
760+ *
761+ * @return $this
762+ */
763+ protected function replaceRequestVariable (&$ stub , $ variable )
764+ {
765+ $ stub = $ this ->strReplace ('request_variable ' , $ variable , $ stub );
766+
767+ return $ this ;
768+ }
769+
770+ /**
771+ * Replaces the type_hinted_request_name for the given stub.
772+ *
773+ * @param $stub
774+ * @param $code
775+ *
776+ * @return $this
777+ */
778+ protected function replaceTypeHintedRequestName (&$ stub , $ code )
779+ {
780+ $ stub = $ this ->strReplace ('type_hinted_request_name ' , $ code , $ stub );
781+
782+ return $ this ;
783+ }
784+
785+ /**
786+ * Replaces the request_name_comment for the given stub.
787+ *
788+ * @param $stub
789+ * @param $comment
790+ *
791+ * @return $this
792+ */
793+ protected function replaceRequestNameComment (&$ stub , $ comment )
794+ {
795+ $ stub = $ this ->strReplace ('request_name_comment ' , $ comment , $ stub );
796+
797+ return $ this ;
798+ }
799+
800+ /**
801+ * Replaces get_data_method template.
802+ *
803+ * @param string $stub
804+ * @param string $code
805+ *
806+ * @return $this
807+ */
808+ protected function replaceGetDataMethod (&$ stub , $ code )
809+ {
810+ $ stub = $ this ->strReplace ('get_data_method ' , $ code , $ stub );
811+
812+ return $ this ;
813+ }
814+
815+ /**
816+ * Replaces the visibility level of a giving stub
817+ *
818+ * @param string $stub
819+ * @param string $level
820+ *
821+ * @return $this
822+ */
823+ protected function replaceMethodVisibilityLevel (&$ stub , $ level )
824+ {
825+ $ stub = $ this ->strReplace ('visibility_level ' , $ level , $ stub );
826+
827+ return $ this ;
828+ }
829+
666830 /**
667831 * Replaces the auth middleware
668832 *
@@ -1049,7 +1213,7 @@ protected function getBooleanSnippet(array $fields)
10491213
10501214 foreach ($ fields as $ field ) {
10511215 if ($ field ->isBoolean () && $ field ->isCheckbox ()) {
1052- $ code .= sprintf (" \$data['%s'] = \$ request ->has('%s'); " , $ field ->name , $ field ->name ) . PHP_EOL ;
1216+ $ code .= sprintf (" \$data['%s'] = %s ->has('%s'); " , $ field ->name , $ this -> requestVariable , $ field ->name ) . PHP_EOL ;
10531217 }
10541218 }
10551219
@@ -1059,7 +1223,7 @@ protected function getBooleanSnippet(array $fields)
10591223 /**
10601224 * Gets the affirm method.
10611225 *
1062- * @param bool $isFormRequest
1226+ * @param bool $withFormRequest
10631227 * @param array $fields
10641228 *
10651229 * @return string
@@ -1081,7 +1245,7 @@ protected function getAffirmMethod($withFormRequest, array $fields, $requestName
10811245 /**
10821246 * Gets the affirm method call.
10831247 *
1084- * @param bool $isFormRequest
1248+ * @param bool $withFormRequest
10851249 *
10861250 * @return string
10871251 */
0 commit comments