|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Microboard\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
9
|
|
|
use Illuminate\Support\Facades\Route; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use Microboard\Factory; |
|
12
|
|
|
use Microboard\Foundations\Traits\MicroboardPathResolver; |
|
13
|
|
|
use Microboard\Models\Role; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
16
|
|
|
|
|
17
|
|
|
class ResourceCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
use MicroboardPathResolver; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command name. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $name = 'microboard:resource'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The console command description. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $description = 'Create an admin resource for given model'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Factory |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $microboard; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Filesystem |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $files; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* ResourceCommand constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Factory $microboard |
|
49
|
|
|
* @param Filesystem $files |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Factory $microboard, Filesystem $files) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct(); |
|
54
|
|
|
|
|
55
|
|
|
$this->microboard = $microboard; |
|
56
|
|
|
$this->files = $files; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Execute the console command. |
|
61
|
|
|
* |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
public function handle() |
|
65
|
|
|
{ |
|
66
|
|
|
$namespace = $this->option('namespace') ? $this->option('namespace') . '\\' : null; |
|
67
|
|
|
$modelName = $this->argument('model'); |
|
68
|
|
|
$model = $this->getNamespacedModel($modelName); |
|
69
|
|
|
$options = $this->getAvailableOptions(); |
|
70
|
|
|
$abilities = $this->getAvailableAbilities(); |
|
71
|
|
|
|
|
72
|
|
|
// Create permissions and make a policy |
|
73
|
|
|
if (in_array('policy', $options)) { |
|
74
|
|
|
$this->microboard->createPermissionsFor( |
|
75
|
|
|
Role::where('name', config('microboard.resources.default_role', 'admin'))->firstOrFail(), |
|
76
|
|
|
$modelName, |
|
|
|
|
|
|
77
|
|
|
$abilities |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
// Make a policy |
|
81
|
|
|
$this->call('microboard:policy', [ |
|
82
|
|
|
'name' => "{$modelName}Policy", |
|
83
|
|
|
'--model' => '/' . $model, |
|
84
|
|
|
'--abilities' => $this->option('abilities'), |
|
85
|
|
|
'--base_path' => $this->option('base_path'), |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Make controller and datatable classes |
|
90
|
|
|
if (in_array('controller', $options)) { |
|
91
|
|
|
// Make a controller |
|
92
|
|
|
$this->call('microboard:controller', [ |
|
93
|
|
|
'name' => "{$namespace}{$modelName}Controller", |
|
94
|
|
|
'--model' => $model, |
|
95
|
|
|
'--base_path' => $this->option('base_path'), |
|
96
|
|
|
'--namespaced' => true |
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
// Make a datatable |
|
100
|
|
View Code Duplication |
if (is_array($abilities) && in_array('viewAny', $abilities)) { |
|
|
|
|
|
|
101
|
|
|
$this->call('microboard:datatable', [ |
|
102
|
|
|
'name' => $modelName, |
|
103
|
|
|
'--model' => trim($model, '/\\'), |
|
104
|
|
|
'--columns' => $this->getModelFillableColumns($model), |
|
105
|
|
|
'--base_path' => $this->option('base_path') |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Make a store form request |
|
110
|
|
View Code Duplication |
if (is_array($abilities) && in_array('create', $abilities)) { |
|
|
|
|
|
|
111
|
|
|
$this->call('microboard:request', [ |
|
112
|
|
|
'name' => "{$modelName}\\StoreFormRequest", |
|
113
|
|
|
'--model' => $model, |
|
114
|
|
|
'--columns' => $this->getModelFillableColumns($model), |
|
115
|
|
|
'--base_path' => $this->option('base_path') |
|
116
|
|
|
]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Make a update form request |
|
120
|
|
View Code Duplication |
if (is_array($abilities) && in_array('update', $abilities)) { |
|
|
|
|
|
|
121
|
|
|
$this->call('microboard:request', [ |
|
122
|
|
|
'name' => "{$modelName}\\UpdateFormRequest", |
|
123
|
|
|
'--model' => $model, |
|
124
|
|
|
'--columns' => $this->getModelFillableColumns($model), |
|
125
|
|
|
'--base_path' => $this->option('base_path') |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Make views and lang |
|
131
|
|
|
if (in_array('views', $options)) { |
|
132
|
|
|
$this->makeFormBladeFile() |
|
133
|
|
|
->makeTableBladeFile(); |
|
134
|
|
|
|
|
135
|
|
|
foreach (config('microboard.localizations', []) as $lang) { |
|
136
|
|
|
if (!isset($lang['code'])) { |
|
137
|
|
|
throw new \InvalidArgumentException('Lang file must contains "code" index!'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->makeLangFileTo($lang['code']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$this->updateRoutesAndNavLinks(); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param $modelName |
|
149
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|mixed |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function getNamespacedModel($modelName) |
|
152
|
|
|
{ |
|
153
|
|
|
$model = ( |
|
154
|
|
|
$this->option('model-namespace') ? |
|
155
|
|
|
('/' . rtrim($this->option('model-namespace'), '/\\') . '/') : |
|
156
|
|
|
'' |
|
157
|
|
|
) . $modelName; |
|
158
|
|
|
$model = str_replace('/', '\\', $model); |
|
159
|
|
|
|
|
160
|
|
|
if (Str::startsWith($model, '\\')) { |
|
161
|
|
|
$namespacedModel = $model; |
|
162
|
|
|
} else { |
|
163
|
|
|
$namespacedModel = $this->laravel->getNamespace() . $model; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $namespacedModel; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return array |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getAvailableOptions() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->option('only') ? |
|
175
|
|
|
explode(',', $this->option('only')) : |
|
176
|
|
|
['controller', 'policy', 'views']; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function getAvailableAbilities() |
|
183
|
|
|
{ |
|
184
|
|
|
$abilities = ['viewAny', 'view', 'create', 'update', 'delete']; |
|
185
|
|
|
|
|
186
|
|
|
if ($this->option('abilities')) { |
|
187
|
|
|
$abilities = array_map(function ($ability) { |
|
188
|
|
|
return trim($ability); |
|
189
|
|
|
}, explode(',', $this->option('abilities'))); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $abilities; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $model |
|
199
|
|
|
* @param bool $filter |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function getModelFillableColumns(string $model, $filter = false) |
|
203
|
|
|
{ |
|
204
|
|
|
$model = resolve($model); |
|
205
|
|
|
|
|
206
|
|
|
return collect($model->getFillable()) |
|
207
|
|
|
->filter(function ($column) use ($model, $filter) { |
|
208
|
|
|
return $filter || !in_array($column, $model->getHidden()); |
|
209
|
|
|
}) |
|
210
|
|
|
->all(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Create table.blade.php file |
|
215
|
|
|
* |
|
216
|
|
|
* @return $this |
|
217
|
|
|
* @throws FileNotFoundException |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function makeTableBladeFile() |
|
220
|
|
|
{ |
|
221
|
|
|
$this->makeBladeFile('table'); |
|
222
|
|
|
|
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Create a new blade file with given name. |
|
228
|
|
|
* |
|
229
|
|
|
* @param $file |
|
230
|
|
|
* @return $this |
|
231
|
|
|
* @throws FileNotFoundException |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function makeBladeFile($file) |
|
234
|
|
|
{ |
|
235
|
|
|
$name = Str::of($this->argument('model'))->slug()->plural(); |
|
|
|
|
|
|
236
|
|
|
$stub = $this->resolveStubPath("/stubs/views/{$file}.stub"); |
|
237
|
|
|
$path = $this->getViewsPath($file, $name); |
|
238
|
|
|
|
|
239
|
|
|
if ($this->files->exists($path)) { |
|
240
|
|
|
return; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$this->makeDirectory($path); |
|
244
|
|
|
|
|
245
|
|
|
$this->files->put($path, $this->replaceVariablesForm($file, $stub, $name)); |
|
246
|
|
|
|
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param $view |
|
252
|
|
|
* @param $name |
|
253
|
|
|
* @return string |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function getViewsPath($view, $name): string |
|
256
|
|
|
{ |
|
257
|
|
|
$path = Str::lower("{$this->option('namespace')}/{$name}/{$view}.blade.php"); |
|
258
|
|
|
|
|
259
|
|
|
return $this->option('base_path') ? |
|
260
|
|
|
$this->option('base_path') . '/resources/views/' . $path : |
|
261
|
|
|
$this->laravel->resourcePath("views/{$path}"); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Build the directory for the class if necessary. |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $path |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function makeDirectory($path) |
|
271
|
|
|
{ |
|
272
|
|
|
if (!$this->files->isDirectory(dirname($path))) { |
|
273
|
|
|
$this->files->makeDirectory(dirname($path), 0777, true, true); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return $path; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Replace views variables. |
|
281
|
|
|
* |
|
282
|
|
|
* @param $file |
|
283
|
|
|
* @param string $stub |
|
284
|
|
|
* @param $name |
|
285
|
|
|
* @return string |
|
286
|
|
|
* @throws FileNotFoundException |
|
287
|
|
|
*/ |
|
288
|
|
|
protected function replaceVariablesForm($file, string $stub, $name) |
|
289
|
|
|
{ |
|
290
|
|
|
$stub = $this->files->get($stub); |
|
291
|
|
|
$replaces = []; |
|
292
|
|
|
|
|
293
|
|
View Code Duplication |
if ($file === 'form') { |
|
|
|
|
|
|
294
|
|
|
$columns = $this->getModelFillableColumns( |
|
295
|
|
|
$this->getNamespacedModel($this->argument('model')), |
|
296
|
|
|
true |
|
297
|
|
|
); |
|
298
|
|
|
$html = ''; |
|
299
|
|
|
|
|
300
|
|
|
foreach ($columns as $column) { |
|
301
|
|
|
$html .= str_replace([ |
|
302
|
|
|
'{{ column }}', '{{ model }}', '{{ variable }}' |
|
303
|
|
|
], [ |
|
304
|
|
|
$column, $name, Str::singular($name) |
|
305
|
|
|
], $this->files->get( |
|
306
|
|
|
$this->resolveStubPath('/stubs/views/form-input.stub') |
|
307
|
|
|
)); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$replaces = [ |
|
311
|
|
|
'{{ columns }}' => $html |
|
312
|
|
|
]; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
View Code Duplication |
if ($file === 'table') { |
|
|
|
|
|
|
316
|
|
|
$columns = $this->getModelFillableColumns( |
|
317
|
|
|
$this->getNamespacedModel($this->argument('model')) |
|
318
|
|
|
); |
|
319
|
|
|
$html = ''; |
|
320
|
|
|
|
|
321
|
|
|
foreach ($columns as $column) { |
|
322
|
|
|
$html .= str_replace([ |
|
323
|
|
|
'{{ column }}', '{{ model }}', '{{ variable }}' |
|
324
|
|
|
], [ |
|
325
|
|
|
$column, $name, Str::singular($name) |
|
326
|
|
|
], $this->files->get( |
|
327
|
|
|
$this->resolveStubPath('/stubs/views/table-column.stub') |
|
328
|
|
|
)); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
$replaces = [ |
|
332
|
|
|
'{{ columns }}' => $html |
|
333
|
|
|
]; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
return str_replace(array_keys($replaces), array_values($replaces), $stub); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Create form.blade.php |
|
341
|
|
|
* |
|
342
|
|
|
* @return $this |
|
343
|
|
|
* @throws FileNotFoundException |
|
344
|
|
|
*/ |
|
345
|
|
|
protected function makeFormBladeFile() |
|
346
|
|
|
{ |
|
347
|
|
|
$this->makeBladeFile('form'); |
|
348
|
|
|
|
|
349
|
|
|
return $this; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* It makes lang files to this model |
|
354
|
|
|
* |
|
355
|
|
|
* @param $code |
|
356
|
|
|
* @return $this |
|
357
|
|
|
* @throws FileNotFoundException |
|
358
|
|
|
*/ |
|
359
|
|
|
protected function makeLangFileTo($code) |
|
360
|
|
|
{ |
|
361
|
|
|
$columns = ''; |
|
362
|
|
|
$name = Str::of($this->argument('model'))->slug()->plural(); |
|
|
|
|
|
|
363
|
|
|
$stub = $this->resolveStubPath("/stubs/lang/{$code}.stub"); |
|
364
|
|
|
$modelName = $this->argument('model'); |
|
365
|
|
|
$path = Str::lower("{$code}/{$name}.php"); |
|
366
|
|
|
$path = $this->option('base_path') ? |
|
367
|
|
|
$this->option('base_path') . '/resources/lang/' . $path : |
|
368
|
|
|
$this->laravel->resourcePath("lang/{$path}"); |
|
369
|
|
|
|
|
370
|
|
|
if ($this->files->exists($path)) { |
|
371
|
|
|
return; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
$this->makeDirectory($path); |
|
375
|
|
|
|
|
376
|
|
|
foreach ($this->getModelFillableColumns($this->getNamespacedModel($modelName), false) as $column) { |
|
377
|
|
|
$title = Str::of($column)->replace('_', ' ')->title(); |
|
378
|
|
|
$columns .= "\r\n\t\t'{$column}' => '{$title}',"; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
$this->files->put($path, str_replace([ |
|
382
|
|
|
'{{ model }}', '{{ columns }}' |
|
383
|
|
|
], [ |
|
384
|
|
|
Str::title($name), $columns |
|
385
|
|
|
], $this->files->get($stub))); |
|
386
|
|
|
|
|
387
|
|
|
return $this; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* @throws FileNotFoundException |
|
392
|
|
|
*/ |
|
393
|
|
|
protected function updateRoutesAndNavLinks() |
|
394
|
|
|
{ |
|
395
|
|
|
$name = $this->argument('model'); |
|
396
|
|
|
$trans = Str::of($name)->slug()->plural(); |
|
|
|
|
|
|
397
|
|
|
|
|
398
|
|
|
if (Route::has("microboard.{$trans}.index")) { |
|
399
|
|
|
return; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
$navPath = ($this->option('base_path') ? |
|
403
|
|
|
$this->option('base_path') . '/resources/views/' : |
|
404
|
|
|
$this->laravel->resourcePath('views/')) |
|
405
|
|
|
. 'vendor/microboard/layouts/partials/navbar-links.blade.php'; |
|
406
|
|
|
$routePath = ($this->option('base_path') ? |
|
407
|
|
|
$this->option('base_path') . '/routes/' : |
|
408
|
|
|
$this->laravel->basePath('routes/')) |
|
409
|
|
|
. 'microboard.php'; |
|
410
|
|
|
|
|
411
|
|
|
$navLink = $this->files->get( |
|
412
|
|
|
$this->resolveStubPath('/stubs/views/nav-link.stub') |
|
413
|
|
|
); |
|
414
|
|
|
|
|
415
|
|
|
if ( |
|
416
|
|
|
!$this->files->exists($navPath) || |
|
417
|
|
|
!$this->files->exists($routePath) |
|
418
|
|
|
) { |
|
419
|
|
|
throw new \InvalidArgumentException('You must do microboard:install first!'); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
$this->files->append( |
|
423
|
|
|
$navPath, |
|
424
|
|
|
str_replace([ |
|
425
|
|
|
'{{ model }}', '{{ route }}', '{{ trans }}' |
|
426
|
|
|
], [ |
|
427
|
|
|
$this->getNamespacedModel($name), |
|
428
|
|
|
$trans, |
|
429
|
|
|
$trans |
|
430
|
|
|
], $navLink) |
|
431
|
|
|
); |
|
432
|
|
|
|
|
433
|
|
|
$this->files->append( |
|
434
|
|
|
$routePath, |
|
435
|
|
|
"\r\nRoute::resource('$trans', '{$name}Controller');" |
|
436
|
|
|
); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* Get the console command arguments. |
|
441
|
|
|
* |
|
442
|
|
|
* @return array |
|
443
|
|
|
*/ |
|
444
|
|
|
protected function getArguments() |
|
445
|
|
|
{ |
|
446
|
|
|
return [ |
|
447
|
|
|
['model', InputArgument::REQUIRED, 'The model name'] |
|
448
|
|
|
]; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** |
|
452
|
|
|
* Get the console command arguments. |
|
453
|
|
|
* |
|
454
|
|
|
* @return array |
|
455
|
|
|
*/ |
|
456
|
|
|
protected function getOptions() |
|
457
|
|
|
{ |
|
458
|
|
|
// TODO:: add description |
|
459
|
|
|
return [ |
|
460
|
|
|
['namespace', '', InputOption::VALUE_OPTIONAL, '', config('microboard.routes.namespace.admin_directory', 'Admin')], |
|
461
|
|
|
['model-namespace', '', InputOption::VALUE_REQUIRED, ''], |
|
462
|
|
|
['base_path', 'p', InputOption::VALUE_OPTIONAL, ''], |
|
463
|
|
|
['abilities', 'a', InputOption::VALUE_OPTIONAL, ''], |
|
464
|
|
|
['only', 'o', InputOption::VALUE_OPTIONAL, ''], |
|
465
|
|
|
]; |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.