Total Complexity | 63 |
Total Lines | 315 |
Duplicated Lines | 0 % |
Changes | 33 | ||
Bugs | 6 | 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 = file_exists(base_path('composer.lock')) ? 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() |
||
204 | } |
||
205 | |||
206 | protected function preSet(array &$svrConf) |
||
243 | } |
||
244 | |||
245 | protected function preCheck(array $svrConf) |
||
259 | } |
||
260 | |||
261 | |||
262 | public function publish() |
||
323 | } |
||
324 | } |
||
325 |