Skip to content

Commit bfcb7fb

Browse files
committed
Adds generic labels to all views.
1 parent 4c99a07 commit bfcb7fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1284
-547
lines changed

config/codegenerator.php

Lines changed: 237 additions & 176 deletions
Large diffs are not rendered by default.

src/Commands/CreateControllerCommand.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Illuminate\Console\Command;
66
use CrestApps\CodeGenerator\Traits\CommonCommand;
7-
use CrestApps\CodeGenerator\Support\Config;
87
use CrestApps\CodeGenerator\Traits\GeneratorReplacers;
9-
use CrestApps\CodeGenerator\Support\Helpers;
108
use CrestApps\CodeGenerator\Models\ForeignRelationship;
9+
use CrestApps\CodeGenerator\Support\Config;
10+
use CrestApps\CodeGenerator\Support\Helpers;
11+
use CrestApps\CodeGenerator\Support\ViewLabelsGenerator;
1112

1213
class 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
*

src/Commands/CreateCreateViewCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CreateCreateViewCommand extends ViewsCommand
1818
{--fields-file= : File name to import fields from.}
1919
{--views-directory= : The name of the directory to create the views under.}
2020
{--routes-prefix= : The routes prefix.}
21+
{--lang-file-name= : The name of the language file.}
2122
{--layout-name=layouts.app : This will extract the validation into a request form class.}
2223
{--template-name= : The template name to use when generating the code.}
2324
{--force : This option will override the view if one already exists.}';
@@ -54,9 +55,9 @@ protected function handleCreateView()
5455
$stub = $this->getStub();
5556
$headers = $this->getHeaderFieldAccessor($fields, $input->modelName);
5657

57-
$this->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile)
58+
$this->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile, $input->modelName)
5859
->createMissingViews($input)
59-
->replaceCommonTemplates($stub, $input)
60+
->replaceCommonTemplates($stub, $input, $fields)
6061
->replaceFileUpload($stub, $fields)
6162
->replaceModelHeader($stub, $headers)
6263
->createFile($destenationFile, $stub)

src/Commands/CreateEditViewCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CreateEditViewCommand extends ViewsCommand
1818
{--fields-file= : File name to import fields from.}
1919
{--views-directory= : The name of the directory to create the views under.}
2020
{--routes-prefix= : The routes prefix.}
21+
{--lang-file-name= : The name of the language file.}
2122
{--layout-name=layouts.app : This will extract the validation into a request form class.}
2223
{--template-name= : The template name to use when generating the code.}
2324
{--force : This option will override the view if one already exists.}';
@@ -52,10 +53,10 @@ protected function handleCreateView()
5253

5354
if ($this->canCreateView($destenationFile, $input->force, $fields)) {
5455
$stub = $this->getStub();
55-
56-
$this->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile)
56+
57+
$this->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile, $input->modelName)
5758
->createMissingViews($input)
58-
->replaceCommonTemplates($stub, $input)
59+
->replaceCommonTemplates($stub, $input, $fields)
5960
->replaceFileUpload($stub, $fields)
6061
->replacePrimaryKey($stub, $this->getPrimaryKeyName($fields))
6162
->replaceModelHeader($stub, $this->getHeaderFieldAccessor($fields, $input->modelName))

src/Commands/CreateFieldsFileCommand.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class CreateFieldsFileCommand extends Command
2121
* @var string
2222
*/
2323
protected $signature = 'create:fields-file
24-
{table-name : The dtabase table name to fetch the field from.}
24+
{model-name : The model name that these files represent.}
25+
{--table-name= : The database table name to fetch the field from.}
2526
{--database-name= : The database name the table is stored in.}
2627
{--fields-filename= : The destination file name to create.}
2728
{--translation-for= : A comma seperated string of languages to create fields for.}
@@ -124,7 +125,19 @@ protected function isForce()
124125
*/
125126
protected function getFilename()
126127
{
127-
return Helpers::postFixWith(trim($this->option('fields-filename')) ?: $this->getTableName(), '.json');
128+
$filename = trim($this->option('fields-filename')) ?: Helpers::makeJsonFileName($this->getModelName());
129+
130+
return str_finish($filename, '.json');
131+
}
132+
133+
/**
134+
* Gets the model name.
135+
*
136+
* @return string
137+
*/
138+
protected function getModelName()
139+
{
140+
return trim($this->argument('model-name'));
128141
}
129142

