Skip to content

Commit afbf7e4

Browse files
committed
Adds protection capability to the resource. Store the table name in the resource file
1 parent 7956ee3 commit afbf7e4

15 files changed

+363
-121
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v2.2.7
2+
- Added capability to lock down resource from with in the resource file. This is helpful if you make code changes to a file and you want the code generator to protect the file from accidentally overriding it when using --force
3+
- When creating resources from existing database, the table name is stored in the resource-file. This step will save you from having to provide the table name via command line each time you create model.
4+
5+
6+
7+
## v2.2.0 - v2.2.6
18
### Upgrade
29
- If you are upgrading from v2.0, v2.1, v2.2, v2.3 to v2.4 or v2.5 make sure you publish the vendor resource. There are some updates to the config file.
310
- If you are upgrading from v2.0, v2.1 or v2.2 to v2.3 make sure you publish the vendor resource. There are some updates to the config file.

src/Commands/Bases/ViewsCommandBase.php

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CrestApps\CodeGenerator\HtmlGenerators\LaravelCollectiveHtml;
66
use CrestApps\CodeGenerator\HtmlGenerators\StandardHtml;
7+
use CrestApps\CodeGenerator\Models\Resource;
78
use CrestApps\CodeGenerator\Models\ViewInput;
89
use CrestApps\CodeGenerator\Support\Config;
910
use CrestApps\CodeGenerator\Support\Helpers;
@@ -40,6 +41,26 @@ protected function getStub()
4041
return $this->getStubContent($this->getStubName(), $this->getTemplateName());
4142
}
4243

44+
/**
45+
* Get the view type
46+
*
47+
* @return string
48+
*/
49+
protected function getViewType()
50+
{
51+
return Helpers::removePostFixWith($this->getStubName(), '.blade');
52+
}
53+
54+
/**
55+
* Get the view name
56+
*
57+
* @return string
58+
*/
59+
protected function getViewName()
60+
{
61+
return sprintf('%s-view', $this->getViewType());
62+
}
63+
4364
/**
4465
* It gets the views destenation path
4566
*
@@ -82,15 +103,18 @@ protected function getCommandInput()
82103
* It generate the view including the full path
83104
*
84105
* @param string $viewsDirectory
85-
* @param string $action
106+
* @param string $routesPrefix
107+
* @param string $viewName
86108
*
87109
* @return string
88110
*/
89-
protected function getDestinationViewFullname($viewsDirectory, $routesPrefix, $action)
111+
protected function getDestinationViewFullname($viewsDirectory, $routesPrefix, $viewName = null)
90112
{
91113
$viewsPath = $this->getFullViewsPath($viewsDirectory, $routesPrefix);
92114

93-
return $this->getDestinationPath($viewsPath) . $this->getDestinationViewName($action);
115+
$filename = $this->getDestinationViewName($viewName ?: $this->getViewType());
116+
117+
return $this->getDestinationPath($viewsPath) . $filename;
94118
}
95119

