1 | <?php |
||
2 | |||
3 | namespace Yeelight\Generators\Commands; |
||
4 | |||
5 | use Illuminate\Support\Collection; |
||
6 | use Symfony\Component\Console\Input\InputArgument; |
||
7 | use Symfony\Component\Console\Input\InputOption; |
||
8 | use Yeelight\Generators\FileAlreadyExistsException; |
||
9 | use Yeelight\Generators\MigrationGenerator; |
||
10 | use Yeelight\Generators\ModelGenerator; |
||
11 | use Yeelight\Generators\RepositoryEloquentGenerator; |
||
12 | use Yeelight\Generators\RepositoryInterfaceGenerator; |
||
13 | |||
14 | /** |
||
15 | * Class RepositoryCommand |
||
16 | * |
||
17 | * @category Yeelight |
||
18 | * |
||
19 | * @package Yeelight\Generators\Commands |
||
20 | * |
||
21 | * @author Sheldon Lee <[email protected]> |
||
22 | * |
||
23 | * @license https://opensource.org/licenses/MIT MIT |
||
24 | * |
||
25 | * @link https://www.yeelight.com |
||
26 | */ |
||
27 | class RepositoryCommand extends CommandBase |
||
28 | { |
||
29 | /** |
||
30 | * The name of command. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $name = 'yl:repository'; |
||
35 | |||
36 | /** |
||
37 | * The description of command. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $description = 'Create a new repository.'; |
||
42 | |||
43 | /** |
||
44 | * The type of class being generated. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $type = 'Repository'; |
||
49 | |||
50 | /** |
||
51 | * $generators |
||
52 | * |
||
53 | * @var Collection |
||
54 | */ |
||
55 | protected $generators = null; |
||
56 | |||
57 | /** |
||
58 | * Execute the command. |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | public function fire() |
||
63 | { |
||
64 | $this->generators = new Collection(); |
||
65 | |||
66 | $this->generators->push( |
||
67 | new MigrationGenerator( |
||
68 | [ |
||
69 | 'name' => 'create_' . snake_case(str_plural($this->argument('name'))) . '_table', |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
70 | 'fields' => $this->option('fillable'), |
||
71 | 'force' => $this->option('force'), |
||
72 | ] |
||
73 | ) |
||
74 | ); |
||
75 | |||
76 | $modelGenerator = new ModelGenerator( |
||
77 | [ |
||
78 | 'name' => $this->argument('name'), |
||
79 | 'fillable' => $this->option('fillable'), |
||
80 | 'force' => $this->option('force'), |
||
81 | ] |
||
82 | ); |
||
83 | |||
84 | $this->generators->push($modelGenerator); |
||
85 | |||
86 | $this->generators->push( |
||
87 | new RepositoryInterfaceGenerator( |
||
88 | [ |
||
89 | 'name' => $this->argument('name'), |
||
90 | 'force' => $this->option('force'), |
||
91 | ] |
||
92 | ) |
||
93 | ); |
||
94 | |||
95 | foreach ($this->generators as $generator) { |
||
96 | $generator->run(); |
||
97 | } |
||
98 | |||
99 | $model = $modelGenerator->getRootNamespace().'\\'.$modelGenerator->getName(); |
||
100 | $model = str_replace( |
||
101 | [ |
||
102 | '\\', |
||
103 | '/', |
||
104 | ], |
||
105 | '\\', |
||
106 | $model |
||
107 | ); |
||
108 | |||
109 | try { |
||
110 | (new RepositoryEloquentGenerator( |
||
111 | [ |
||
112 | 'name' => $this->argument('name'), |
||
113 | 'rules' => $this->option('rules'), |
||
114 | 'fields' => $this->option('fields'), |
||
115 | 'validator' => $this->option('validator'), |
||
116 | 'presenter' => $this->option('presenter'), |
||
117 | 'force' => $this->option('force'), |
||
118 | 'model' => $model, |
||
119 | ] |
||
120 | ))->run(); |
||
121 | $this->info('Repository created successfully.'); |
||
122 | } catch (FileAlreadyExistsException $e) { |
||
123 | $this->error($this->type.' already exists!'); |
||
124 | |||
125 | return false; |
||
0 ignored issues
–
show
|
|||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * The array of command arguments. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function getArguments() |
||
135 | { |
||
136 | return [ |
||
137 | [ |
||
138 | 'name', |
||
139 | InputArgument::REQUIRED, |
||
140 | 'The name of class being generated.', |
||
141 | null, |
||
142 | ], |
||
143 | ]; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * The array of command options. |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function getOptions() |
||
152 | { |
||
153 | return [ |
||
154 | [ |
||
155 | 'fillable', |
||
156 | null, |
||
157 | InputOption::VALUE_OPTIONAL, |
||
158 | 'The fillable attributes.', |
||
159 | null, |
||
160 | ], |
||
161 | [ |
||
162 | 'rules', |
||
163 | null, |
||
164 | InputOption::VALUE_OPTIONAL, |
||
165 | 'The rules of validation attributes.', |
||
166 | null, |
||
167 | ], |
||
168 | [ |
||
169 | 'fields', |
||
170 | null, |
||
171 | InputOption::VALUE_OPTIONAL, |
||
172 | 'The fields attributes.', |
||
173 | null, |
||
174 | ], |
||
175 | [ |
||
176 | 'validator', |
||
177 | null, |
||
178 | InputOption::VALUE_OPTIONAL, |
||
179 | 'Adds validator reference to the repository.', |
||
180 | null, |
||
181 | ], |
||
182 | [ |
||
183 | 'presenter', |
||
184 | null, |
||
185 | InputOption::VALUE_OPTIONAL, |
||
186 | 'Adds presenter reference to the repository.', |
||
187 | null, |
||
188 | ], |
||
189 | [ |
||
190 | 'force', |
||
191 | 'f', |
||
192 | InputOption::VALUE_NONE, |
||
193 | 'Force the creation if file already exists.', |
||
194 | null, |
||
195 | ], |
||
196 | ]; |
||
197 | } |
||
198 | } |
||
199 |