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