96120
/**
@@ -133,24 +157,35 @@ protected function replaceCommonTemplates(&$stub, ViewInput $input, array $field
133157
*
134158
* @param string $file
135159
* @param bool $force
136-
* @param array $fields
160+
* @param CrestApps\CodeGenerator\Models\Resource
137161
*
138162
* @return bool
139163
*/
140-
protected function canCreateView($file, $force, array $fields = null)
164+
protected function canCreateView($file, $force, Resource $resource)
141165
{
166+
$viewName = $this->getViewName();
167+
168+
if ($resource->isProtected($viewName)) {
169+
$this->warn('The ' . $viewName . ' is protected and cannot be regenerated. To regenerate the file, unprotect it from the resource file.');
170+
171+
return false;
172+
}
173+
142174
if ($this->alreadyExists($file) && !$force) {
143175
$this->error($this->getViewNameFromFile($file) . ' view already exists.');
176+
144177
return false;
145178
}
146179

147-
if (!is_null($fields) && !isset($fields[0])) {
180+
if (!$resource->hasFields()) {
148181
$this->error('You must provide at least one field to generate the views!');
182+
149183
return false;
150184
}
151185

152-
if (!is_null($fields) && is_null($this->getPrimaryKeyName($fields))) {
186+
if (!$resource->hasPrimaryField()) {
153187
$this->error('None of the fields is set primary! You must assign on of the fields to be a primary field.');
188+
154189
return false;
155190
}
156191

@@ -276,13 +311,16 @@ protected function getViewCommand($view)
276311
* It checks of a destination view exists or not
277312
*
278313
* @param string $viewsDirectory
314+
* @param string $routesPrefix
279315
* @param string $viewName
280316
*
281317
* @return bool
282318
*/
283319
protected function isViewExists($viewsDirectory, $routesPrefix, $viewName)
284320
{
285-
return $this->alreadyExists($this->getDestinationViewFullname($viewsDirectory, $routesPrefix, $viewName));
321+
$destenatioFile = $this->getDestinationViewFullname($viewsDirectory, $routesPrefix, $viewName);
322+
323+
return $this->alreadyExists($destenatioFile);
286324
}
287325

288326
/**

src/Commands/CreateControllerCommand.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ public function getNameInput()
8484
public function handle()
8585
{
8686
$input = $this->getCommandInput();
87-
$destenationFile = $this->getDestenationFile($input->controllerName, $input->controllerDirectory);
8887

89-
if ($this->alreadyExists($destenationFile)) {
90-
$this->error('The controller already exists!');
88+
$resources = Resource::fromFile($input->resourceFile, $input->langFile);
9189

90+
$destenationFile = $this->getDestenationFile($input->controllerName, $input->controllerDirectory);
91+
92+
if ($this->hasErrors($resources, $destenationFile)) {
9293
return false;
9394
}
9495

@@ -104,7 +105,6 @@ public function handle()
104105
}
105106
}
106107

107-
$resources = Resource::fromFile($input->resourceFile, $input->langFile);
108108
$fields = $resources->fields;
109109
$viewVariablesForIndex = $this->getCompactVariablesFor($fields, $this->getPluralVariable($input->modelName), 'index');
110110
$viewVariablesForShow = $this->getCompactVariablesFor($fields, $this->getSingularVariable($input->modelName), 'show');
@@ -153,6 +153,33 @@ public function handle()
153153
->info('A controller was crafted successfully.');
154154
}
155155

156+
/**
157+
* Build the model class with the given name.
158+
*
159+
* @param CrestApps\CodeGenerator\Models\Resource $resource
160+
* @param string $destenationFile
161+
*
162+
* @return bool
163+
*/
164+
protected function hasErrors(Resource $resource, $destenationFile)
165+
{
166+
$hasErrors = false;
167+
168+
if ($resource->isProtected('controller')) {
169+
$this->warn('The controller is protected and cannot be regenerated. To regenerate the file, unprotect it from the resource file.');
170+
171+
$hasErrors = true;
172+
}
173+
174+
if ($this->alreadyExists($destenationFile)) {
175+
$this->error('The controller already exists!');
176+
177+
$hasErrors = true;
178+
}
179+
180+
return $hasErrors;
181+
}
182+
156183
/**
157184
* Extracts a namespace from a giving string
158185
*

src/Commands/CreateCreateViewCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected function handleCreateView()
4848
{
4949
$input = $this->getCommandInput();
5050
$resources = Resource::fromFile($input->resourceFile, $input->languageFileName);
51-
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix, 'create');
51+
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix);
5252

53-
if ($this->canCreateView($destenationFile, $input->force, $resources->fields)) {
53+
if ($this->canCreateView($destenationFile, $input->force, $resources)) {
5454
$stub = $this->getStub();
5555
$headers = $this->getHeaderFieldAccessor($resources->fields, $input->modelName);
5656

src/Commands/CreateEditViewCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected function handleCreateView()
4848
{
4949
$input = $this->getCommandInput();
5050
$resources = Resource::fromFile($input->resourceFile, $input->languageFileName);
51-
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix, 'edit');
51+
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix);
5252

53-
if ($this->canCreateView($destenationFile, $input->force, $resources->fields)) {
53+
if ($this->canCreateView($destenationFile, $input->force, $resources)) {
5454
$stub = $this->getStub();
5555

5656
$this->createLanguageFile($input->languageFileName, $input->resourceFile, $input->modelName)

src/Commands/CreateFormRequestCommand.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ public function handle()
4040
{
4141
$input = $this->getCommandInput();
4242

43-
$stub = $this->getStubContent('form-request', $input->template);
4443
$resources = Resource::fromFile($input->resourceFile, 'crestapps');
45-
$destenationFile = $this->getDestenationFile($input->fileName, $input->formRequestDirectory);
46-
47-
$validations = $this->getValidationRules($resources->fields, $input->modelName, $input->formRequestDirectory);
4844

49-
if ($this->alreadyExists($destenationFile)) {
50-
$this->error('The form-request already exists! To override the existing file, use --force option.');
45+
$destenationFile = $this->getDestenationFile($input->fileName, $input->formRequestDirectory);
5146

47+
if ($this->hasErrors($resources, $destenationFile)) {
5248
return false;
5349
}
50+
51+
$stub = $this->getStubContent('form-request', $input->template);
52+
53+
$validations = $this->getValidationRules($resources->fields, $input->modelName, $input->formRequestDirectory);
54+
5455
$this->replaceFormRequestClass($stub, $input->fileName)
5556
->replaceValidationRules($stub, $validations)
5657
->replaceFileValidationSnippet($stub, $this->getFileValidationSnippet($resources->fields, $input))
@@ -65,6 +66,33 @@ public function handle()
6566
->info('A new form-request have been crafted!');
6667
}
6768

69+
/**
70+
* Build the model class with the given name.
71+
*
72+
* @param CrestApps\CodeGenerator\Models\Resource $resource
73+
* @param string $destenationFile
74+
*
75+
* @return bool
76+
*/
77+
protected function hasErrors(Resource $resource, $destenationFile)
78+
{
79+
$hasErrors = false;
80+
81+
if ($resource->isProtected('form-request')) {
82+
$this->warn('The form-request is protected and cannot be regenerated. To regenerate the file, unprotect it from the resource file.');
83+
84+
$hasErrors = true;
85+
}
86+
87+
if ($this->alreadyExists($destenationFile)) {
88+
$this->error('The form-request already exists! To override the existing file, use --force option.');
89+
90+
$hasErrors = true;
91+
}
92+
93+
return $hasErrors;
94+
95+
}
6896
/**
6997
* Gets the signature of the getData method.
7098
*

src/Commands/CreateFormViewCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected function handleCreateView()
4848
{
4949
$input = $this->getCommandInput();
5050
$resources = Resource::fromFile($input->resourceFile, $input->languageFileName);
51-
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix, 'form');
51+
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix);
5252

53-
if ($this->canCreateView($destenationFile, $input->force, $resources->fields)) {
53+
if ($this->canCreateView($destenationFile, $input->force, $resources)) {
5454
$stub = $this->getStub();
5555
$htmlCreator = $this->getHtmlGenerator($resources->fields, $input->modelName, $this->getTemplateName());
5656
$headers = $this->getHeaderFieldAccessor($resources->fields, $input->modelName);

src/Commands/CreateIndexViewCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected function handleCreateView()
4848
{
4949
$input = $this->getCommandInput();
5050
$resources = Resource::fromFile($input->resourceFile, $input->languageFileName);
51-
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix, 'index');
51+
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix);
5252

53-
if ($this->canCreateView($destenationFile, $input->force, $resources->fields)) {
53+
if ($this->canCreateView($destenationFile, $input->force, $resources)) {
5454
$stub = $this->getStub();
5555
$htmlCreator = $this->getHtmlGenerator($resources->fields, $input->modelName, $this->getTemplateName());
5656

src/Commands/CreateLanguageCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use CrestApps\CodeGenerator\Support\Helpers;
1010
use CrestApps\CodeGenerator\Support\ViewLabelsGenerator;
1111
use CrestApps\CodeGenerator\Traits\CommonCommand;
12-
use Illuminate\Console\Command;
1312
use Exception;
13+
use Illuminate\Console\Command;
1414

1515
class CreateLanguageCommand extends Command
1616
{
@@ -43,10 +43,14 @@ class CreateLanguageCommand extends Command
4343
public function handle()
4444
{
4545
$input = $this->getCommandInput();
46-
$resources = Resource::fromFile($input->resourceFile, $input->fileName);
46+
$resource = Resource::fromFile($input->resourceFile, $input->fileName);
47+
48+
if ($resource->isProtected('languages')) {
49+
return $this->warn('The language file(s) is protected and cannot be regenerated. To regenerate the file, unprotect it from the resource file.');
50+
}
4751

48-
$languages = Helpers::getLanguageItems($resources->fields);
49-
$viewLabels = new ViewLabelsGenerator($input->modelName, $resources->fields, $this->isCollectiveTemplate());
52+
$languages = Helpers::getLanguageItems($resource->fields);
53+
$viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields, $this->isCollectiveTemplate());
5054

5155
$standardLabels = $viewLabels->getTranslatedLabels(array_keys($languages));
5256

0 commit comments

Comments
 (0)