1 | <?php |
||
10 | class ApiMakeCommand extends Command |
||
11 | { |
||
12 | use DetectsApplicationNamespace; |
||
13 | |||
14 | /** |
||
15 | * The filesystem instance. |
||
16 | * |
||
17 | * @var \Illuminate\Filesystem\Filesystem |
||
18 | */ |
||
19 | protected $files; |
||
20 | |||
21 | /** |
||
22 | * The console command name. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $name = 'make:api'; |
||
27 | |||
28 | /** |
||
29 | * The console command description. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $description = 'Create api controller, transformer and api routes for a given model (arrilot/laravel-api-generator)'; |
||
34 | |||
35 | /** |
||
36 | * The array of variables available in stubs. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $stubVariables = [ |
||
41 | 'app' => [], |
||
42 | 'model' => [], |
||
43 | 'controller' => [], |
||
44 | 'transformer' => [], |
||
45 | 'route' => [], |
||
46 | ]; |
||
47 | |||
48 | protected $modelsBaseNamespace; |
||
49 | |||
50 | /** |
||
51 | * Create a new controller creator command instance. |
||
52 | * |
||
53 | * @param \Illuminate\Filesystem\Filesystem $files |
||
54 | */ |
||
55 | public function __construct(Filesystem $files) |
||
61 | |||
62 | /** |
||
63 | * Execute the console command. |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | public function handle() |
||
68 | { |
||
69 | $this->prepareVariablesForStubs($this->argument('name')); |
||
70 | |||
71 | $this->createController(); |
||
72 | |||
73 | $this->createTransformer(); |
||
74 | |||
75 | $this->addRoutes(); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Prepare names, paths and namespaces for stubs. |
||
80 | * |
||
81 | * @param $name |
||
82 | */ |
||
83 | protected function prepareVariablesForStubs($name) |
||
96 | |||
97 | /** |
||
98 | * Set the model name and namespace. |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | protected function setModelData($name) |
||
123 | |||
124 | /** |
||
125 | * Set the controller names and namespaces. |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | protected function setControllerData() |
||
133 | |||
134 | /** |
||
135 | * Set route data for a given model. |
||
136 | * "Profile\Payer" -> "profile_payers". |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | protected function setRouteData() |
||
149 | |||
150 | /** |
||
151 | * Set the transformer names and namespaces. |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | protected function setTransformerData() |
||
159 | |||
160 | /** |
||
161 | * Set entity's names and namespaces. |
||
162 | * |
||
163 | * @param string $entity |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | protected function setDataForEntity($entity) |
||
187 | |||
188 | /** |
||
189 | * Create controller class file from a stub. |
||
190 | */ |
||
191 | protected function createController() |
||
195 | |||
196 | /** |
||
197 | * Create controller class file from a stub. |
||
198 | */ |
||
199 | protected function createTransformer() |
||
203 | |||
204 | /** |
||
205 | * Add routes to routes file. |
||
206 | */ |
||
207 | protected function addRoutes() |
||
232 | |||
233 | /** |
||
234 | * Create class with a given type. |
||
235 | * |
||
236 | * @param $type |
||
237 | * |
||
238 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
239 | */ |
||
240 | protected function createClass($type) |
||
255 | |||
256 | /** |
||
257 | * Get the destination file path. |
||
258 | * |
||
259 | * @param string $name |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function getPath($name) |
||
269 | |||
270 | /** |
||
271 | * Build the directory for the class if needed. |
||
272 | * |
||
273 | * @param string $path |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | protected function makeDirectoryIfNeeded($path) |
||
283 | |||
284 | /** |
||
285 | * Get stub content and replace all stub placeholders |
||
286 | * with data from $this->stubData. |
||
287 | * |
||
288 | * @param string $path |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | protected function constructStub($path) |
||
304 | |||
305 | /** |
||
306 | * Get the console command arguments. |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | protected function getArguments() |
||
316 | |||
317 | /** |
||
318 | * Convert "/" to "\". |
||
319 | * |
||
320 | * @param $string |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | protected function convertSlashes($string) |
||
328 | } |
||
329 |