| Total Complexity | 52 |
| Total Lines | 251 |
| 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 |
||
| 8 | class LaravelSCommand extends Command |
||
| 9 | { |
||
| 10 | protected $signature = 'laravels'; |
||
| 11 | |||
| 12 | protected $description = 'LaravelS console tool'; |
||
| 13 | |||
| 14 | protected $actions; |
||
| 15 | |||
| 16 | protected $isLumen = false; |
||
| 17 | |||
| 18 | public function __construct() |
||
| 19 | { |
||
| 20 | $this->actions = ['start', 'stop', 'restart', 'reload', 'publish']; |
||
| 21 | $actions = implode('|', $this->actions); |
||
| 22 | $this->signature .= sprintf(' {action : %s} {--d|daemonize : Whether run as a daemon for start & restart}', $actions); |
||
| 23 | $this->description .= ': ' . $actions; |
||
| 24 | |||
| 25 | parent::__construct(); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function fire() |
||
| 31 | } |
||
| 32 | |||
| 33 | public function handle() |
||
| 34 | { |
||
| 35 | $action = (string)$this->argument('action'); |
||
| 36 | if (!in_array($action, $this->actions, true)) { |
||
| 37 | $this->warn(sprintf('LaravelS: action %s is not available, only support %s', $action, implode('|', $this->actions))); |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | $this->isLumen = stripos($this->getApplication()->getVersion(), 'Lumen') !== false; |
||
| 42 | $this->loadConfigManually(); |
||
| 43 | $this->{$action}(); |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function loadConfigManually() |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | protected function outputLogo() |
||
| 72 | ]); |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function start() |
||
| 76 | { |
||
| 77 | $this->outputLogo(); |
||
| 78 | |||
| 79 | $svrConf = config('laravels'); |
||
| 80 | $basePath = array_get($svrConf, 'laravel_base_path', base_path()); |
||
| 81 | |||
| 82 | if (empty($svrConf['swoole']['document_root'])) { |
||
| 83 | $svrConf['swoole']['document_root'] = $basePath . '/public'; |
||
| 84 | } |
||
| 85 | if (empty($svrConf['process_prefix'])) { |
||
| 86 | $svrConf['process_prefix'] = $basePath; |
||
| 87 | } |
||
| 88 | if (!empty($svrConf['events'])) { |
||
| 89 | if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) { |
||
| 90 | $this->error('LaravelS: Asynchronous event listening needs to set task_worker_num > 0'); |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | if ($this->option('daemonize')) { |
||
| 95 | $svrConf['swoole']['daemonize'] = true; |
||
| 96 | } |
||
| 97 | |||
| 98 | $laravelConf = [ |
||
| 99 | 'root_path' => $basePath, |
||
| 100 | 'static_path' => $svrConf['swoole']['document_root'], |
||
| 101 | 'register_providers' => array_unique((array)array_get($svrConf, 'register_providers', [])), |
||
| 102 | 'is_lumen' => $this->isLumen, |
||
| 103 | '_SERVER' => $_SERVER, |
||
| 104 | '_ENV' => $_ENV, |
||
| 105 | ]; |
||
| 106 | |||
| 107 | if (file_exists($svrConf['swoole']['pid_file'])) { |
||
| 108 | $pid = (int)file_get_contents($svrConf['swoole']['pid_file']); |
||
| 109 | if ($pid > 0 && $this->killProcess($pid, 0)) { |
||
| 110 | $this->warn(sprintf('LaravelS: PID[%s] is already running at %s:%s.', $pid, $svrConf['listen_ip'], $svrConf['listen_port'])); |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // Implements gracefully reload, avoid including laravel's files before worker start |
||
| 116 | $cmd = sprintf('%s %s/../GoLaravelS.php', PHP_BINARY, __DIR__); |
||
| 117 | $ret = $this->popen($cmd, json_encode(compact('svrConf', 'laravelConf'))); |
||
| 118 | if ($ret === false) { |
||
| 119 | $this->error('LaravelS: popen ' . $cmd . ' failed'); |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | $pidFile = empty($svrConf['swoole']['pid_file']) ? storage_path('laravels.pid') : $svrConf['swoole']['pid_file']; |
||
| 124 | |||
| 125 | // Make sure that master process started |
||
| 126 | $time = 0; |
||
| 127 | while (!file_exists($pidFile) && $time <= 20) { |
||
| 128 | usleep(100000); |
||
| 129 | $time++; |
||
| 130 | } |
||
| 131 | if (file_exists($pidFile)) { |
||
| 132 | $this->info(sprintf('LaravelS: PID[%s] is running at %s:%s.', file_get_contents($pidFile), $svrConf['listen_ip'], $svrConf['listen_port'])); |
||
| 133 | } else { |
||
| 134 | $this->error(sprintf('LaravelS: PID file[%s] does not exist.', $pidFile)); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | protected function popen($cmd, $input = null) |
||
| 139 | { |
||
| 140 | $fp = popen($cmd, 'w'); |
||
| 141 | if ($fp === false) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | if ($input !== null) { |
||
| 145 | fwrite($fp, $input); |
||
| 146 | } |
||
| 147 | pclose($fp); |
||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | protected function stop() |
||
| 152 | { |
||
| 153 | $pidFile = config('laravels.swoole.pid_file') ?: storage_path('laravels.pid'); |
||
| 154 | if (file_exists($pidFile)) { |
||
| 155 | $pid = (int)file_get_contents($pidFile); |
||
| 156 | if ($this->killProcess($pid, 0)) { |
||
| 157 | if ($this->killProcess($pid, SIGTERM)) { |
||
| 158 | // Make sure that master process quit |
||
| 159 | $time = 0; |
||
| 160 | while ($this->killProcess($pid, 0) && $time <= 20) { |
||
| 161 | usleep(100000); |
||
| 162 | $this->killProcess($pid, SIGTERM); |
||
| 163 | $time++; |
||
| 164 | } |
||
| 165 | if (file_exists($pidFile)) { |
||
| 166 | unlink($pidFile); |
||
| 167 | } |
||
| 168 | $this->info("LaravelS: PID[{$pid}] is stopped."); |
||
| 169 | } else { |
||
| 170 | $this->error("LaravelS: PID[{$pid}] is stopped failed."); |
||
| 171 | } |
||
| 172 | } else { |
||
| 173 | $this->warn("LaravelS: PID[{$pid}] does not exist, or permission denied."); |
||
| 174 | if (file_exists($pidFile)) { |
||
| 175 | unlink($pidFile); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } else { |
||
| 179 | $this->info('LaravelS: already stopped.'); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | protected function restart() |
||
| 184 | { |
||
| 185 | $this->stop(); |
||
| 186 | $this->start(); |
||
| 187 | } |
||
| 188 | |||
| 189 | protected function reload() |
||
| 190 | { |
||
| 191 | $pidFile = config('laravels.swoole.pid_file') ?: storage_path('laravels.pid'); |
||
| 192 | if (!file_exists($pidFile)) { |
||
| 193 | $this->error('LaravelS: it seems that LaravelS is not running.'); |
||
| 194 | return; |
||
| 195 | } |
||
| 196 | |||
| 197 | $pid = (int)file_get_contents($pidFile); |
||
| 198 | if (!$this->killProcess($pid, 0)) { |
||
| 199 | $this->error("LaravelS: PID[{$pid}] does not exist, or permission denied."); |
||
| 200 | return; |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($this->killProcess($pid, SIGUSR1)) { |
||
| 204 | $this->info("LaravelS: PID[{$pid}] is reloaded."); |
||
| 205 | } else { |
||
| 206 | $this->error("LaravelS: PID[{$pid}] is reloaded failed."); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | protected function publish() |
||
| 251 | } |
||
| 252 | |||
| 253 | protected function killProcess($pid, $sig) |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.