1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
class MakeCommand extends GeneratorCommand |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The console command name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'admin:make {name} {--model=} {--O|output}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Make empty admin controller'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResourceGenerator |
26
|
|
|
*/ |
27
|
|
|
protected $generator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function handle() |
35
|
|
|
{ |
36
|
|
|
if (!$this->modelExists()) { |
37
|
|
|
$this->error('Model does not exists !'); |
38
|
|
|
|
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$modelName = $this->option('model'); |
43
|
|
|
|
44
|
|
|
$this->generator = new ResourceGenerator($modelName); |
45
|
|
|
|
46
|
|
|
if ($this->option('output')) { |
47
|
|
|
return $this->output($modelName); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
parent::handle(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $modelName |
55
|
|
|
*/ |
56
|
|
|
protected function output($modelName) |
57
|
|
|
{ |
58
|
|
|
$this->alert("laravel-admin controller code for model [{$modelName}]"); |
59
|
|
|
|
60
|
|
|
$this->info($this->generator->generateGrid()); |
61
|
|
|
$this->info($this->generator->generateShow()); |
62
|
|
|
$this->info($this->generator->generateForm()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Determine if the model is exists. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected function modelExists() |
71
|
|
|
{ |
72
|
|
|
$model = $this->option('model'); |
73
|
|
|
|
74
|
|
|
if (empty($model)) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return class_exists($model) && is_subclass_of($model, Model::class); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Replace the class name for the given stub. |
83
|
|
|
* |
84
|
|
|
* @param string $stub |
85
|
|
|
* @param string $name |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function replaceClass($stub, $name) |
90
|
|
|
{ |
91
|
|
|
$stub = parent::replaceClass($stub, $name); |
92
|
|
|
|
93
|
|
|
return str_replace( |
94
|
|
|
[ |
95
|
|
|
'DummyModelNamespace', |
96
|
|
|
'DummyModel', |
97
|
|
|
'DummyGrid', |
98
|
|
|
'DummyShow', |
99
|
|
|
'DummyForm' |
100
|
|
|
], |
101
|
|
|
[ |
102
|
|
|
$this->option('model'), |
103
|
|
|
class_basename($this->option('model')), |
|
|
|
|
104
|
|
|
$this->indentCodes($this->generator->generateGrid()), |
105
|
|
|
$this->indentCodes($this->generator->generateShow()), |
106
|
|
|
$this->indentCodes($this->generator->generateForm()) |
107
|
|
|
], |
108
|
|
|
$stub |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $code |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function indentCodes($code) |
117
|
|
|
{ |
118
|
|
|
$indent = str_repeat(' ', 8); |
119
|
|
|
|
120
|
|
|
return rtrim($indent. preg_replace("/\r\n/", "\r\n{$indent}", $code)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the stub file for the generator. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function getStub() |
129
|
|
|
{ |
130
|
|
|
if ($this->option('model')) { |
131
|
|
|
return __DIR__.'/stubs/controller.stub'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return __DIR__.'/stubs/blank.stub'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the default namespace for the class. |
139
|
|
|
* |
140
|
|
|
* @param string $rootNamespace |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function getDefaultNamespace($rootNamespace) |
145
|
|
|
{ |
146
|
|
|
$directory = config('admin.directory'); |
147
|
|
|
|
148
|
|
|
$namespace = ucfirst(basename($directory)); |
149
|
|
|
|
150
|
|
|
return $rootNamespace."\\$namespace\Controllers"; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the desired class name from the input. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected function getNameInput() |
159
|
|
|
{ |
160
|
|
|
$name = trim($this->argument('name')); |
161
|
|
|
|
162
|
|
|
$this->type = $this->qualifyClass($name); |
163
|
|
|
|
164
|
|
|
return $name; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.