1 | <?php |
||
2 | |||
3 | namespace App\Console\Commands; |
||
4 | |||
5 | use Illuminate\Console\Command; |
||
6 | use Illuminate\Support\Str; |
||
7 | |||
8 | class CreateModelRelatedFiles extends Command |
||
9 | { |
||
10 | /** |
||
11 | * The name and signature of the console command. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $signature = 'light:basic {model : model name} {chineseName}'; |
||
16 | |||
17 | /** |
||
18 | * The console command description. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $description = 'generate model related files, such as model controller, repository, request and so on'; |
||
23 | |||
24 | /** |
||
25 | * Create a new command instance. |
||
26 | * |
||
27 | * @return void |
||
28 | */ |
||
29 | public function __construct() |
||
30 | { |
||
31 | parent::__construct(); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Execute the console command. |
||
36 | * |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function handle() |
||
40 | { |
||
41 | $model = trim($this->argument('model')); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
42 | $modelChineseName = \mb_convert_encoding(trim($this->argument('chineseName')), 'utf-8', ['utf-8', 'gbk']); |
||
43 | $modelPlural = Str::plural($model); |
||
44 | $modelUCFirst = Str::ucfirst($model); |
||
45 | |||
46 | $template = file_get_contents(storage_path() . '/template/template.txt'); |
||
47 | |||
48 | $types = ['repository', 'controller', 'request', 'model', 'view', 'route']; |
||
49 | foreach ($types as $type) { |
||
50 | if (preg_match_all("%!start{$type}=(.+?\.php)(.+?)!end{$type}%s", $template, $match)) { |
||
51 | foreach ($match[1] as $k => $v) { |
||
52 | if ($type === 'view') { |
||
53 | $path = resource_path('views/admin/' . $model); |
||
54 | if (!is_dir($path)) { |
||
55 | mkdir($path); |
||
56 | } |
||
57 | $file = resource_path() . '/' . str_replace('{{-$model-}}', $model, $v); |
||
58 | } elseif ($type === 'route') { |
||
59 | $file = base_path('routes/auto/' . str_replace('{{-$model-}}', $model, $v)); |
||
60 | } else { |
||
61 | $file = app_path() . '/' . str_replace('{{-$model_uc_first-}}', $modelUCFirst, $v); |
||
62 | } |
||
63 | if (file_exists($file)) { |
||
64 | echo "{$file} 已存在" . PHP_EOL; |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | $content = ltrim(str_replace('{{-$model_uc_first-}}', $modelUCFirst, $match[2][$k])); |
||
69 | $content = str_replace('{{-$model_chinese_name-}}', $modelChineseName, $content); |
||
70 | $content = str_replace('{{-$model-}}', $model, $content); |
||
71 | $content = str_replace('{{-$model_plural-}}', $modelPlural, $content); |
||
72 | file_put_contents($file, $content); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | echo 'All files created successfully'; |
||
78 | return; |
||
79 | } |
||
80 | } |
||
81 |