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