1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apiato\Core\Generator\Commands; |
4
|
|
|
|
5
|
|
|
use Apiato\Core\Generator\GeneratorCommand; |
6
|
|
|
use Apiato\Core\Generator\Interfaces\ComponentsGenerator; |
7
|
|
|
use Illuminate\Support\Pluralizer; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ModelGenerator |
13
|
|
|
* |
14
|
|
|
* @author Justin Atack <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ModelGenerator extends GeneratorCommand implements ComponentsGenerator |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name = 'apiato:generate:model'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Create a new Model class'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The type of class being generated. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $fileType = 'Model'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The structure of the file path. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $pathStructure = '{container-name}/Models/*'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The structure of the file name. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $nameStructure = '{file-name}'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The name of the stub file. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $stubName = 'model.stub'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* User required/optional inputs expected to be passed while calling the command. |
63
|
|
|
* This is a replacement of the `getArguments` function "which reads whenever it's called". |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
public $inputs = [ |
68
|
|
|
['repository', null, InputOption::VALUE_OPTIONAL, 'Generate the corresponding Repository for this Model?'], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getUserInputs() |
75
|
|
|
{ |
76
|
|
|
$repository = $this->checkParameterOrConfirm('repository', 'Do you want to generate the corresponding Repository for this Model?', true); |
77
|
|
View Code Duplication |
if($repository) { |
|
|
|
|
78
|
|
|
// we need to generate a corresponding repository |
79
|
|
|
// so call the other command |
80
|
|
|
$status = $this->call('apiato:generate:repository', [ |
81
|
|
|
'--container' => $this->containerName, |
82
|
|
|
'--file' => $this->fileName . 'Repository' |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
if ($status == 0) { |
86
|
|
|
$this->printInfoMessage('The Repository was successfully generated'); |
87
|
|
|
} |
88
|
|
|
else { |
89
|
|
|
$this->printErrorMessage('Could not generate the corresponding Repository!'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'path-parameters' => [ |
95
|
|
|
'container-name' => $this->containerName, |
96
|
|
|
], |
97
|
|
|
'stub-parameters' => [ |
98
|
|
|
'_container-name' => Str::lower($this->containerName), |
99
|
|
|
'container-name' => $this->containerName, |
100
|
|
|
'class-name' => $this->fileName, |
101
|
|
|
'resource-key' => strtolower(Pluralizer::plural($this->fileName)), |
102
|
|
|
], |
103
|
|
|
'file-parameters' => [ |
104
|
|
|
'file-name' => $this->fileName, |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.