Total Complexity | 73 |
Total Lines | 330 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 2 | Features | 1 |
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 |
||
11 | class LaravelSCommand extends Command |
||
12 | { |
||
13 | protected $signature = 'laravels {action? : publish|config|info} |
||
14 | {--d|daemonize : Run as a daemon} |
||
15 | {--i|ignore : Ignore checking PID file of Master process} |
||
16 | {--x=|x-version= : The version(branch) of the current project, stored in $_ENV/$_SERVER}'; |
||
17 | |||
18 | protected $description = 'LaravelS console tool'; |
||
19 | |||
20 | public function fire() |
||
21 | { |
||
22 | $this->handle(); |
||
23 | } |
||
24 | |||
25 | public function handle() |
||
26 | { |
||
27 | $action = (string)$this->argument('action'); |
||
28 | switch ($action) { |
||
29 | case 'publish': |
||
30 | $this->publish(); |
||
31 | break; |
||
32 | case 'config': |
||
33 | case 'info': |
||
34 | $this->prepareConfig(); |
||
35 | $this->showInfo(); |
||
36 | break; |
||
37 | default: |
||
38 | $this->info(sprintf('Usage: [%s] ./artisan laravels publish|config|info', PHP_BINARY)); |
||
39 | if (in_array($action, ['start', 'stop', 'restart', 'reload'], true)) { |
||
40 | $this->error(sprintf( |
||
41 | 'The "%s" command has been migrated to "bin/laravels", %ssee https://github.com/hhxsv5/laravel-s#run', |
||
42 | $action, |
||
43 | file_exists(base_path('bin/laravels')) ? '' : 'please run `php artisan laravels publish` first, ' |
||
44 | )); |
||
45 | } |
||
46 | break; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | protected function isLumen() |
||
51 | { |
||
52 | return stripos($this->getApplication()->getVersion(), 'Lumen') !== false; |
||
53 | } |
||
54 | |||
55 | protected function loadConfig() |
||
56 | { |
||
57 | // Load configuration laravel.php manually for Lumen |
||
58 | $basePath = config('laravels.laravel_base_path') ?: base_path(); |
||
59 | if ($this->isLumen() && file_exists($basePath . '/config/laravels.php')) { |
||
60 | $this->getLaravel()->configure('laravels'); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | protected function showInfo() |
||
65 | { |
||
66 | $this->showLogo(); |
||
67 | $this->showComponents(); |
||
68 | $this->showProtocols(); |
||
69 | $this->comment('>>> Feedback: <options=underscore>https://github.com/hhxsv5/laravel-s</>'); |
||
70 | } |
||
71 | |||
72 | protected function showLogo() |
||
73 | { |
||
74 | static $logo = <<<EOS |
||
75 | _ _ _____ |
||
76 | | | | |/ ____| |
||
77 | | | __ _ _ __ __ ___ _____| | (___ |
||
78 | | | / _` | '__/ _` \ \ / / _ \ |\___ \ |
||
79 | | |___| (_| | | | (_| |\ V / __/ |____) | |
||
80 | |______\__,_|_| \__,_| \_/ \___|_|_____/ |
||
81 | |||
82 | EOS; |
||
83 | $this->info($logo); |
||
84 | $this->info('Speed up your Laravel/Lumen'); |
||
85 | } |
||
86 | |||
87 | protected function showComponents() |
||
88 | { |
||
89 | $this->comment('>>> Components'); |
||
90 | $laravelSVersion = '-'; |
||
91 | $lockFile = base_path('composer.lock'); |
||
92 | $cfg = file_exists($lockFile) ? json_decode(file_get_contents($lockFile), true) : []; |
||
93 | if (isset($cfg['packages'])) { |
||
94 | $packages = array_merge($cfg['packages'], Arr::get($cfg, 'packages-dev', [])); |
||
95 | foreach ($packages as $package) { |
||
96 | if (isset($package['name']) && $package['name'] === 'hhxsv5/laravel-s') { |
||
97 | $laravelSVersion = ltrim($package['version'], 'vV'); |
||
98 | break; |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | $this->table(['Component', 'Version'], [ |
||
103 | [ |
||
104 | 'PHP', |
||
105 | PHP_VERSION, |
||
106 | ], |
||
107 | [ |
||
108 | extension_loaded('openswoole') ? 'Open Swoole' : 'Swoole', |
||
109 | SWOOLE_VERSION, |
||
110 | ], |
||
111 | [ |
||
112 | 'LaravelS', |
||
113 | $laravelSVersion, |
||
114 | ], |
||
115 | [ |
||
116 | $this->getApplication()->getName() . ' [<info>' . env('APP_ENV', config('app.env')) . '</info>]', |
||
117 | $this->getApplication()->getVersion(), |
||
118 | ], |
||
119 | ]); |
||
120 | } |
||
121 | |||
122 | protected function showProtocols() |
||
123 | { |
||
124 | $this->comment('>>> Protocols'); |
||
125 | |||
126 | $config = unserialize((string)file_get_contents($this->getConfigPath())); |
||
127 | $ssl = isset($config['server']['swoole']['ssl_key_file'], $config['server']['swoole']['ssl_cert_file']); |
||
128 | $socketType = isset($config['server']['socket_type']) ? $config['server']['socket_type'] : SWOOLE_SOCK_TCP; |
||
129 | if (in_array($socketType, [SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_UNIX_STREAM])) { |
||
130 | $listenAt = $config['server']['listen_ip']; |
||
131 | } else { |
||
132 | $listenAt = sprintf('%s:%s', $config['server']['listen_ip'], $config['server']['listen_port']); |
||
133 | } |
||
134 | |||
135 | $tableRows = [ |
||
136 | [ |
||
137 | 'Main HTTP', |
||
138 | '<info>On</info>', |
||
139 | $this->getApplication()->getName(), |
||
140 | sprintf('%s://%s', $ssl ? 'https' : 'http', $listenAt), |
||
141 | ], |
||
142 | ]; |
||
143 | if (!empty($config['server']['websocket']['enable'])) { |
||
144 | $tableRows [] = [ |
||
145 | 'Main WebSocket', |
||
146 | '<info>On</info>', |
||
147 | $config['server']['websocket']['handler'], |
||
148 | sprintf('%s://%s', $ssl ? 'wss' : 'ws', $listenAt), |
||
149 | ]; |
||
150 | } |
||
151 | |||
152 | $socketTypeNames = [ |
||
153 | SWOOLE_SOCK_TCP => 'TCP IPV4 Socket', |
||
154 | SWOOLE_SOCK_TCP6 => 'TCP IPV6 Socket', |
||
155 | SWOOLE_SOCK_UDP => 'UDP IPV4 Socket', |
||
156 | SWOOLE_SOCK_UDP6 => 'TCP IPV6 Socket', |
||
157 | SWOOLE_SOCK_UNIX_DGRAM => 'Unix Socket Dgram', |
||
158 | SWOOLE_SOCK_UNIX_STREAM => 'Unix Socket Stream', |
||
159 | ]; |
||
160 | $sockets = isset($config['server']['sockets']) ? $config['server']['sockets'] : []; |
||
161 | foreach ($sockets as $key => $socket) { |
||
162 | if (isset($socket['enable']) && !$socket['enable']) { |
||
163 | continue; |
||
164 | } |
||
165 | |||
166 | $name = 'Port#' . $key . ' '; |
||
167 | $name .= isset($socketTypeNames[$socket['type']]) ? $socketTypeNames[$socket['type']] : 'Unknown socket'; |
||
168 | $tableRows [] = [ |
||
169 | $name, |
||
170 | '<info>On</info>', |
||
171 | $socket['handler'], |
||
172 | sprintf('%s:%s', $socket['host'], $socket['port']), |
||
173 | ]; |
||
174 | } |
||
175 | $this->table(['Protocol', 'Status', 'Handler', 'Listen At'], $tableRows); |
||
176 | } |
||
177 | |||
178 | protected function prepareConfig() |
||
179 | { |
||
180 | $this->loadConfig(); |
||
181 | |||
182 | $svrConf = config('laravels'); |
||
183 | |||
184 | $this->preSet($svrConf); |
||
185 | |||
186 | $ret = $this->preCheck($svrConf); |
||
187 | if ($ret !== 0) { |
||
188 | return $ret; |
||
189 | } |
||
190 | |||
191 | // Fixed $_ENV['APP_ENV'] |
||
192 | if (isset($_SERVER['APP_ENV'])) { |
||
193 | $_ENV['APP_ENV'] = $_SERVER['APP_ENV']; |
||
194 | } |
||
195 | |||
196 | $laravelConf = [ |
||
197 | 'root_path' => $svrConf['laravel_base_path'], |
||
198 | 'static_path' => $svrConf['swoole']['document_root'], |
||
199 | 'cleaners' => array_unique((array)Arr::get($svrConf, 'cleaners', [])), |
||
200 | 'register_providers' => array_unique((array)Arr::get($svrConf, 'register_providers', [])), |
||
201 | 'destroy_controllers' => Arr::get($svrConf, 'destroy_controllers', []), |
||
202 | 'is_lumen' => $this->isLumen(), |
||
203 | '_SERVER' => $_SERVER, |
||
204 | '_ENV' => $_ENV, |
||
205 | ]; |
||
206 | |||
207 | $config = ['server' => $svrConf, 'laravel' => $laravelConf]; |
||
208 | return file_put_contents($this->getConfigPath(), serialize($config)) > 0 ? 0 : 1; |
||
209 | } |
||
210 | |||
211 | protected function getConfigPath() |
||
212 | { |
||
213 | return storage_path('laravels.conf'); |
||
214 | } |
||
215 | |||
216 | protected function preSet(array &$svrConf) |
||
217 | { |
||
218 | if (!isset($svrConf['enable_gzip'])) { |
||
219 | $svrConf['enable_gzip'] = false; |
||
220 | } |
||
221 | if (empty($svrConf['laravel_base_path'])) { |
||
222 | $svrConf['laravel_base_path'] = base_path(); |
||
223 | } |
||
224 | if (empty($svrConf['process_prefix'])) { |
||
225 | $svrConf['process_prefix'] = $svrConf['laravel_base_path']; |
||
226 | } |
||
227 | if ($this->option('ignore')) { |
||
228 | $svrConf['ignore_check_pid'] = true; |
||
229 | } elseif (!isset($svrConf['ignore_check_pid'])) { |
||
230 | $svrConf['ignore_check_pid'] = false; |
||
231 | } |
||
232 | if (empty($svrConf['swoole']['document_root'])) { |
||
233 | $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public'; |
||
234 | } |
||
235 | if ($this->option('daemonize')) { |
||
236 | $svrConf['swoole']['daemonize'] = true; |
||
237 | } elseif (!isset($svrConf['swoole']['daemonize'])) { |
||
238 | $svrConf['swoole']['daemonize'] = false; |
||
239 | } |
||
240 | if (empty($svrConf['swoole']['pid_file'])) { |
||
241 | $svrConf['swoole']['pid_file'] = storage_path('laravels.pid'); |
||
242 | } |
||
243 | if (empty($svrConf['timer']['max_wait_time'])) { |
||
244 | $svrConf['timer']['max_wait_time'] = 5; |
||
245 | } |
||
246 | |||
247 | // Configure PrometheusTimerProcessMetricsCronJob automatically |
||
248 | if (isset($svrConf['processes']) && !empty($svrConf['timer']['enable'])) { |
||
249 | foreach ($svrConf['processes'] as $process) { |
||
250 | if ($process['class'] === CollectorProcess::class && (!isset($process['enable']) || $process['enable'])) { |
||
251 | $svrConf['timer']['jobs'][] = TimerProcessMetricsCronJob::class; |
||
252 | break; |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // Set X-Version |
||
258 | $xVersion = (string)$this->option('x-version'); |
||
259 | if ($xVersion !== '') { |
||
260 | $_SERVER['X_VERSION'] = $_ENV['X_VERSION'] = $xVersion; |
||
261 | } |
||
262 | return 0; |
||
263 | } |
||
264 | |||
265 | protected function preCheck(array $svrConf) |
||
266 | { |
||
267 | if (!empty($svrConf['enable_gzip']) && version_compare(SWOOLE_VERSION, '4.1.0', '>=')) { |
||
268 | $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.'); |
||
269 | $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.'); |
||
270 | return 1; |
||
271 | } |
||
272 | if (!empty($svrConf['events'])) { |
||
273 | if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) { |
||
274 | $this->error('Asynchronous event listening needs to set task_worker_num > 0'); |
||
275 | return 1; |
||
276 | } |
||
277 | } |
||
278 | return 0; |
||
279 | } |
||
280 | |||
281 | |||
282 | public function publish() |
||
341 | } |
||
342 | } |
||
343 |