1 | <?php |
||||
2 | |||||
3 | namespace BukanKalengKaleng\LaravelEntity\Console\Commands; |
||||
4 | |||||
5 | use Illuminate\Console\Command; |
||||
6 | use Illuminate\Filesystem\Filesystem; |
||||
7 | use Illuminate\Support\Str; |
||||
8 | |||||
9 | class EntityMake extends Command |
||||
10 | { |
||||
11 | protected $data; |
||||
12 | protected $entity; |
||||
13 | protected $namespace; |
||||
14 | protected $modelName; |
||||
15 | protected $modelFullPath; |
||||
16 | protected $modelNamespace; |
||||
17 | protected $controllerName; |
||||
18 | protected $pluralizedEntity; |
||||
19 | protected $requestStoreName; |
||||
20 | protected $requestUpdateName; |
||||
21 | protected $additionalSteps = []; |
||||
22 | protected $tableContents = []; |
||||
23 | protected $tableHeaders = ['Artefact', 'Filename and/or Location']; |
||||
24 | |||||
25 | /** |
||||
26 | * The name and signature of the console command. |
||||
27 | * |
||||
28 | * @var string |
||||
29 | */ |
||||
30 | protected $signature = 'entity:make |
||||
31 | {name : Name of the entity, eg: Post, Product, Employee} |
||||
32 | {--namespace=Both : eg: Both | None | [your-choice]} |
||||
33 | {--a|all=true : Generate an entity\'s with its all artefacts} |
||||
34 | {--c|controller : Generate an entity\'s Controller} |
||||
35 | {--d|dummy : Generate an entity\'s Dummy Seeder} |
||||
36 | {--f|factory : Generate an entity\'s Factory} |
||||
37 | {--m|migration : Generate an entity\'s Migration} |
||||
38 | {--p|policy : Generate an entity\'s Policy} |
||||
39 | {--r|request : Generate an entity\'s Request} |
||||
40 | {--s|seeder : Generate an entity\'s Table Seeder} |
||||
41 | {--t|test : Generate an entity\'s Unit and Feature Test} '; |
||||
42 | |||||
43 | /** |
||||
44 | * The console command description. |
||||
45 | * |
||||
46 | * @var string |
||||
47 | */ |
||||
48 | protected $description = 'Generate an entity along with their artefacts'; |
||||
49 | |||||
50 | /** |
||||
51 | * Create a new command instance. |
||||
52 | * |
||||
53 | * @return void |
||||
54 | */ |
||||
55 | public function __construct(Filesystem $files) |
||||
56 | { |
||||
57 | parent::__construct(); |
||||
58 | |||||
59 | $this->files = $files; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * Execute the console command. |
||||
64 | * |
||||
65 | * @return mixed |
||||
66 | */ |
||||
67 | public function handle() |
||||
68 | { |
||||
69 | $this->line(''); |
||||
70 | |||||
71 | if ($this->option('all')) { |
||||
72 | $this->input->setOption('controller', true); |
||||
73 | $this->input->setOption('dummy', true); |
||||
74 | $this->input->setOption('factory', true); |
||||
75 | $this->input->setOption('migration', true); |
||||
76 | $this->input->setOption('policy', true); |
||||
77 | $this->input->setOption('request', true); |
||||
78 | $this->input->setOption('seeder', true); |
||||
79 | $this->input->setOption('test', true); |
||||
80 | } |
||||
81 | |||||
82 | $this->comment('[START] Creating new entity.....'); |
||||
83 | |||||
84 | $this->entity = $this->setEntityName($this->argument('name')); |
||||
0 ignored issues
–
show
Are you sure the assignment to
$this->entity is correct as $this->setEntityName($this->argument('name')) targeting BukanKalengKaleng\Larave...tyMake::setEntityName() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() It seems like
$this->argument('name') can also be of type string[] ; however, parameter $name of BukanKalengKaleng\Larave...tyMake::setEntityName() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
85 | $this->namespace = $this->option('namespace'); |
||||
86 | |||||
87 | switch ($this->checkExistingModel()) { |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->checkExistingModel() targeting BukanKalengKaleng\Larave...e::checkExistingModel() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
88 | case 'Abort': |
||||
89 | $this->line('Aborted.'); |
||||
90 | $this->info(''); |
||||
91 | |||||
92 | break; |
||||
93 | |||||
94 | case 'Use existing model': |
||||
95 | $this->line('Model already exists: '.$this->modelNamespace.'/'.$this->modelName); |
||||
96 | |||||
97 | $this->makeMigration(); |
||||
98 | $this->makeRequest(); |
||||
99 | $this->makeController(); |
||||
100 | $this->makeFactory(); |
||||
101 | $this->makePolicy(); |
||||
102 | $this->makeSeeder(); |
||||
103 | $this->makeTest(); |
||||
104 | |||||
105 | $this->printReport(); |
||||
106 | $this->printAdditionalSteps(); |
||||
107 | |||||
108 | break; |
||||
109 | |||||
110 | case 'Overwrite existing model': |
||||
111 | |||||
112 | case 'No model': |
||||
113 | default: |
||||
114 | $this->makeModel(); |
||||
115 | $this->makeMigration(); |
||||
116 | $this->makeRequest(); |
||||
117 | $this->makeController(); |
||||
118 | $this->makeFactory(); |
||||
119 | $this->makePolicy(); |
||||
120 | $this->makeSeeder(); |
||||
121 | $this->makeTest(); |
||||
122 | |||||
123 | $this->printReport(); |
||||
124 | $this->printAdditionalSteps(); |
||||
125 | |||||
126 | break; |
||||
127 | } |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * Set a proper entity name. |
||||
132 | * |
||||
133 | * @param string $name |
||||
134 | * @return void |
||||
135 | */ |
||||
136 | protected function setEntityName($name) |
||||
0 ignored issues
–
show
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
137 | { |
||||
138 | if ($this->argument('name') == strtolower($this->argument('name'))) { |
||||
0 ignored issues
–
show
It seems like
$this->argument('name') can also be of type string[] ; however, parameter $str of strtolower() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
139 | return ucfirst($this->argument('name')); |
||||
0 ignored issues
–
show
It seems like
$this->argument('name') can also be of type string[] ; however, parameter $str of ucfirst() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
140 | } |
||||
141 | |||||
142 | return $this->argument('name'); |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * Check for existing model. |
||||
147 | * |
||||
148 | * @return void |
||||
149 | */ |
||||
150 | protected function checkExistingModel() |
||||
151 | { |
||||
152 | $modelChoice = ''; |
||||
153 | $this->modelName = $this->entity.'.php'; |
||||
154 | $this->modelNamespace = 'App/'.config('entity.model.namespace'); |
||||
155 | $this->modelFullPath = app_path(config('entity.model.namespace').'/'.$this->modelName); |
||||
156 | |||||
157 | if ($this->files->exists($this->modelFullPath)) { |
||||
158 | $this->error('Model already exists: '.$this->modelNamespace.'/'.$this->modelName); |
||||
159 | |||||
160 | $modelChoice = $this->choice('What should we do?', [ |
||||
161 | 'Overwrite existing model', |
||||
162 | 'Use existing model', |
||||
163 | 'Abort', |
||||
164 | ]); |
||||
165 | } else { |
||||
166 | $this->makeDirectory($this->modelFullPath); |
||||
167 | |||||
168 | $modelChoice = 'No model'; |
||||
169 | |||||
170 | return $modelChoice; |
||||
171 | } |
||||
172 | |||||
173 | return $modelChoice; |
||||
174 | } |
||||
175 | |||||
176 | /** |
||||
177 | * Run make:model command with defined entity name and/or namespace. |
||||
178 | * |
||||
179 | * @return void |
||||
180 | */ |
||||
181 | protected function makeModel() |
||||
182 | { |
||||
183 | if (config('entity.model.should_use_default_base') === true) { |
||||
184 | $this->compileModelStub($this->modelFullPath); |
||||
185 | } else { |
||||
186 | $this->makeCustomBaseModel(); |
||||
187 | |||||
188 | $this->compileCustomModelStub($this->modelFullPath); |
||||
189 | } |
||||
190 | |||||
191 | $this->addToTable('Model', $this->modelNamespace.'/'.$this->modelName); |
||||
192 | |||||
193 | $this->info($this->data['artefact'].' created.'); |
||||
194 | } |
||||
195 | |||||
196 | /** |
||||
197 | * Make custom base model. |
||||
198 | * |
||||
199 | * @return void |
||||
200 | */ |
||||
201 | protected function makeCustomBaseModel() |
||||
202 | { |
||||
203 | $path = app_path(config('entity.model.custom_base_directory')); |
||||
204 | $file = $path.'/'.config('entity.model.custom_base_name').'.php'; |
||||
205 | |||||
206 | if (! $this->files->exists($file)) { |
||||
207 | $stub = $this->files->get(__DIR__.'/stubs/model.base.custom.stub'); |
||||
208 | |||||
209 | $classNamepace = 'App'; |
||||
210 | |||||
211 | if (! empty(config('entity.model.custom_base_directory'))) { |
||||
212 | $classNamepace .= '\\'.config('entity.model.custom_base_directory'); |
||||
213 | } |
||||
214 | |||||
215 | $stub = str_replace('{{classNamespace}}', $classNamepace, $stub); |
||||
216 | $stub = str_replace('{{className}}', config('entity.model.custom_base_name'), $stub); |
||||
217 | |||||
218 | $this->makeDirectory($file); |
||||
219 | |||||
220 | $this->files->put($file, $stub); |
||||
221 | } |
||||
222 | } |
||||
223 | |||||
224 | /** |
||||
225 | * Compile the Model stub. |
||||
226 | * |
||||
227 | * @param string $path |
||||
228 | * @return void |
||||
229 | */ |
||||
230 | protected function compileModelStub($path) |
||||
231 | { |
||||
232 | $stub = $this->files->get(__DIR__.'/stubs/model.stub'); |
||||
233 | |||||
234 | $stub = str_replace('{{className}}', $this->entity, $stub); |
||||
235 | $stub = str_replace('{{classNamespace}}', config('entity.model.namespace'), $stub); |
||||
236 | |||||
237 | $this->files->put($path, $stub); |
||||
238 | |||||
239 | return $this; |
||||
240 | } |
||||
241 | |||||
242 | /** |
||||
243 | * Compile the Custom Model stub. |
||||
244 | * |
||||
245 | * @param string $path |
||||
246 | * @return void |
||||
247 | */ |
||||
248 | protected function compileCustomModelStub($path) |
||||
249 | { |
||||
250 | $stub = $this->files->get(__DIR__.'/stubs/model.custom.stub'); |
||||
251 | |||||
252 | $customBaseModelNamepace = ''; |
||||
253 | |||||
254 | if (! empty(config('entity.model.custom_base_directory'))) { |
||||
255 | $customBaseModelNamepace = config('entity.model.custom_base_directory').'\\'; |
||||
256 | } |
||||
257 | |||||
258 | $customBaseModelNamepace .= config('entity.model.custom_base_name'); |
||||
259 | |||||
260 | $stub = str_replace('{{classNamespace}}', config('entity.model.namespace'), $stub); |
||||
261 | |||||
262 | $stub = str_replace('{{customBaseModelNamespace}}', $customBaseModelNamepace, $stub); |
||||
263 | |||||
264 | $stub = str_replace('{{className}}', $this->entity, $stub); |
||||
265 | $stub = str_replace('{{customBaseName}}', config('entity.model.custom_base_name'), $stub); |
||||
266 | |||||
267 | $this->files->put($path, $stub); |
||||
268 | |||||
269 | return $this; |
||||
270 | } |
||||
271 | |||||
272 | /** |
||||
273 | * Run make:migration command with defined entity name and/or namespace. |
||||
274 | * |
||||
275 | * @return void |
||||
276 | */ |
||||
277 | protected function makeMigration() |
||||
278 | { |
||||
279 | if ($this->option('migration')) { |
||||
280 | $this->pluralizedEntity = Str::plural($this->entity); |
||||
281 | $migration = 'create_'.Str::snake($this->pluralizedEntity).'_table'; |
||||
282 | |||||
283 | $this->callSilent('make:migration', [ |
||||
284 | 'name' => $migration, |
||||
285 | '--create' => Str::snake($this->pluralizedEntity), |
||||
286 | ]); |
||||
287 | |||||
288 | $this->addToTable('Migration', $migration.'.php'); |
||||
289 | |||||
290 | $this->info($this->data['artefact'].' created.'); |
||||
291 | } |
||||
292 | } |
||||
293 | |||||
294 | /** |
||||
295 | * Run make:request command with defined entity name and/or namespace. |
||||
296 | * |
||||
297 | * @return void |
||||
298 | */ |
||||
299 | protected function makeRequest() |
||||
300 | { |
||||
301 | if ($this->option('request')) { |
||||
302 | $this->requestStoreName = $this->entity.'Store.php'; |
||||
303 | $this->requestUpdateName = $this->entity.'Update.php'; |
||||
304 | |||||
305 | $formRequestPath = app_path('Http/Requests'); |
||||
306 | $backendPath = $formRequestPath.'/'.config('entity.namespace.backend'); |
||||
307 | $frontendPath = $formRequestPath.'/'.config('entity.namespace.frontend'); |
||||
308 | |||||
309 | $fileExistsMessage = 'Form Request already exists:'; |
||||
310 | |||||
311 | switch (strtolower($this->namespace)) { |
||||
312 | case 'both': |
||||
313 | /** Store Request on Backend namespace */ |
||||
314 | if ($this->files->exists( |
||||
315 | $path = $backendPath.'/'.$this->requestStoreName |
||||
316 | )) { |
||||
317 | $this->input->setOption('request', false); |
||||
318 | |||||
319 | $this->line($fileExistsMessage.' '.config('entity.namespace.backend').'/'.$this->requestStoreName); |
||||
320 | } else { |
||||
321 | $this->callSilent('make:request', [ |
||||
322 | 'name' => config('entity.namespace.backend').'/'.$this->removeFileExtension($this->requestStoreName), |
||||
323 | ]); |
||||
324 | |||||
325 | $this->addToTable('Form Request', config('entity.namespace.backend').'/'.$this->requestStoreName); |
||||
326 | } |
||||
327 | |||||
328 | /** Update Request on Backend namespace */ |
||||
329 | if ($this->files->exists( |
||||
330 | $path = $backendPath.'/'.$this->requestUpdateName |
||||
331 | )) { |
||||
332 | $this->input->setOption('request', false); |
||||
333 | |||||
334 | $this->line($fileExistsMessage.' '.config('entity.namespace.backend').'/'.$this->requestUpdateName); |
||||
335 | } else { |
||||
336 | $this->callSilent('make:request', [ |
||||
337 | 'name' => config('entity.namespace.backend').'/'.$this->removeFileExtension($this->requestUpdateName), |
||||
338 | ]); |
||||
339 | |||||
340 | $this->addToTable('Form Request', config('entity.namespace.backend').'/'.$this->requestUpdateName); |
||||
341 | } |
||||
342 | |||||
343 | /** Store Request on Frontend namespace */ |
||||
344 | if ($this->files->exists( |
||||
345 | $path = $frontendPath.'/'.$this->requestStoreName |
||||
346 | )) { |
||||
347 | $this->input->setOption('request', false); |
||||
348 | |||||
349 | $this->line($fileExistsMessage.' '.config('entity.namespace.frontend').'/'.$this->requestStoreName); |
||||
350 | } else { |
||||
351 | $this->callSilent('make:request', [ |
||||
352 | 'name' => config('entity.namespace.frontend').'/'.$this->removeFileExtension($this->requestStoreName), |
||||
353 | ]); |
||||
354 | |||||
355 | $this->addToTable('Form Request', config('entity.namespace.frontend').'/'.$this->requestStoreName); |
||||
356 | } |
||||
357 | |||||
358 | /** Update Request on Frontend namespace */ |
||||
359 | if ($this->files->exists( |
||||
360 | $path = $frontendPath.'/'.$this->requestUpdateName |
||||
361 | )) { |
||||
362 | $this->input->setOption('request', false); |
||||
363 | |||||
364 | $this->line($fileExistsMessage.' '.config('entity.namespace.frontend').'/'.$this->requestUpdateName); |
||||
365 | } else { |
||||
366 | $this->callSilent('make:request', [ |
||||
367 | 'name' => config('entity.namespace.frontend').'/'.$this->removeFileExtension($this->requestUpdateName), |
||||
368 | ]); |
||||
369 | |||||
370 | $this->addToTable('Form Request', config('entity.namespace.frontend').'/'.$this->requestUpdateName); |
||||
371 | } |
||||
372 | |||||
373 | break; |
||||
374 | |||||
375 | case 'none': |
||||
376 | /** Store Request */ |
||||
377 | if ($this->files->exists( |
||||
378 | $path = $formRequestPath.'/'.$this->requestStoreName |
||||
379 | )) { |
||||
380 | $this->input->setOption('request', false); |
||||
381 | |||||
382 | $this->line($fileExistsMessage.' '.$this->requestStoreName); |
||||
383 | } else { |
||||
384 | $this->callSilent('make:request', [ |
||||
385 | 'name' => $this->removeFileExtension($this->requestStoreName), |
||||
386 | ]); |
||||
387 | |||||
388 | $this->addToTable('Form Request', $this->requestStoreName); |
||||
389 | } |
||||
390 | |||||
391 | /** Update Request */ |
||||
392 | if ($this->files->exists( |
||||
393 | $path = $formRequestPath.'/'.$this->requestUpdateName |
||||
394 | )) { |
||||
395 | $this->input->setOption('request', false); |
||||
396 | |||||
397 | $this->line($fileExistsMessage.' '.$this->requestUpdateName); |
||||
398 | } else { |
||||
399 | $this->callSilent('make:request', [ |
||||
400 | 'name' => $this->removeFileExtension($this->requestUpdateName), |
||||
401 | ]); |
||||
402 | |||||
403 | $this->addToTable('Form Request', $this->requestUpdateName); |
||||
404 | } |
||||
405 | |||||
406 | break; |
||||
407 | |||||
408 | default: |
||||
409 | /** Store Request */ |
||||
410 | if ($this->files->exists( |
||||
411 | $path = $formRequestPath.'/'.$this->namespace.'/'.$this->requestStoreName |
||||
412 | )) { |
||||
413 | $this->input->setOption('request', false); |
||||
414 | |||||
415 | $this->line($fileExistsMessage.' '.$this->namespace.'/'.$this->requestStoreName); |
||||
416 | } else { |
||||
417 | $this->callSilent('make:request', [ |
||||
418 | 'name' => $this->namespace.'/'.$this->removeFileExtension($this->requestStoreName), |
||||
419 | ]); |
||||
420 | |||||
421 | $this->addToTable('Form Request', $this->namespace.'/'.$this->requestStoreName); |
||||
422 | } |
||||
423 | |||||
424 | /** Frontend Request */ |
||||
425 | if ($this->files->exists( |
||||
426 | $path = $formRequestPath.'/'.$this->namespace.'/'.$this->requestUpdateName |
||||
427 | )) { |
||||
428 | $this->input->setOption('request', false); |
||||
429 | |||||
430 | $this->line($fileExistsMessage.' '.$this->namespace.'/'.$this->requestUpdateName); |
||||
431 | } else { |
||||
432 | $this->callSilent('make:request', [ |
||||
433 | 'name' => $this->namespace.'/'.$this->removeFileExtension($this->requestUpdateName), |
||||
434 | ]); |
||||
435 | |||||
436 | $this->addToTable('Form Request', $this->namespace.'/'.$this->requestUpdateName); |
||||
437 | } |
||||
438 | |||||
439 | break; |
||||
440 | } |
||||
441 | |||||
442 | if ($this->option('request')) { |
||||
443 | $this->info($this->data['artefact'].' created.'); |
||||
444 | } |
||||
445 | } |
||||
446 | } |
||||
447 | |||||
448 | /** |
||||
449 | * Run make:controller command with defined entity name and/or namespace. |
||||
450 | * |
||||
451 | * @return void |
||||
452 | */ |
||||
453 | protected function makeController() |
||||
454 | { |
||||
455 | if ($this->option('controller')) { |
||||
456 | $this->controllerName = $this->entity.'Controller.php'; |
||||
457 | |||||
458 | $controllerPath = app_path('Http/Controllers'); |
||||
459 | $backendPath = $controllerPath.'/'.config('entity.namespace.backend'); |
||||
460 | $frontendPath = $controllerPath.'/'.config('entity.namespace.frontend'); |
||||
461 | |||||
462 | $fileExistsMessage = 'Controller already exists:'; |
||||
463 | |||||
464 | switch (strtolower($this->namespace)) { |
||||
465 | case 'both': |
||||
466 | /** Backend's Controller */ |
||||
467 | if ($this->files->exists( |
||||
468 | $path = $backendPath.'/'.$this->controllerName |
||||
469 | )) { |
||||
470 | $this->input->setOption('controller', false); |
||||
471 | |||||
472 | $this->line($fileExistsMessage.' '.config('entity.namespace.backend').'/'.$this->controllerName); |
||||
473 | } else { |
||||
474 | $this->compileControllerStub($path, config('entity.namespace.backend')); |
||||
475 | |||||
476 | $this->addToTable('Controller', config('entity.namespace.backend').'/'.$this->controllerName); |
||||
477 | } |
||||
478 | |||||
479 | /** Frontend's Controller */ |
||||
480 | if ($this->files->exists( |
||||
481 | $path = $frontendPath.'/'.$this->controllerName |
||||
482 | )) { |
||||
483 | $this->input->setOption('controller', false); |
||||
484 | |||||
485 | $this->line($fileExistsMessage.' '.config('entity.namespace.frontend').'/'.$this->controllerName); |
||||
486 | } else { |
||||
487 | $this->compileControllerStub($path, config('entity.namespace.frontend')); |
||||
488 | |||||
489 | $this->addToTable('Controller', config('entity.namespace.frontend').'/'.$this->controllerName); |
||||
490 | } |
||||
491 | |||||
492 | break; |
||||
493 | |||||
494 | case 'none': |
||||
495 | if ($this->files->exists( |
||||
496 | $path = $controllerPath.'/'.$this->controllerName |
||||
497 | )) { |
||||
498 | $this->input->setOption('controller', false); |
||||
499 | |||||
500 | $this->line($fileExistsMessage.' '.$this->controllerName); |
||||
501 | } else { |
||||
502 | $this->compileControllerStub($path); |
||||
503 | |||||
504 | $this->addToTable('Controller', $this->controllerName); |
||||
505 | } |
||||
506 | |||||
507 | break; |
||||
508 | |||||
509 | default: |
||||
510 | if ($this->files->exists( |
||||
511 | $path = $controllerPath.'/'.$this->namespace.'/'.$this->controllerName |
||||
512 | )) { |
||||
513 | $this->input->setOption('controller', false); |
||||
514 | |||||
515 | $this->line($fileExistsMessage.' '.$this->namespace.'/'.$this->controllerName); |
||||
516 | } else { |
||||
517 | $this->compileControllerStub($path, $this->namespace); |
||||
518 | |||||
519 | $this->addToTable('Controller', $this->namespace.'/'.$this->controllerName); |
||||
520 | } |
||||
521 | |||||
522 | break; |
||||
523 | } |
||||
524 | |||||
525 | if ($this->option('controller')) { |
||||
526 | $this->info($this->data['artefact'].' created.'); |
||||
527 | } |
||||
528 | } |
||||
529 | } |
||||
530 | |||||
531 | /** |
||||
532 | * Compile the Controller stub. |
||||
533 | * |
||||
534 | * @param string $path |
||||
535 | * @return void |
||||
536 | */ |
||||
537 | protected function compileControllerStub($path, $namespace = '') |
||||
538 | { |
||||
539 | if ($namespace) { |
||||
540 | $stub = $this->files->get(__DIR__.'/stubs/controller.stub'); |
||||
541 | } else { |
||||
542 | $stub = $this->files->get(__DIR__.'/stubs/controller.none.stub'); |
||||
543 | } |
||||
544 | |||||
545 | $stub = str_replace('{{classNamespace}}', $namespace, $stub); |
||||
546 | |||||
547 | $stub = str_replace('{{modelNamespace}}', config('entity.model.namespace'), $stub); |
||||
548 | $stub = str_replace('{{modelName}}', $this->removeFileExtension($this->modelName), $stub); |
||||
549 | |||||
550 | $stub = str_replace('{{className}}', $this->removeFileExtension($this->controllerName), $stub); |
||||
551 | |||||
552 | $stub = str_replace('{{requestStoreNamespace}}', $namespace, $stub); |
||||
553 | $stub = str_replace('{{requestStoreName}}', $this->removeFileExtension($this->requestStoreName), $stub); |
||||
554 | |||||
555 | $stub = str_replace('{{requestUpdateNamespace}}', $namespace, $stub); |
||||
556 | $stub = str_replace('{{requestUpdateName}}', $this->removeFileExtension($this->requestUpdateName), $stub); |
||||
557 | |||||
558 | $stub = str_replace('{{modelNameVariabel}}', Str::camel($this->entity), $stub); |
||||
559 | |||||
560 | $this->makeDirectory($path); |
||||
561 | |||||
562 | $this->files->put($path, $stub); |
||||
563 | |||||
564 | return $this; |
||||
565 | } |
||||
566 | |||||
567 | /** |
||||
568 | * Run make:factory command with defined entity name and/or namespace. |
||||
569 | * |
||||
570 | * @return void |
||||
571 | */ |
||||
572 | protected function makeFactory() |
||||
573 | { |
||||
574 | if ($this->option('factory')) { |
||||
575 | $factory = $this->entity.'Factory'; |
||||
576 | |||||
577 | if ($this->files->exists( |
||||
578 | $path = database_path('factories/'.$factory.'.php')) |
||||
579 | ) { |
||||
580 | $this->input->setOption('factory', false); |
||||
581 | |||||
582 | return $this->line('Factory already exists: '.$factory.'.php'); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->line('Factory alr... ' . $factory . '.php') targeting Illuminate\Console\Command::line() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
583 | } |
||||
584 | |||||
585 | $this->compileFactoryStub($path); |
||||
586 | |||||
587 | $this->addToTable('Factory', $factory.'.php'); |
||||
588 | |||||
589 | $this->info($this->data['artefact'].' created.'); |
||||
590 | } |
||||
591 | } |
||||
592 | |||||
593 | /** |
||||
594 | * Compile the Factory stub. |
||||
595 | * |
||||
596 | * @param string $path |
||||
597 | * @return void |
||||
598 | */ |
||||
599 | protected function compileFactoryStub($path) |
||||
600 | { |
||||
601 | $stub = $this->files->get(__DIR__.'/stubs/model.factory.stub'); |
||||
602 | |||||
603 | $stub = str_replace('{{modelNamespace}}', config('entity.model.namespace'), $stub); |
||||
604 | $stub = str_replace('{{modelName}}', $this->removeFileExtension($this->modelName), $stub); |
||||
605 | |||||
606 | $this->files->put($path, $stub); |
||||
607 | |||||
608 | return $this; |
||||
609 | } |
||||
610 | |||||
611 | /** |
||||
612 | * Run make:policy command with defined entity name and/or namespace. |
||||
613 | * |
||||
614 | * @return void |
||||
615 | */ |
||||
616 | protected function makePolicy() |
||||
617 | { |
||||
618 | if ($this->option('policy')) { |
||||
619 | $policy = $this->entity.'Policy'; |
||||
620 | |||||
621 | if ($this->files->exists( |
||||
622 | $path = app_path('Policies/'.$policy.'.php')) |
||||
623 | ) { |
||||
624 | $this->input->setOption('policy', false); |
||||
625 | |||||
626 | return $this->line('Policy already exists: '.$policy.'.php'); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->line('Policy alre...: ' . $policy . '.php') targeting Illuminate\Console\Command::line() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
627 | } |
||||
628 | |||||
629 | $this->callSilent('make:policy', [ |
||||
630 | 'name' => $policy, |
||||
631 | '--model' => config('entity.model.namespace').'/'.$this->entity, |
||||
632 | ]); |
||||
633 | |||||
634 | $this->addToTable('Policy', $policy.'.php'); |
||||
635 | |||||
636 | $this->info($this->data['artefact'].' created.'); |
||||
637 | |||||
638 | array_push($this->additionalSteps, 'Register the Policy'); |
||||
639 | } |
||||
640 | } |
||||
641 | |||||
642 | /** |
||||
643 | * Run make:seed command with defined entity name and/or namespace. |
||||
644 | * |
||||
645 | * @return void |
||||
646 | */ |
||||
647 | protected function makeSeeder() |
||||
648 | { |
||||
649 | /** Table seeder */ |
||||
650 | if ($this->option('seeder')) { |
||||
651 | $seederTable = ($this->pluralizedEntity).'TableSeeder.php'; |
||||
652 | |||||
653 | if ($this->files->exists( |
||||
654 | $path = database_path('seeds/'.$seederTable)) |
||||
655 | ) { |
||||
656 | $this->input->setOption('seeder', false); |
||||
657 | |||||
658 | $this->line('Table Seeder already exists: '.$seederTable); |
||||
659 | } else { |
||||
660 | $this->callSilent('make:seeder', [ |
||||
661 | 'name' => $this->removeFileExtension($seederTable), |
||||
662 | ]); |
||||
663 | |||||
664 | $this->addToTable('Table Seeder', $seederTable); |
||||
665 | |||||
666 | $this->info($this->data['artefact'].' created.'); |
||||
667 | |||||
668 | array_push($this->additionalSteps, 'Call the Table Seeder in DatabaseSeeder'); |
||||
669 | } |
||||
670 | } |
||||
671 | |||||
672 | /** Dummy data seeder */ |
||||
673 | if (config('entity.dummy.should_create') === true) { |
||||
674 | if ($this->option('dummy')) { |
||||
675 | $seederDummy = $this->pluralizedEntity.'.php'; |
||||
676 | |||||
677 | $path = database_path('seeds/'.config('entity.dummy.directory').'/'.$seederDummy); |
||||
678 | |||||
679 | if ($this->files->exists($path)) { |
||||
680 | $this->input->setOption('dummy', false); |
||||
681 | |||||
682 | $this->line('Dummy Seeder already exists: '.config('entity.dummy.dummies').$seederDummy); |
||||
683 | } else { |
||||
684 | $this->compileDummySeederStub($path); |
||||
685 | |||||
686 | $this->makeDummyDataSeeder(); |
||||
687 | |||||
688 | $this->addToTable('Dummy Seeder', config('entity.dummy.dummies').$seederDummy); |
||||
689 | |||||
690 | $this->info($this->data['artefact'].' created.'); |
||||
691 | |||||
692 | array_push($this->additionalSteps, 'Call the Dummy seeder in DummyDataSeeder'); |
||||
693 | } |
||||
694 | } |
||||
695 | } |
||||
696 | } |
||||
697 | |||||
698 | /** |
||||
699 | * Compile the dummy data Seeder stub. |
||||
700 | * |
||||
701 | * @param string $path |
||||
702 | * @return void |
||||
703 | */ |
||||
704 | protected function compileDummySeederStub($path) |
||||
705 | { |
||||
706 | $stub = $this->files->get(__DIR__.'/stubs/seeder.dummy.stub'); |
||||
707 | |||||
708 | $stub = str_replace('{{modelNamespace}}', config('entity.model.namespace'), $stub); |
||||
709 | $stub = str_replace('{{modelName}}', $this->removeFileExtension($this->modelName), $stub); |
||||
710 | |||||
711 | $stub = str_replace('{{className}}', $this->pluralizedEntity, $stub); |
||||
712 | |||||
713 | $this->makeDirectory($path); |
||||
714 | |||||
715 | $this->files->put($path, $stub); |
||||
716 | |||||
717 | return $this; |
||||
718 | } |
||||
719 | |||||
720 | /** |
||||
721 | * Make DummyDataSeeder, if not exists. |
||||
722 | * |
||||
723 | * @return void |
||||
724 | */ |
||||
725 | protected function makeDummyDataSeeder() |
||||
726 | { |
||||
727 | if (! $this->files->exists( |
||||
728 | $path = database_path('seeds/DummyDataSeeder.php') |
||||
729 | )) { |
||||
730 | $stub = $this->files->get(__DIR__.'/stubs/dataseeder.dummy.stub'); |
||||
731 | |||||
732 | $this->files->put($path, $stub); |
||||
733 | } |
||||
734 | } |
||||
735 | |||||
736 | /** |
||||
737 | * Run make:test command with defined entity name and/or namespace. |
||||
738 | * |
||||
739 | * @return void |
||||
740 | */ |
||||
741 | protected function makeTest() |
||||
742 | { |
||||
743 | if ($this->option('test')) { |
||||
744 | |||||
745 | /** Feature test */ |
||||
746 | $test = $this->entity.'Test'; |
||||
747 | |||||
748 | if ($this->files->exists( |
||||
749 | $path = base_path().'/tests/Feature/'.$test.'.php') |
||||
750 | ) { |
||||
751 | $this->input->setOption('test', false); |
||||
752 | |||||
753 | $this->line('Test: Feature already exists: Feature/'.$test.'.php'); |
||||
754 | } else { |
||||
755 | $this->callSilent('make:test', [ |
||||
756 | 'name' => $test, |
||||
757 | ]); |
||||
758 | |||||
759 | $this->addToTable('Test: Feature', 'Feature/'.$test.'.php'); |
||||
760 | |||||
761 | $this->info($this->data['artefact'].' created.'); |
||||
762 | } |
||||
763 | |||||
764 | /** Unit test */ |
||||
765 | if ($this->files->exists( |
||||
766 | $path = base_path().'/tests/Unit/'.$test.'.php') |
||||
767 | ) { |
||||
768 | $this->input->setOption('test', false); |
||||
769 | |||||
770 | $this->line('Test: Unit already exists: Unit/'.$test.'.php'); |
||||
771 | } else { |
||||
772 | $this->callSilent('make:test', [ |
||||
773 | 'name' => $test, |
||||
774 | '--unit' => true, |
||||
775 | ]); |
||||
776 | |||||
777 | $this->addToTable('Test: Unit', 'Unit/'.$test.'.php'); |
||||
778 | |||||
779 | $this->info($this->data['artefact'].' created.'); |
||||
780 | } |
||||
781 | } |
||||
782 | } |
||||
783 | |||||
784 | /** |
||||
785 | * Build the directory for the class if necessary. |
||||
786 | * |
||||
787 | * @param string $path |
||||
788 | * @return string |
||||
789 | */ |
||||
790 | protected function makeDirectory($path) |
||||
791 | { |
||||
792 | if (! $this->files->isDirectory(dirname($path))) { |
||||
793 | $this->files->makeDirectory(dirname($path), 0777, true, true); |
||||
794 | } |
||||
795 | } |
||||
796 | |||||
797 | /** |
||||
798 | * Print the whole command result / report. |
||||
799 | * |
||||
800 | * @return void |
||||
801 | */ |
||||
802 | protected function printReport() |
||||
803 | { |
||||
804 | if ($this->option('controller') || |
||||
805 | ($this->option('controller') && $this->option('request'))) { |
||||
806 | array_push($this->additionalSteps, 'Define a route for the generated Controller'); |
||||
807 | } elseif ($this->option('request')) { |
||||
808 | array_push($this->additionalSteps, 'Use generated Requests for the Controller'); |
||||
809 | } |
||||
810 | |||||
811 | if (in_array(true, $this->options(), true) === false) { |
||||
812 | $this->error('No files has been created.'); |
||||
813 | } |
||||
814 | |||||
815 | $this->comment('[DONE ] Creating new entity.'); |
||||
816 | $this->line(''); |
||||
817 | |||||
818 | if (in_array(true, $this->options(), true) === true) { |
||||
819 | $this->table($this->tableHeaders, $this->tableContents); |
||||
820 | $this->line(''); |
||||
821 | } |
||||
822 | } |
||||
823 | |||||
824 | /** |
||||
825 | * Print additional steps, if any. |
||||
826 | * |
||||
827 | * @return void |
||||
828 | */ |
||||
829 | protected function printAdditionalSteps() |
||||
830 | { |
||||
831 | if ($this->additionalSteps) { |
||||
0 ignored issues
–
show
The expression
$this->additionalSteps of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||||
832 | $this->comment('ATTENTION: You may have to proceed these additional steps:'); |
||||
833 | |||||
834 | foreach ($this->additionalSteps as $key => $step) { |
||||
835 | $this->line('- '.$step); |
||||
836 | } |
||||
837 | |||||
838 | $this->line(''); |
||||
839 | } |
||||
840 | } |
||||
841 | |||||
842 | /** |
||||
843 | * Add new row of data to output table. |
||||
844 | * |
||||
845 | * @param string $artefact |
||||
846 | * @param string $location |
||||
847 | * @return void |
||||
848 | */ |
||||
849 | protected function addToTable($artefact, $location) |
||||
850 | { |
||||
851 | $this->data['artefact'] = $artefact; |
||||
852 | $this->data['location'] = $location; |
||||
853 | |||||
854 | array_push($this->tableContents, $this->data); |
||||
855 | } |
||||
856 | |||||
857 | /** |
||||
858 | * Remove '.php' extension from file name. |
||||
859 | */ |
||||
860 | protected function removeFileExtension($filename) |
||||
861 | { |
||||
862 | return substr($filename, 0, -4); |
||||
863 | } |
||||
864 | } |
||||
865 |