|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class MakeCommand extends GeneratorCommand |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The console command name. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'admin:make {name} |
|
17
|
|
|
{--model=} |
|
18
|
|
|
{--title=} |
|
19
|
|
|
{--stub= : Path to the custom stub file. } |
|
20
|
|
|
{--namespace=} |
|
21
|
|
|
{--O|output}'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The console command description. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $description = 'Make admin controller'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ResourceGenerator |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $generator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $controllerName; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $modelName; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Execute the console command. |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function handle() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->modelName = $this->getModelName(); |
|
|
|
|
|
|
53
|
|
|
$this->controllerName = $this->getControllerName(); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if (!$this->modelExists()) { |
|
56
|
|
|
$this->error('Model does not exists !'); |
|
57
|
|
|
|
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$stub = $this->option('stub'); |
|
62
|
|
|
|
|
63
|
|
|
if ($stub and !is_file($stub)) { |
|
64
|
|
|
$this->error('The stub file dose not exist.'); |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->generator = new ResourceGenerator($this->modelName); |
|
70
|
|
|
|
|
71
|
|
|
if ($this->option('output')) { |
|
72
|
|
|
return $this->output($this->modelName); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (parent::handle() !== false) { |
|
76
|
|
|
$path = Str::plural(Str::kebab(class_basename($this->modelName))); |
|
77
|
|
|
|
|
78
|
|
|
$this->line(''); |
|
79
|
|
|
$this->comment('Add the following route to app/Admin/routes.php:'); |
|
80
|
|
|
$this->line(''); |
|
81
|
|
|
$this->info(" \$router->resource('{$path}', {$this->controllerName}::class);"); |
|
82
|
|
|
$this->line(''); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array|string|null |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getControllerName() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->argument('name'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array|string|null |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getModelName() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->option('model'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array|bool|string|null |
|
104
|
|
|
* @throws \ReflectionException |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getTitle() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($title = $this->option('title')) { |
|
109
|
|
|
return $title; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return __((new \ReflectionClass($this->modelName))->getShortName()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $modelName |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function output($modelName) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->alert("laravel-admin controller code for model [{$modelName}]"); |
|
121
|
|
|
|
|
122
|
|
|
$this->info($this->generator->generateGrid()); |
|
123
|
|
|
$this->info($this->generator->generateShow()); |
|
124
|
|
|
$this->info($this->generator->generateForm()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Determine if the model is exists. |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function modelExists() |
|
133
|
|
|
{ |
|
134
|
|
|
if (empty($this->modelName)) { |
|
135
|
|
|
return true; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return class_exists($this->modelName) && is_subclass_of($this->modelName, Model::class); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Replace the class name for the given stub. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $stub |
|
145
|
|
|
* @param string $name |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function replaceClass($stub, $name) |
|
150
|
|
|
{ |
|
151
|
|
|
$stub = parent::replaceClass($stub, $name); |
|
152
|
|
|
|
|
153
|
|
|
return str_replace( |
|
154
|
|
|
[ |
|
155
|
|
|
'DummyModelNamespace', |
|
156
|
|
|
'DummyTitle', |
|
157
|
|
|
'DummyModel', |
|
158
|
|
|
'DummyGrid', |
|
159
|
|
|
'DummyShow', |
|
160
|
|
|
'DummyForm', |
|
161
|
|
|
], |
|
162
|
|
|
[ |
|
163
|
|
|
$this->modelName, |
|
164
|
|
|
$this->getTitle(), |
|
165
|
|
|
class_basename($this->modelName), |
|
166
|
|
|
$this->indentCodes($this->generator->generateGrid()), |
|
167
|
|
|
$this->indentCodes($this->generator->generateShow()), |
|
168
|
|
|
$this->indentCodes($this->generator->generateForm()), |
|
169
|
|
|
], |
|
170
|
|
|
$stub |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param string $code |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function indentCodes($code) |
|
180
|
|
|
{ |
|
181
|
|
|
$indent = str_repeat(' ', 8); |
|
182
|
|
|
|
|
183
|
|
|
return rtrim($indent.preg_replace("/\r\n/", "\r\n{$indent}", $code)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get the stub file for the generator. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function getStub() |
|
192
|
|
|
{ |
|
193
|
|
|
if ($stub = $this->option('stub')) { |
|
194
|
|
|
return $stub; |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if ($this->modelName) { |
|
198
|
|
|
return __DIR__.'/stubs/controller.stub'; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return __DIR__.'/stubs/blank.stub'; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Get the default namespace for the class. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $rootNamespace |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
212
|
|
|
{ |
|
213
|
|
|
if ($namespace = $this->option('namespace')) { |
|
214
|
|
|
return $namespace; |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return config('admin.route.namespace'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get the desired class name from the input. |
|
222
|
|
|
* |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getNameInput() |
|
226
|
|
|
{ |
|
227
|
|
|
$this->type = $this->qualifyClass($this->controllerName); |
|
228
|
|
|
|
|
229
|
|
|
return $this->controllerName; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.