1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Console\ControllerMakeCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Microboard\Foundations\Traits\MicroboardPathResolver; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
class ResourceController extends ControllerMakeCommand |
12
|
|
|
{ |
13
|
|
|
use MicroboardPathResolver; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = 'microboard:controller'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get the stub file for the generator. |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
protected function getStub() |
28
|
|
|
{ |
29
|
|
|
return $this->resolveStubPath('/stubs/controller.stub'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the fully-qualified model class name. |
34
|
|
|
* |
35
|
|
|
* @param string $model |
36
|
|
|
* @return string |
37
|
|
|
* |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
protected function parseModel($model) |
41
|
|
|
{ |
42
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
43
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
47
|
|
|
|
48
|
|
|
if (!$this->option('namespaced') && !Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
49
|
|
|
$model = $rootNamespace . $model; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $model; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Build the model replacement values. |
57
|
|
|
* |
58
|
|
|
* @param array $replace |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function buildModelReplacements(array $replace) |
62
|
|
|
{ |
63
|
|
|
$replacements = parent::buildModelReplacements($replace); |
64
|
|
|
$use = "\r\nuse " . $replacements['{{ namespacedModel }}'] . ';'; |
65
|
|
|
$method = "\r\n\r\n\t/**\r\n\t * @return string\r\n\t */\r\n\t" . |
66
|
|
|
"protected function getModel(): string\r\n\t{\r\n\t\t" . |
67
|
|
|
"return {$replacements['{{ model }}']}::class;\r\n\t}"; |
68
|
|
|
|
69
|
|
|
if ($this->laravel->getNamespace() . $replacements['{{ model }}'] === $replacements['{{ namespacedModel }}']) { |
70
|
|
|
$use = ''; |
71
|
|
|
$method = ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return array_merge($replacements, [ |
75
|
|
|
'{{ useModel }}' => $use, |
76
|
|
|
'useModel' => $use, |
77
|
|
|
'{{ getModelMethod }}' => $method |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the console command arguments. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function getOptions() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
...$this->pathOptions(), |
|
|
|
|
90
|
|
|
['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'], |
91
|
|
|
['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'], |
92
|
|
|
['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class.'], |
93
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'], |
94
|
|
|
['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class.'], |
95
|
|
|
['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'], |
96
|
|
|
['namespaced', '', InputOption::VALUE_NONE, 'The Model you provided is includes his namespace.'], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|