|
4 | 4 |
|
5 | 5 | use CrestApps\CodeGenerator\HtmlGenerators\LaravelCollectiveHtml; |
6 | 6 | use CrestApps\CodeGenerator\HtmlGenerators\StandardHtml; |
| 7 | +use CrestApps\CodeGenerator\Models\Resource; |
7 | 8 | use CrestApps\CodeGenerator\Models\ViewInput; |
8 | 9 | use CrestApps\CodeGenerator\Support\Config; |
9 | 10 | use CrestApps\CodeGenerator\Support\Helpers; |
@@ -40,6 +41,26 @@ protected function getStub() |
40 | 41 | return $this->getStubContent($this->getStubName(), $this->getTemplateName()); |
41 | 42 | } |
42 | 43 |
|
| 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 | + |
43 | 64 | /** |
44 | 65 | * It gets the views destenation path |
45 | 66 | * |
@@ -82,15 +103,18 @@ protected function getCommandInput() |
82 | 103 | * It generate the view including the full path |
83 | 104 | * |
84 | 105 | * @param string $viewsDirectory |
85 | | - * @param string $action |
| 106 | + * @param string $routesPrefix |
| 107 | + * @param string $viewName |
86 | 108 | * |
87 | 109 | * @return string |
88 | 110 | */ |
89 | | - protected function getDestinationViewFullname($viewsDirectory, $routesPrefix, $action) |
| 111 | + protected function getDestinationViewFullname($viewsDirectory, $routesPrefix, $viewName = null) |
90 | 112 | { |
91 | 113 | $viewsPath = $this->getFullViewsPath($viewsDirectory, $routesPrefix); |
92 | 114 |
|
93 | | - return $this->getDestinationPath($viewsPath) . $this->getDestinationViewName($action); |
| 115 | + $filename = $this->getDestinationViewName($viewName ?: $this->getViewType()); |
| 116 | + |
| 117 | + return $this->getDestinationPath($viewsPath) . $filename; |
94 | 118 | } |
95 | 119 |
|
96 | 120 | /** |
@@ -133,24 +157,35 @@ protected function replaceCommonTemplates(&$stub, ViewInput $input, array $field |
133 | 157 | * |
134 | 158 | * @param string $file |
135 | 159 | * @param bool $force |
136 | | - * @param array $fields |
| 160 | + * @param CrestApps\CodeGenerator\Models\Resource |
137 | 161 | * |
138 | 162 | * @return bool |
139 | 163 | */ |
140 | | - protected function canCreateView($file, $force, array $fields = null) |
| 164 | + protected function canCreateView($file, $force, Resource $resource) |
141 | 165 | { |
| 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 | + |
142 | 174 | if ($this->alreadyExists($file) && !$force) { |
143 | 175 | $this->error($this->getViewNameFromFile($file) . ' view already exists.'); |
| 176 | + |
144 | 177 | return false; |
145 | 178 | } |
146 | 179 |
|
147 | | - if (!is_null($fields) && !isset($fields[0])) { |
| 180 | + if (!$resource->hasFields()) { |
148 | 181 | $this->error('You must provide at least one field to generate the views!'); |
| 182 | + |
149 | 183 | return false; |
150 | 184 | } |
151 | 185 |
|
152 | | - if (!is_null($fields) && is_null($this->getPrimaryKeyName($fields))) { |
| 186 | + if (!$resource->hasPrimaryField()) { |
153 | 187 | $this->error('None of the fields is set primary! You must assign on of the fields to be a primary field.'); |
| 188 | + |
154 | 189 | return false; |
155 | 190 | } |
156 | 191 |
|
@@ -276,13 +311,16 @@ protected function getViewCommand($view) |
276 | 311 | * It checks of a destination view exists or not |
277 | 312 | * |
278 | 313 | * @param string $viewsDirectory |
| 314 | + * @param string $routesPrefix |
279 | 315 | * @param string $viewName |
280 | 316 | * |
281 | 317 | * @return bool |
282 | 318 | */ |
283 | 319 | protected function isViewExists($viewsDirectory, $routesPrefix, $viewName) |
284 | 320 | { |
285 | | - return $this->alreadyExists($this->getDestinationViewFullname($viewsDirectory, $routesPrefix, $viewName)); |
| 321 | + $destenatioFile = $this->getDestinationViewFullname($viewsDirectory, $routesPrefix, $viewName); |
| 322 | + |
| 323 | + return $this->alreadyExists($destenatioFile); |
286 | 324 | } |
287 | 325 |
|
288 | 326 | /** |
|
0 commit comments