130143
/**
@@ -144,7 +157,7 @@ protected function getDatabaseName()
144157
*/
145158
protected function getTableName()
146159
{
147-
return trim($this->argument('table-name'));
160+
return trim($this->option('table-name')) ?: $this->makeTableName($this->getModelName());
148161
}
149162

150163
/**

src/Commands/CreateFormRequestCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class CreateFormRequestCommand extends Command
2020
* @var string
2121
*/
2222
protected $signature = 'create:form-request
23-
{class-name : The name of the form-request class.}
23+
{model-name : The model name.}
24+
{--class-name= : The name of the form-request class.}
2425
{--fields= : The fields to create the validation rules from.}
2526
{--fields-file= : File name to import fields from.}
2627
{--template-name= : The template name to use when generating the code.}
@@ -184,13 +185,14 @@ protected function getDestenationFile($name)
184185
*/
185186
protected function getCommandInput()
186187
{
187-
$fileName = trim($this->argument('class-name'));
188-
$fields = trim($this->option('fields'));
189-
$fieldsFile = trim($this->option('fields-file'));
188+
$modelName = trim($this->argument('model-name'));
189+
$fileName = trim($this->option('class-name')) ?: $modelName . 'FormRequest';
190+
$fields = trim($this->option('fields'));
191+
$fieldsFile = trim($this->option('fields-file')) ?: Helpers::makeJsonFileName($modelName);
190192
$force = $this->option('force');
191193
$template = $this->option('template-name');
192194

193-
return (object) compact('fileName', 'fields', 'fieldsFile', 'force', 'template');
195+
return (object) compact('modelName','fileName', 'fields', 'fieldsFile', 'force', 'template');
194196
}
195197

196198
/**

src/Commands/CreateFormViewCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CreateFormViewCommand extends ViewsCommand
1818
{--fields-file= : File name to import fields from.}
1919
{--views-directory= : The name of the directory to create the views under.}
2020
{--routes-prefix= : The routes prefix.}
21+
{--lang-file-name= : The name of the language file.}
2122
{--layout-name=layouts.app : This will extract the validation into a request form class.}
2223
{--template-name= : The template name to use when generating the code.}
2324
{--force : This option will override the view if one already exists.}';
@@ -55,8 +56,8 @@ protected function handleCreateView()
5556
$htmlCreator = $this->getHtmlGenerator($fields, $input->modelName, $this->getTemplateName());
5657
$headers = $this->getHeaderFieldAccessor($fields, $input->modelName);
5758

58-
$this->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile)
59-
->replaceCommonTemplates($stub, $input)
59+
$this->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile, $input->modelName)
60+
->replaceCommonTemplates($stub, $input, $fields)
6061
->replaceFields($stub, $htmlCreator->getHtmlFields())
6162
->replaceModelHeader($stub, $headers)
6263
->createFile($destenationFile, $stub)

src/Commands/CreateIndexViewCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CreateIndexViewCommand extends ViewsCommand
1919
{--fields-file= : File name to import fields from.}
2020
{--views-directory= : The name of the directory to create the views under.}
2121
{--routes-prefix= : The routes prefix.}
22+
{--lang-file-name= : The name of the language file.}
2223
{--layout-name=layouts.app : This will extract the validation into a request form class.}
2324
{--template-name= : The template name to use when generating the code.}
2425
{--force : This option will override the view if one already exists.}';
@@ -55,7 +56,7 @@ protected function handleCreateView()
5556
$stub = $this->getStub();
5657
$htmlCreator = $this->getHtmlGenerator($fields, $input->modelName, $this->getTemplateName());
5758

58-
$this->replaceCommonTemplates($stub, $input)
59+
$this->replaceCommonTemplates($stub, $input, $fields)
5960
->replacePrimaryKey($stub, $this->getPrimaryKeyName($fields))
6061
->replaceHeaderCells($stub, $htmlCreator->getIndexHeaderCells())
6162
->replaceBodyCells($stub, $htmlCreator->getIndexBodyCells())

0 commit comments

Comments
 (0)