|
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
|
|
|
{--header=} |
|
19
|
|
|
{--stub= : Path to the custom stub file. } |
|
20
|
|
|
{--O|output}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Make admin controller'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ResourceGenerator |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $generator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Execute the console command. |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
public function handle() |
|
40
|
|
|
{ |
|
41
|
|
|
if (!$this->modelExists()) { |
|
42
|
|
|
$this->error('Model does not exists !'); |
|
43
|
|
|
|
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$stub = $this->option('stub'); |
|
48
|
|
|
|
|
49
|
|
|
if ($stub and !is_file($stub)) { |
|
50
|
|
|
$this->error('The stub file dose not exist.'); |
|
51
|
|
|
|
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$modelName = $this->option('model'); |
|
56
|
|
|
|
|
57
|
|
|
$this->generator = new ResourceGenerator($modelName); |
|
58
|
|
|
|
|
59
|
|
|
if ($this->option('output')) { |
|
60
|
|
|
return $this->output($modelName); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (parent::handle() !== false) { |
|
64
|
|
|
|
|
65
|
|
|
$name = $this->argument('name'); |
|
66
|
|
|
$path = Str::plural(Str::slug(class_basename($this->option('model')))); |
|
67
|
|
|
|
|
68
|
|
|
$this->line(''); |
|
69
|
|
|
$this->comment('Add the following route to app/Admin/routes.php:'); |
|
70
|
|
|
$this->line(''); |
|
71
|
|
|
$this->info(" \$router->resource('{$path}', {$name}::class);"); |
|
72
|
|
|
$this->line(''); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $modelName |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function output($modelName) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->alert("laravel-admin controller code for model [{$modelName}]"); |
|
82
|
|
|
|
|
83
|
|
|
$this->info($this->generator->generateGrid()); |
|
84
|
|
|
$this->info($this->generator->generateShow()); |
|
85
|
|
|
$this->info($this->generator->generateForm()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Determine if the model is exists. |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function modelExists() |
|
94
|
|
|
{ |
|
95
|
|
|
$model = $this->option('model'); |
|
96
|
|
|
|
|
97
|
|
|
if (empty($model)) { |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return class_exists($model) && is_subclass_of($model, Model::class); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Replace the class name for the given stub. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $stub |
|
108
|
|
|
* @param string $name |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function replaceClass($stub, $name) |
|
113
|
|
|
{ |
|
114
|
|
|
$stub = parent::replaceClass($stub, $name); |
|
115
|
|
|
|
|
116
|
|
|
return str_replace( |
|
117
|
|
|
[ |
|
118
|
|
|
'DummyModelNamespace', |
|
119
|
|
|
'DummyHeader', |
|
120
|
|
|
'DummyModel', |
|
121
|
|
|
'DummyGrid', |
|
122
|
|
|
'DummyShow', |
|
123
|
|
|
'DummyForm', |
|
124
|
|
|
], |
|
125
|
|
|
[ |
|
126
|
|
|
$this->option('model'), |
|
127
|
|
|
$this->option('header') ?: $this->option('model'), |
|
128
|
|
|
class_basename($this->option('model')), |
|
129
|
|
|
$this->indentCodes($this->generator->generateGrid()), |
|
130
|
|
|
$this->indentCodes($this->generator->generateShow()), |
|
131
|
|
|
$this->indentCodes($this->generator->generateForm()), |
|
132
|
|
|
], |
|
133
|
|
|
$stub |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $code |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function indentCodes($code) |
|
143
|
|
|
{ |
|
144
|
|
|
$indent = str_repeat(' ', 8); |
|
145
|
|
|
|
|
146
|
|
|
return rtrim($indent.preg_replace("/\r\n/", "\r\n{$indent}", $code)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the stub file for the generator. |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function getStub() |
|
155
|
|
|
{ |
|
156
|
|
|
if ($stub = $this->option('stub')) { |
|
157
|
|
|
return $stub; |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($this->option('model')) { |
|
161
|
|
|
return __DIR__.'/stubs/controller.stub'; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return __DIR__.'/stubs/blank.stub'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the default namespace for the class. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $rootNamespace |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
175
|
|
|
{ |
|
176
|
|
|
return config('admin.route.namespace'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the desired class name from the input. |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function getNameInput() |
|
185
|
|
|
{ |
|
186
|
|
|
$name = trim($this->argument('name')); |
|
187
|
|
|
|
|
188
|
|
|
$this->type = $this->qualifyClass($name); |
|
189
|
|
|
|
|
190
|
|
|
return $name; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|