Total Complexity | 56 |
Total Lines | 294 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
7 | class LaravelSCommand extends Command |
||
8 | { |
||
9 | protected $signature = 'laravels {action? : publish|config|info} |
||
10 | {--d|daemonize : Whether run as a daemon for "start & restart"} |
||
11 | {--i|ignore : Whether ignore checking process pid for "start & restart"}'; |
||
12 | |||
13 | protected $description = 'LaravelS console tool'; |
||
14 | |||
15 | public function fire() |
||
16 | { |
||
17 | $this->handle(); |
||
18 | } |
||
19 | |||
20 | public function handle() |
||
21 | { |
||
22 | $action = (string)$this->argument('action'); |
||
23 | switch ($action) { |
||
24 | case 'publish': |
||
25 | $this->publish(); |
||
26 | break; |
||
27 | case 'config': |
||
28 | $this->prepareConfig(); |
||
|
|||
29 | case 'info': |
||
30 | $this->showInfo(); |
||
31 | break; |
||
32 | default: |
||
33 | $this->info(sprintf('Usage: [%s] ./artisan laravels publish|config|info', PHP_BINARY)); |
||
34 | if (in_array($action, ['start', 'stop', 'restart', 'reload'], true)) { |
||
35 | $this->error(sprintf( |
||
36 | 'The "%s" command has been migrated to "bin/laravels", %ssee https://github.com/hhxsv5/laravel-s#run', |
||
37 | $action, |
||
38 | file_exists(base_path('bin/laravels')) ? '' : 'please run `php artisan laravels publish` first, ' |
||
39 | )); |
||
40 | } |
||
41 | break; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | protected function isLumen() |
||
48 | } |
||
49 | |||
50 | protected function loadConfig() |
||
51 | { |
||
52 | // Load configuration laravel.php manually for Lumen |
||
53 | $basePath = config('laravels.laravel_base_path') ?: base_path(); |
||
54 | if ($this->isLumen() && file_exists($basePath . '/config/laravels.php')) { |
||
55 | $this->getLaravel()->configure('laravels'); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | protected function showInfo() |
||
60 | { |
||
61 | $this->showLogo(); |
||
62 | $this->showComponents(); |
||
63 | $this->showDashboard(); |
||
64 | } |
||
65 | |||
66 | protected function showLogo() |
||
79 | } |
||
80 | |||
81 | protected function showComponents() |
||
82 | { |
||
83 | $this->comment('>>> Components'); |
||
84 | $laravelSVersion = '-'; |
||
85 | $cfg = (array)json_decode(file_get_contents(base_path('composer.lock')), true); |
||
86 | if (isset($cfg['packages'])) { |
||
87 | $packages = array_merge($cfg['packages'], array_get($cfg, 'packages-dev', [])); |
||
88 | foreach ($packages as $package) { |
||
89 | if (isset($package['name']) && $package['name'] === 'hhxsv5/laravel-s') { |
||
90 | $laravelSVersion = ltrim($package['version'], 'vV'); |
||
91 | break; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | $this->table(['Component', 'Version'], [ |
||
96 | [ |
||
97 | 'PHP', |
||
98 | phpversion(), |
||
99 | ], |
||
100 | [ |
||
101 | 'Swoole', |
||
102 | swoole_version(), |
||
103 | ], |
||
104 | [ |
||
105 | 'LaravelS', |
||
106 | $laravelSVersion, |
||
107 | ], |
||
108 | [ |
||
109 | $this->getApplication()->getName() . ' [<info>' . env('APP_ENV') . '</info>]', |
||
110 | $this->getApplication()->getVersion(), |
||
111 | ], |
||
112 | ]); |
||
113 | } |
||
114 | |||
115 | protected function showDashboard() |
||
116 | { |
||
117 | $this->comment('>>> Dashboard'); |
||
118 | |||
119 | $config = (array)json_decode(file_get_contents(base_path('storage/laravels.json')), true); |
||
120 | $socketType = isset($config['server']['socket_type']) ? $config['server']['socket_type'] : SWOOLE_SOCK_TCP; |
||
121 | if (in_array($socketType, [SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_UNIX_STREAM])) { |
||
122 | $listenAt = $config['server']['listen_ip']; |
||
123 | } else { |
||
124 | $listenAt = sprintf('%s:%s', $config['server']['listen_ip'], $config['server']['listen_port']); |
||
125 | } |
||
126 | |||
127 | $tableRows = [ |
||
128 | [ |
||
129 | 'Main HTTP', |
||
130 | '<info>On</info>', |
||
131 | $this->getApplication()->getName(), |
||
132 | $listenAt, |
||
133 | ], |
||
134 | ]; |
||
135 | if (!empty($config['server']['websocket']['enable'])) { |
||
136 | $tableRows [] = [ |
||
137 | 'Main WebSocket', |
||
138 | '<info>On</info>', |
||
139 | $config['server']['websocket']['handler'], |
||
140 | $listenAt, |
||
141 | ]; |
||
142 | } |
||
143 | |||
144 | $socketTypeNames = [ |
||
145 | SWOOLE_SOCK_TCP => 'TCP IPV4 Socket', |
||
146 | SWOOLE_SOCK_TCP6 => 'TCP IPV6 Socket', |
||
147 | SWOOLE_SOCK_UDP => 'UDP IPV4 Socket', |
||
148 | SWOOLE_SOCK_UDP6 => 'TCP IPV6 Socket', |
||
149 | SWOOLE_SOCK_UNIX_DGRAM => 'Unix Socket Dgram', |
||
150 | SWOOLE_SOCK_UNIX_STREAM => 'Unix Socket Stream', |
||
151 | ]; |
||
152 | $sockets = isset($config['server']['sockets']) ? $config['server']['sockets'] : []; |
||
153 | foreach ($sockets as $key => $socket) { |
||
154 | $name = 'Port#' . $key . ' '; |
||
155 | $name .= isset($socketTypeNames[$socket['type']]) ? $socketTypeNames[$socket['type']] : 'Unknown socket'; |
||
156 | $tableRows [] = [ |
||
157 | $name, |
||
158 | '<info>On</info>', |
||
159 | $socket['handler'], |
||
160 | sprintf('%s:%s', $socket['host'], $socket['port']), |
||
161 | ]; |
||
162 | } |
||
163 | $this->table(['Protocol', 'Status', 'Handler', 'Listen At'], $tableRows); |
||
164 | |||
165 | $this->comment('>>> Feedback: <options=underscore>https://github.com/hhxsv5/laravel-s</>'); |
||
166 | } |
||
167 | |||
168 | protected function prepareConfig() |
||
193 | } |
||
194 | |||
195 | protected function preSet(array &$svrConf) |
||
196 | { |
||
197 | if (!isset($svrConf['enable_gzip'])) { |
||
198 | $svrConf['enable_gzip'] = false; |
||
199 | } |
||
200 | if (empty($svrConf['laravel_base_path'])) { |
||
201 | $svrConf['laravel_base_path'] = base_path(); |
||
202 | } |
||
203 | if (empty($svrConf['process_prefix'])) { |
||
204 | $svrConf['process_prefix'] = $svrConf['laravel_base_path']; |
||
205 | } |
||
206 | if ($this->option('ignore')) { |
||
207 | $svrConf['ignore_check_pid'] = true; |
||
208 | } elseif (!isset($svrConf['ignore_check_pid'])) { |
||
209 | $svrConf['ignore_check_pid'] = false; |
||
210 | } |
||
211 | if (empty($svrConf['swoole']['document_root'])) { |
||
212 | $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public'; |
||
213 | } |
||
214 | if ($this->option('daemonize')) { |
||
215 | $svrConf['swoole']['daemonize'] = true; |
||
216 | } elseif (!isset($svrConf['swoole']['daemonize'])) { |
||
217 | $svrConf['swoole']['daemonize'] = false; |
||
218 | } |
||
219 | if (empty($svrConf['swoole']['pid_file'])) { |
||
220 | $svrConf['swoole']['pid_file'] = storage_path('laravels.pid'); |
||
221 | } |
||
222 | if (empty($svrConf['timer']['pid_file'])) { |
||
223 | $svrConf['timer']['pid_file'] = storage_path('laravels-timer.pid'); |
||
224 | } |
||
225 | return 0; |
||
226 | } |
||
227 | |||
228 | protected function preCheck(array $svrConf) |
||
229 | { |
||
230 | if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) { |
||
231 | $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.'); |
||
232 | $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.'); |
||
233 | return 1; |
||
234 | } |
||
235 | if (!empty($svrConf['events'])) { |
||
236 | if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) { |
||
237 | $this->error('Asynchronous event listening needs to set task_worker_num > 0'); |
||
238 | return 1; |
||
239 | } |
||
240 | } |
||
241 | return 0; |
||
242 | } |
||
243 | |||
244 | |||
245 | public function publish() |
||
301 | } |
||
302 | } |
||
303 |