| Total Complexity | 65 |
| Total Lines | 317 |
| 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 | case 'info': |
||
| 31 | $this->prepareConfig(); |
||
| 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 | $lockFile = base_path('composer.lock'); |
||
| 89 | $cfg = file_exists($lockFile) ? json_decode(file_get_contents($lockFile), true) : []; |
||
| 90 | if (isset($cfg['packages'])) { |
||
| 91 | $packages = array_merge($cfg['packages'], Arr::get($cfg, 'packages-dev', [])); |
||
| 92 | foreach ($packages as $package) { |
||
| 93 | if (isset($package['name']) && $package['name'] === 'hhxsv5/laravel-s') { |
||
| 94 | $laravelSVersion = ltrim($package['version'], 'vV'); |
||
| 95 | break; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $this->table(['Component', 'Version'], [ |
||
| 100 | [ |
||
| 101 | 'PHP', |
||
| 102 | PHP_VERSION, |
||
| 103 | ], |
||
| 104 | [ |
||
| 105 | 'Swoole', |
||
| 106 | swoole_version(), |
||
| 107 | ], |
||
| 108 | [ |
||
| 109 | 'LaravelS', |
||
| 110 | $laravelSVersion, |
||
| 111 | ], |
||
| 112 | [ |
||
| 113 | $this->getApplication()->getName() . ' [<info>' . env('APP_ENV', config('app.env')) . '</info>]', |
||
| 114 | $this->getApplication()->getVersion(), |
||
| 115 | ], |
||
| 116 | ]); |
||
| 117 | } |
||
| 118 | |||
| 119 | protected function showProtocols() |
||
| 120 | { |
||
| 121 | $this->comment('>>> Protocols'); |
||
| 122 | |||
| 123 | $config = unserialize((string)file_get_contents($this->getConfigPath())); |
||
| 124 | $ssl = isset($config['server']['swoole']['ssl_key_file'], $config['server']['swoole']['ssl_cert_file']); |
||
| 125 | $socketType = isset($config['server']['socket_type']) ? $config['server']['socket_type'] : SWOOLE_SOCK_TCP; |
||
| 126 | if (in_array($socketType, [SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_UNIX_STREAM])) { |
||
| 127 | $listenAt = $config['server']['listen_ip']; |
||
| 128 | } else { |
||
| 129 | $listenAt = sprintf('%s:%s', $config['server']['listen_ip'], $config['server']['listen_port']); |
||
| 130 | } |
||
| 131 | |||
| 132 | $tableRows = [ |
||
| 133 | [ |
||
| 134 | 'Main HTTP', |
||
| 135 | '<info>On</info>', |
||
| 136 | $this->getApplication()->getName(), |
||
| 137 | sprintf('%s://%s', $ssl ? 'https' : 'http', $listenAt), |
||
| 138 | ], |
||
| 139 | ]; |
||
| 140 | if (!empty($config['server']['websocket']['enable'])) { |
||
| 141 | $tableRows [] = [ |
||
| 142 | 'Main WebSocket', |
||
| 143 | '<info>On</info>', |
||
| 144 | $config['server']['websocket']['handler'], |
||
| 145 | sprintf('%s://%s', $ssl ? 'wss' : 'ws', $listenAt), |
||
| 146 | ]; |
||
| 147 | } |
||
| 148 | |||
| 149 | $socketTypeNames = [ |
||
| 150 | SWOOLE_SOCK_TCP => 'TCP IPV4 Socket', |
||
| 151 | SWOOLE_SOCK_TCP6 => 'TCP IPV6 Socket', |
||
| 152 | SWOOLE_SOCK_UDP => 'UDP IPV4 Socket', |
||
| 153 | SWOOLE_SOCK_UDP6 => 'TCP IPV6 Socket', |
||
| 154 | SWOOLE_SOCK_UNIX_DGRAM => 'Unix Socket Dgram', |
||
| 155 | SWOOLE_SOCK_UNIX_STREAM => 'Unix Socket Stream', |
||
| 156 | ]; |
||
| 157 | $sockets = isset($config['server']['sockets']) ? $config['server']['sockets'] : []; |
||
| 158 | foreach ($sockets as $key => $socket) { |
||
| 159 | if (isset($socket['enable']) && !$socket['enable']) { |
||
| 160 | continue; |
||
| 161 | } |
||
| 162 | |||
| 163 | $name = 'Port#' . $key . ' '; |
||
| 164 | $name .= isset($socketTypeNames[$socket['type']]) ? $socketTypeNames[$socket['type']] : 'Unknown socket'; |
||
| 165 | $tableRows [] = [ |
||
| 166 | $name, |
||
| 167 | '<info>On</info>', |
||
| 168 | $socket['handler'], |
||
| 169 | sprintf('%s:%s', $socket['host'], $socket['port']), |
||
| 170 | ]; |
||
| 171 | } |
||
| 172 | $this->table(['Protocol', 'Status', 'Handler', 'Listen At'], $tableRows); |
||
| 173 | } |
||
| 174 | |||
| 175 | protected function prepareConfig() |
||
| 176 | { |
||
| 177 | $this->loadConfig(); |
||
| 178 | |||
| 179 | $svrConf = config('laravels'); |
||
| 180 | |||
| 181 | $this->preSet($svrConf); |
||
| 182 | |||
| 183 | $ret = $this->preCheck($svrConf); |
||
| 184 | if ($ret !== 0) { |
||
| 185 | return $ret; |
||
| 186 | } |
||
| 187 | |||
| 188 | $laravelConf = [ |
||
| 189 | 'root_path' => $svrConf['laravel_base_path'], |
||
| 190 | 'static_path' => $svrConf['swoole']['document_root'], |
||
| 191 | 'cleaners' => array_unique((array)Arr::get($svrConf, 'cleaners', [])), |
||
| 192 | 'register_providers' => array_unique((array)Arr::get($svrConf, 'register_providers', [])), |
||
| 193 | 'destroy_controllers' => Arr::get($svrConf, 'destroy_controllers', []), |
||
| 194 | 'is_lumen' => $this->isLumen(), |
||
| 195 | '_SERVER' => $_SERVER, |
||
| 196 | '_ENV' => $_ENV, |
||
| 197 | ]; |
||
| 198 | |||
| 199 | $config = ['server' => $svrConf, 'laravel' => $laravelConf]; |
||
| 200 | return file_put_contents($this->getConfigPath(), serialize($config)) > 0 ? 0 : 1; |
||
| 201 | } |
||
| 202 | |||
| 203 | protected function getConfigPath() |
||
| 204 | { |
||
| 205 | return storage_path('laravels.conf'); |
||
| 206 | } |
||
| 207 | |||
| 208 | protected function preSet(array &$svrConf) |
||
| 209 | { |
||
| 210 | if (!isset($svrConf['enable_gzip'])) { |
||
| 211 | $svrConf['enable_gzip'] = false; |
||
| 212 | } |
||
| 213 | if (empty($svrConf['laravel_base_path'])) { |
||
| 214 | $svrConf['laravel_base_path'] = base_path(); |
||
| 215 | } |
||
| 216 | if (empty($svrConf['process_prefix'])) { |
||
| 217 | $svrConf['process_prefix'] = $svrConf['laravel_base_path']; |
||
| 218 | } |
||
| 219 | if ($this->option('ignore')) { |
||
| 220 | $svrConf['ignore_check_pid'] = true; |
||
| 221 | } elseif (!isset($svrConf['ignore_check_pid'])) { |
||
| 222 | $svrConf['ignore_check_pid'] = false; |
||
| 223 | } |
||
| 224 | if (empty($svrConf['swoole']['document_root'])) { |
||
| 225 | $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public'; |
||
| 226 | } |
||
| 227 | if ($this->option('daemonize')) { |
||
| 228 | $svrConf['swoole']['daemonize'] = true; |
||
| 229 | } elseif (!isset($svrConf['swoole']['daemonize'])) { |
||
| 230 | $svrConf['swoole']['daemonize'] = false; |
||
| 231 | } |
||
| 232 | if (empty($svrConf['swoole']['pid_file'])) { |
||
| 233 | $svrConf['swoole']['pid_file'] = storage_path('laravels.pid'); |
||
| 234 | } |
||
| 235 | if (empty($svrConf['timer']['max_wait_time'])) { |
||
| 236 | $svrConf['timer']['max_wait_time'] = 5; |
||
| 237 | } |
||
| 238 | |||
| 239 | // Set X-Version |
||
| 240 | $xVersion = (string)$this->option('x-version'); |
||
| 241 | if ($xVersion !== '') { |
||
| 242 | $_SERVER['X_VERSION'] = $_ENV['X_VERSION'] = $xVersion; |
||
| 243 | } |
||
| 244 | return 0; |
||
| 245 | } |
||
| 246 | |||
| 247 | protected function preCheck(array $svrConf) |
||
| 248 | { |
||
| 249 | if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) { |
||
| 250 | $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.'); |
||
| 251 | $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.'); |
||
| 252 | return 1; |
||
| 253 | } |
||
| 254 | if (!empty($svrConf['events'])) { |
||
| 255 | if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) { |
||
| 256 | $this->error('Asynchronous event listening needs to set task_worker_num > 0'); |
||
| 257 | return 1; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | return 0; |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | public function publish() |
||
| 325 | } |
||
| 326 | } |
||
| 327 |