44
55use Illuminate \Console \Command ;
66use CrestApps \CodeGenerator \Traits \CommonCommand ;
7- use CrestApps \CodeGenerator \Support \Config ;
87use CrestApps \CodeGenerator \Traits \GeneratorReplacers ;
9- use CrestApps \CodeGenerator \Support \Helpers ;
108use CrestApps \CodeGenerator \Models \ForeignRelationship ;
9+ use CrestApps \CodeGenerator \Support \Config ;
10+ use CrestApps \CodeGenerator \Support \Helpers ;
11+ use CrestApps \CodeGenerator \Support \ViewLabelsGenerator ;
1112
1213class CreateControllerCommand extends Command
1314{
@@ -19,8 +20,8 @@ class CreateControllerCommand extends Command
1920 * @var string
2021 */
2122 protected $ signature = 'create:controller
22- {controller -name : The name of the controler .}
23- {--model -name= : The model name that this controller will represent .}
23+ {model -name : The model name that this controller will represent .}
24+ {--controller -name= : The name of the controler .}
2425 {--controller-directory= : The directory where the controller should be created under.}
2526 {--model-directory= : The path where the model should be created under.}
2627 {--views-directory= : The path where the views should be created under.}
@@ -92,17 +93,20 @@ public function handle()
9293 $ requestNameSpace = $ this ->getRequestsNamespace ($ requestName );
9394 $ this ->makeFormRequest ($ input );
9495 }
95-
96+
9697 $ fields = $ this ->getFields ($ input ->fields , $ input ->langFile , $ input ->fieldsFile );
97- $ viewVariablesForIndex = $ this ->getCompactVariablesFor ($ fields , $ this ->getModelPluralName ($ input ->modelName ), 'index ' );
98- $ viewVariablesForShow = $ this ->getCompactVariablesFor ($ fields , $ this ->getModelName ($ input ->modelName ), 'show ' );
99- $ viewVariablesForEdit = $ this ->getCompactVariablesFor ($ fields , $ this ->getModelName ($ input ->modelName ), 'form ' );
98+ $ viewVariablesForIndex = $ this ->getCompactVariablesFor ($ fields , $ this ->getPluralVariable ($ input ->modelName ), 'index ' );
99+ $ viewVariablesForShow = $ this ->getCompactVariablesFor ($ fields , $ this ->getSingularVariable ($ input ->modelName ), 'show ' );
100+ $ viewVariablesForEdit = $ this ->getCompactVariablesFor ($ fields , $ this ->getSingularVariable ($ input ->modelName ), 'form ' );
100101 $ modelFullName = $ this ->getModelFullName ($ input ->modelDirectory , $ input ->modelName );
101102 $ affirmMethod = $ this ->getAffirmMethod ($ input ->withFormRequest , $ fields , $ requestNameSpace );
102103 $ classToExtendFullname = $ this ->getFullClassToExtend ($ input ->extends );
103104 $ namespacesToUse = $ this ->getRequiredUseClasses ($ fields , [$ modelFullName , $ requestNameSpace , $ classToExtendFullname ]);
104105 $ dataMethod = $ this ->getDataMethod ($ fields , $ requestNameSpace . '\\' . $ requestName , $ input ->withFormRequest );
105106 $ stub = $ this ->getStubContent ('controller ' );
107+ $ languages = array_keys (Helpers::getLanguageItems ($ fields ));
108+ $ viewLabels = new ViewLabelsGenerator ($ input ->modelName , $ this ->isCollectiveTemplate ());
109+ $ standardLabels = $ viewLabels ->getLabels ($ languages );
106110
107111 return $ this ->replaceViewNames ($ stub , $ input ->viewDirectory , $ input ->prefix )
108112 ->replaceGetDataMethod ($ stub , $ dataMethod )
@@ -111,7 +115,7 @@ public function handle()
111115 ->replaceNamespace ($ stub , $ this ->getControllersNamespace ())
112116 ->replaceControllerExtends ($ stub , $ this ->getControllerExtends ($ classToExtendFullname ))
113117 ->replaceUseCommandPlaceholder ($ stub , $ namespacesToUse )
114- ->replaceRouteNames ($ stub , $ input ->modelName , $ input ->prefix )
118+ ->replaceRouteNames ($ stub , $ this -> getModelName ( $ input ->modelName ) , $ input ->prefix )
115119 ->replaceConstructor ($ stub , $ this ->getConstructor ($ input ->withAuth ))
116120 ->replaceCallAffirm ($ stub , $ this ->getCallAffirm ($ input ->withFormRequest ))
117121 ->replaceAffirmMethod ($ stub , $ affirmMethod )
@@ -133,6 +137,7 @@ public function handle()
133137 ->replaceRequestFullName ($ stub , $ requestNameSpace )
134138 ->replaceRequestVariable ($ stub , ' ' . $ this ->requestVariable )
135139 ->replaceTypeHintedRequestName ($ stub , $ this ->getTypeHintedRequestName ($ requestName ))
140+ ->replaceStandardLabels ($ stub , $ standardLabels )
136141 ->createFile ($ destenationFile , $ stub )
137142 ->info ('A controller was crafted successfully. ' );
138143 }
@@ -666,9 +671,11 @@ protected function isContainMultipleAnswers(array $fields)
666671 */
667672 protected function makeFormRequest ($ input )
668673 {
674+
669675 $ this ->callSilent ('create:form-request ' ,
670676 [
671- 'class-name ' => $ input ->formRequestName ,
677+ 'model-name ' => $ input ->modelName ,
678+ '--class-name ' => $ input ->formRequestName ,
672679 '--fields ' => $ input ->fields ,
673680 '--force ' => $ input ->force ,
674681 '--fields-file ' => $ input ->fieldsFile ,
@@ -700,19 +707,19 @@ protected function getModelFullName($directory, $name)
700707 */
701708 protected function getCommandInput ()
702709 {
703- $ controllerName = Helpers:: postFixWith ( trim ($ this ->argument ('controller -name ' )), ' Controller ' );
704- $ plainControllerName = str_singular (Helpers:: removePostFixWith ( $ controllerName , ' Controller ' ));
705- $ modelName = $ this -> option ( ' model-name ' ) ?: $ plainControllerName ;
710+ $ modelName = trim ($ this ->argument ('model -name ' ));
711+ $ cName = trim ( $ this -> option ( ' controller-name ' ));
712+ $ controllerName = $ cName ? str_finish ( $ cName , ' Controller ' ) : Helpers:: makeControllerName ( $ modelName ); ;
706713 $ viewDirectory = $ this ->option ('views-directory ' );
707714 $ prefix = $ this ->option ('routes-prefix ' );
708715 $ perPage = intval ($ this ->option ('models-per-page ' ));
709- $ fields = $ this ->option ('fields ' );
710- $ fieldsFile = $ this ->option ('fields-file ' );
711- $ langFile = $ this ->option ('lang-file-name ' ) ?: strtolower ( str_plural ( $ modelName) );
716+ $ fields = trim ( $ this ->option ('fields ' ) );
717+ $ fieldsFile = trim ( $ this ->option ('fields-file ' )) ?: Helpers:: makeJsonFileName ( $ modelName );
718+ $ langFile = $ this ->option ('lang-file-name ' ) ?: Helpers:: makeLocaleGroup ( $ modelName );
712719 $ withFormRequest = $ this ->option ('with-form-request ' );
713720 $ force = $ this ->option ('force ' );
714721 $ modelDirectory = $ this ->option ('model-directory ' );
715- $ formRequestName = $ plainControllerName . ' FormRequest ' ;
722+ $ formRequestName = ' ' ;
716723 $ template = $ this ->getTemplateName ();
717724 $ extends = $ this ->generatorOption ('controller-extends ' );
718725 $ withAuth = $ this ->option ('with-auth ' );
@@ -722,6 +729,26 @@ protected function getCommandInput()
722729 'controllerName ' , 'extends ' , 'withAuth ' );
723730 }
724731
732+ /**
733+ * It Replaces the templates of the givin $labels
734+ *
735+ * @param string $stub
736+ * @param array $items
737+ *
738+ * @return $this
739+ */
740+ protected function replaceStandardLabels (&$ stub , array $ items )
741+ {
742+ foreach ($ items as $ labels ) {
743+ foreach ($ labels as $ label ) {
744+ $ text = $ label ->isPlain ? sprintf ("'%s' " , $ label ->text ) : sprintf ("trans('%s') " , $ label ->localeGroup );
745+ $ stub = $ this ->strReplace ($ label ->template , $ text , $ stub );
746+ }
747+ }
748+
749+ return $ this ;
750+ }
751+
725752 /**
726753 * Replaces on_store_setter
727754 *
0 commit comments