MakeApiCommand.php
12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 自定义命令: 自定义API接口,创建api接口的全部类:控制层、服务层、数据验证层、数据转换层、模型层
* 命令:php artisan make:api User (这个是路由的单数,如果是多个单词要用驼峰写法UserRole)
* 参数说明:
* --title:该模块的名称
* --author:作者
* --policy:是否创建 policy 策略类
* --request:是否创建 request 验证类
* --admin:是否创建 laravel-admin 管理端的类
* ...
* ...
* ...
*
+-----------------------------------------------------------------------------------------------------------------------
*
* @copyright Copyright
* @author Richer
* @package App\Console\Commands
* @version 2019年11月27日,11:30:36
* @link
*/
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class MakeApiCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'make:api {name}
{--title=}
{--author=}
{--email=}
{--policy}
{--request}
{--admin}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make Api';
/**
* @var AdminResourceGenerator
*/
protected $generator;
/**
* Execute the console command.
*
* @return void
* @throws \Exception
*/
public function handle()
{
// 获取对象的名称
$name = $this->argument('name');
// controller
$controller_name = Str::plural(ucfirst($name));
// 获取 title:该模块的中文名
$title = $this->option('title') ? : 'xxx';
// 获取 author 当前模块的作者
$author = $this->option('author') ? : 'Richer';
// 获取 email 当前模块的作者的email
$email = $this->option('email') ? : 'yangzi1028@163.com';
// 获取是否创建 policy
$policy = $this->option('policy');
// 是否创建 request
$request = $this->option('request');
$arguments= [
'title' => $title,
'author' => $author,
'email' => $email,
];
// $title = $this->argument('--title');
// dd($title);
// // 获取单数形式
// $name = str_world2singular($name_origin);
// // $this->info(" {$name}");
// // 多个单词将中划线转为驼峰
// $name = str_strikeline2camp($name);
// 根据
// // 判断模型是否存在
// if (!$this->modelExists($name)) {
// $this->error('Model does not exists !');
// return false;
// }
// 创建controller
$this->makeClass($name, 'controller', $arguments);
// 创建service
$this->makeClass($name, 'service', $arguments);
// 创建model
$this->makeClass($name, 'model', $arguments);
// 创建transform
$this->makeClass($name, 'transformer', $arguments);
// if ($request) {
// 创建request
$this->makeClass($name, 'request', $arguments);
// }
if ($policy) {
// 创建policy
$this->makeClass($name, 'policy', $arguments);
}
// 转成kebab case (烤肉串式):路由的模式
$path = strtolower(Str::plural(Str::kebab(class_basename($name))));
//$this->info($this->generator->generateGrid());
// $rute_path ='routes/api.php';
// $rute_file = fopen($rute_path, 'a+');
//
// fwrite($rute_file, PHP_EOL);
// fwrite($rute_file, "/*".PHP_EOL);
// fwrite($rute_file, "|--------------------------------------------------------------------------".PHP_EOL);
// fwrite($rute_file, "|TODO(移动到对应的位置)当前模块名".PHP_EOL);
// fwrite($rute_file, "|--------------------------------------------------------------------------".PHP_EOL);
// fwrite($rute_file, "*/".PHP_EOL);
// fwrite($rute_file, "\$api->resource('{$path}', '{$name}Controller');".PHP_EOL);
// // 关闭文件
// fclose($rute_file);
$this->line('');
$this->comment('将以下路由添加到routes/api.php中(Add the following route to routes/api.php):');
$this->info(" \$api->resource('{$path}', '".$controller_name."Controller');");
$this->line('');
// 是否创建admin
$admin = $this->option('admin');
if ($admin) {
// 渲染生成admin端的文件
$this->generator = new AdminResourceGenerator($name);
// 创建 grid
$grid = $this->generator->generateGrid();
$this->makeClass($name, 'admin-grid', $arguments, $grid);
// 创建 form
$form = $this->generator->generateForm();
$this->makeClass($name, 'admin-form', $arguments, '', $form);
// 创建admin controller
$this->makeClass($name, 'admin-controller', $arguments);
// 写入admin路由
$this->writeAdminRoute($path, $name, $title);
}
}
/**
* 创建controller
*
* @param $name
* @param string $class_type
* @param $arguments
* @param string $grid_fields
* @param string $form_fields
*/
public function makeClass($name, $class_type, $arguments, $grid_fields = '', $form_fields = '')
{
$rootNamespace = $this->rootNamespace();
// add by Richer 于 2020年9月11日09:50:23 controller 按照规范为复数
$controller_name = Str::plural(ucfirst($name));
// 拼接命名空间:此处写死
$class_name = ucfirst($name).ucfirst($class_type);
$table_name = strtolower(Arr::last(world_transform($name)));
$obj_name = strtolower($name);
$class = $rootNamespace.ucfirst(Arr::last(world_transform($class_type))).'\\'.$class_name;
switch ($class_type) {
case 'controller':
$class_name = $controller_name.ucfirst($class_type);
$class = $rootNamespace.'\Http\Api\V1\Controllers\\' . $class_name;
break;
case 'request':
$class_name = ucfirst($name) . 'Request';
$class = $rootNamespace.'\Http\Requests\\' . $class_name;
break;
case 'policy':
$class_name = ucfirst($name).'Policy';
$class = $rootNamespace.'\Policies\\'.$class_name;
break;
case 'model':
$class_name = ucfirst($name);
$class = $rootNamespace.'\Models\\'.$class_name;
break;
case 'admin-grid':
$class_name = ucfirst($name) . 'Grid';
$class = $rootNamespace.'\Admin\Grids\\' . $class_name;
break;
case 'admin-form':
$class_name = ucfirst($name) . 'Form';
$class = $rootNamespace.'\Admin\Forms\\' . $class_name;
break;
case 'admin-controller':
$class_name = $controller_name . 'Controller';
$class = $rootNamespace.'\Admin\Controllers\\' . $class_name;
break;
case 'agency-controller':
$class_name = $controller_name . 'Controller';
$class = $rootNamespace.'\Agency\Controllers\\' . $class_name;
break;
}
// 获取文件存储位置
$class_path = $this->getPath($class);
// 判断文件是否存在,存在则抛出错误
if (class_exists($class)) {
$this->error($class_name.' already exists!');
// return false;
} else {
// make class
$version = date('Y年m月d日 H:i:s');
$content = file_get_contents(__DIR__.'/stubs/'.$class_type.'.stub');
$content = str_replace(
[
':version',
':class_name',
':controller_name',
':title',
':author',
':email',
':grid_fields',
':form_fields',
':table_name',
':obj_name'
],
[
$version,
$name,
$controller_name,
$arguments['title'],
$arguments['author'],
$arguments['email'],
$grid_fields,
$form_fields,
$table_name,
$obj_name,
],
$content
);
$file = fopen($class_path, 'w');
// 写入
fwrite($file, $content);
}
}
/**
* 创建 api controller
*
* @param $name
* @param string $title
* @param string $author
*/
protected function makeApiController($name, $title = 'xxx', $author = 'Richer')
{
// $class_name = $controller_name.ucfirst($class_type);
// $class = 'App\Http\Api\V1\Controllers\\' . $class_name;
}
/**
* 写入admin路由
* @param $path
* @param $name
* @param $title
*/
protected function writeAdminRoute($path, $name, $title)
{
$route_path ='app/Admin/routes.php';
$admin_conttoller = Str::plural(ucfirst($name)) .'Controller';
$route_file = fopen($route_path, 'r+');
fseek($route_file, -6, SEEK_END);
fwrite($route_file, PHP_EOL);
fwrite($route_file, " /*".PHP_EOL);
fwrite($route_file, " |-------------------------------------------------------------------------------------------------------------------".PHP_EOL);
fwrite($route_file, " | {$title}管理".PHP_EOL);
fwrite($route_file, " |-------------------------------------------------------------------------------------------------------------------".PHP_EOL);
fwrite($route_file, " */".PHP_EOL);
fwrite($route_file, " \$router->resource('{$path}', '{$admin_conttoller}');".PHP_EOL);
fwrite($route_file, "});".PHP_EOL);
// 关闭文件
fclose($route_file);
}
public function makeFile($path)
{
\File::isDirectory($path) or \File::makeDirectory($path, 0777, true, true);
}
/**
* @param string $modelName
*/
protected function output($modelName)
{
$this->alert("Richer-api controller code for model [{$modelName}]");
}
/**
* Determine if the model is exists.
*
* @param $name
* @return bool
*/
protected function modelExists($name)
{
$model = 'App\Models\\'.ucfirst($name);
return class_exists($model) && is_subclass_of($model, Model::class);
}
/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
*
* @return string
*/
protected function replaceClass($stub, $name)
{
$stub = parent::replaceClass($stub, $name);
return str_replace(
[
'DummyModelNamespace',
'DummyTitle',
'DummyModel',
'DummyGrid',
'DummyShow',
'DummyForm',
],
[
$this->option('model'),
$this->option('title') ?: $this->option('model'),
class_basename($this->option('model')),
],
$stub
);
}
/**
* @param string $code
*
* @return string
*/
protected function indentCodes($code)
{
$indent = str_repeat(' ', 8);
return rtrim($indent.preg_replace("/\r\n/", "\r\n{$indent}", $code));
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($stub = $this->option('stub')) {
return $stub;
}
if ($this->option('model')) {
return __DIR__.'/stubs/controller.stub';
}
return __DIR__.'/stubs/blank.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
if ($namespace = $this->option('namespace')) {
return $namespace;
}
}
/**
* Get the desired class name from the input.
*
* @return string
*/
protected function getNameInput()
{
$name = trim($this->argument('name'));
$this->type = $this->qualifyClass($name);
return $name;
}
}