Total Complexity | 60 |
Total Lines | 315 |
Duplicated Lines | 0 % |
Changes | 32 | ||
Bugs | 5 | Features | 2 |
Complex classes like LaravelSCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LaravelSCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class LaravelSCommand extends Command |
||
9 | { |
||
10 | protected $signature = 'laravels {action? : publish|config|info} |
||
11 | {--d|daemonize : Run as a daemon} |
||
12 | {--i|ignore : Ignore checking PID file of Master process} |
||
13 | {--x=|x-version= : The version(branch) of the current project, stored in $_ENV/$_SERVER}'; |
||
14 | |||
15 | protected $description = 'LaravelS console tool'; |
||
16 | |||
17 | public function fire() |
||
18 | { |
||
19 | $this->handle(); |
||
20 | } |
||
21 | |||
22 | public function handle() |
||
23 | { |
||
24 | $action = (string)$this->argument('action'); |
||
25 | switch ($action) { |
||
26 | case 'publish': |
||
27 | $this->publish(); |
||
28 | break; |
||
29 | case 'config': |
||
30 | $this->prepareConfig(); |
||
31 | case 'info': |
||
32 | $this->showInfo(); |
||
33 | break; |
||
34 | default: |
||
35 | $this->info(sprintf('Usage: [%s] ./artisan laravels publish|config|info', PHP_BINARY)); |
||
36 | if (in_array($action, ['start', 'stop', 'restart', 'reload'], true)) { |
||
37 | $this->error(sprintf( |
||
38 | 'The "%s" command has been migrated to "bin/laravels", %ssee https://github.com/hhxsv5/laravel-s#run', |
||
39 | $action, |
||
40 | file_exists(base_path('bin/laravels')) ? '' : 'please run `php artisan laravels publish` first, ' |
||
41 | )); |
||
42 | } |
||
43 | break; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | protected function isLumen() |
||
50 | } |
||
51 | |||
52 | protected function loadConfig() |
||
58 | } |
||
59 | } |
||
60 | |||
61 | protected function showInfo() |
||
62 | { |
||
63 | $this->showLogo(); |
||
64 | $this->showComponents(); |
||
65 | $this->showProtocols(); |
||
66 | $this->comment('>>> Feedback: <options=underscore>https://github.com/hhxsv5/laravel-s</>'); |
||
67 | } |
||
68 | |||
69 | protected function showLogo() |
||
82 | } |
||
83 | |||
84 | protected function showComponents() |
||
85 | { |
||
86 | $this->comment('>>> Components'); |
||
87 | $laravelSVersion = '-'; |
||
88 | $cfg = (array)json_decode(file_get_contents(base_path('composer.lock')), true); |
||
89 | if (isset($cfg['packages'])) { |
||
90 | $packages = array_merge($cfg['packages'], Arr::get($cfg, 'packages-dev', [])); |
||
91 | foreach ($packages as $package) { |
||
92 | if (isset($package['name']) && $package['name'] === 'hhxsv5/laravel-s') { |
||
93 | $laravelSVersion = ltrim($package['version'], 'vV'); |
||
94 | break; |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | $this->table(['Component', 'Version'], [ |
||
99 | [ |
||
100 | 'PHP', |
||
101 | phpversion(), |
||
102 | ], |
||
103 | [ |
||
104 | 'Swoole', |
||
105 | swoole_version(), |
||
106 | ], |
||
107 | [ |
||
108 | 'LaravelS', |
||
109 | $laravelSVersion, |
||
110 | ], |
||
111 | [ |
||
112 | $this->getApplication()->getName() . ' [<info>' . env('APP_ENV', config('app.env')) . '</info>]', |
||
113 | $this->getApplication()->getVersion(), |
||
114 | ], |
||
115 | ]); |
||
116 | } |
||
117 | |||
118 | protected function showProtocols() |
||
171 | } |
||
172 | |||
173 | protected function prepareConfig() |
||
174 | { |
||
175 | $this->loadConfig(); |
||
176 | |||
177 | $svrConf = config('laravels'); |
||
178 | |||
179 | $this->preSet($svrConf); |
||
180 | |||
181 | $ret = $this->preCheck($svrConf); |
||
182 | if ($ret !== 0) { |
||
183 | return $ret; |
||
184 | } |
||
185 | |||
186 | $laravelConf = [ |
||
187 | 'root_path' => $svrConf['laravel_base_path'], |
||
188 | 'static_path' => $svrConf['swoole']['document_root'], |
||
189 | 'cleaners' => array_unique((array)Arr::get($svrConf, 'cleaners', [])), |
||
190 | 'register_providers' => array_unique((array)Arr::get($svrConf, 'register_providers', [])), |
||
191 | 'destroy_controllers' => Arr::get($svrConf, 'destroy_controllers', []), |
||
192 | 'is_lumen' => $this->isLumen(), |
||
193 | '_SERVER' => $_SERVER, |
||
194 | '_ENV' => $_ENV, |
||
195 | ]; |
||
196 | |||
197 | $config = ['server' => $svrConf, 'laravel' => $laravelConf]; |
||
198 | return file_put_contents($this->getConfPath(), serialize($config)) > 0 ? 0 : 1; |
||
199 | } |
||
200 | |||
201 | protected function getConfPath() |
||
202 | { |
||
203 | return base_path('storage/laravels.conf'); |
||
204 | } |
||
205 | |||
206 | protected function preSet(array &$svrConf) |
||
207 | { |
||
208 | if (!isset($svrConf['enable_gzip'])) { |
||
209 | $svrConf['enable_gzip'] = false; |
||
210 | } |
||
211 | if (empty($svrConf['laravel_base_path'])) { |
||
212 | $svrConf['laravel_base_path'] = base_path(); |
||
213 | } |
||
214 | if (empty($svrConf['process_prefix'])) { |
||
215 | $svrConf['process_prefix'] = $svrConf['laravel_base_path']; |
||
216 | } |
||
217 | if ($this->option('ignore')) { |
||
218 | $svrConf['ignore_check_pid'] = true; |
||
219 | } elseif (!isset($svrConf['ignore_check_pid'])) { |
||
220 | $svrConf['ignore_check_pid'] = false; |
||
221 | } |
||
222 | if (empty($svrConf['swoole']['document_root'])) { |
||
223 | $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public'; |
||
224 | } |
||
225 | if ($this->option('daemonize')) { |
||
226 | $svrConf['swoole']['daemonize'] = true; |
||
227 | } elseif (!isset($svrConf['swoole']['daemonize'])) { |
||
228 | $svrConf['swoole']['daemonize'] = false; |
||
229 | } |
||
230 | if (empty($svrConf['swoole']['pid_file'])) { |
||
231 | $svrConf['swoole']['pid_file'] = storage_path('laravels.pid'); |
||
232 | } |
||
233 | if (empty($svrConf['timer']['max_wait_time'])) { |
||
234 | $svrConf['timer']['max_wait_time'] = 5; |
||
235 | } |
||
236 | |||
237 | // Set X-Version |
||
238 | $xVersion = (string)$this->option('x-version'); |
||
239 | if ($xVersion !== '') { |
||
240 | $_SERVER['X_VERSION'] = $_ENV['X_VERSION'] = $xVersion; |
||
241 | } |
||
242 | return 0; |
||
243 | } |
||
244 | |||
245 | protected function preCheck(array $svrConf) |
||
246 | { |
||
247 | if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) { |
||
248 | $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.'); |
||
249 | $this->info('If there is a proxy server like Nginx, suggest that enable gzip in Nginx and disable gzip in Swoole, to avoid the repeated gzip compression for response.'); |
||
250 | return 1; |
||
251 | } |
||
252 | if (!empty($svrConf['events'])) { |
||
253 | if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) { |
||
254 | $this->error('Asynchronous event listening needs to set task_worker_num > 0'); |
||
255 | return 1; |
||
256 | } |
||
257 | } |
||
258 | return 0; |
||
259 | } |
||
260 | |||
261 | |||
262 | public function publish() |
||
323 | } |
||
324 | } |
||
325 |