| Total Complexity | 53 |
| Total Lines | 286 |
| Duplicated Lines | 0 % |
| Changes | 17 | ||
| Bugs | 5 | Features | 2 |
Complex classes like Portal 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 Portal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Portal extends Command |
||
| 16 | { |
||
| 17 | use LogTrait; |
||
| 18 | |||
| 19 | |||
| 20 | /**@var string */ |
||
| 21 | protected $basePath; |
||
| 22 | |||
| 23 | /**@var InputInterface */ |
||
| 24 | protected $input; |
||
| 25 | |||
| 26 | /**@var OutputInterface */ |
||
| 27 | protected $output; |
||
| 28 | |||
| 29 | public function __construct($basePath) |
||
| 33 | } |
||
| 34 | |||
| 35 | protected function configure() |
||
| 36 | { |
||
| 37 | $this->setDescription('LaravelS console tool'); |
||
| 38 | $this->setHelp('LaravelS console tool'); |
||
| 39 | |||
| 40 | $this->addArgument('action', InputArgument::OPTIONAL, 'start|stop|restart|reload|info|help', 'help'); |
||
| 41 | $this->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'The environment the command should run under, this feature requires Laravel 5.2+'); |
||
| 42 | $this->addOption('daemonize', 'd', InputOption::VALUE_NONE, 'Whether run as a daemon for "start & restart"'); |
||
| 43 | $this->addOption('ignore', 'i', InputOption::VALUE_NONE, 'Whether ignore checking process pid for "start & restart"'); |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 47 | { |
||
| 48 | $this->input = $input; |
||
| 49 | $this->output = $output; |
||
| 50 | LaravelS::setOutputStyle(new SymfonyStyle($this->input, $this->output)); |
||
| 51 | |||
| 52 | try { |
||
| 53 | $action = $input->getArgument('action'); |
||
| 54 | switch ($action) { |
||
| 55 | case 'start': |
||
| 56 | $this->start(); |
||
| 57 | break; |
||
| 58 | case 'stop': |
||
| 59 | $this->stop(); |
||
| 60 | break; |
||
| 61 | case 'restart': |
||
| 62 | $this->restart(); |
||
| 63 | break; |
||
| 64 | case 'reload': |
||
| 65 | $this->reload(); |
||
| 66 | break; |
||
| 67 | case 'info': |
||
| 68 | $this->showInfo(); |
||
| 69 | break; |
||
| 70 | default: |
||
| 71 | $help = <<<EOS |
||
| 72 | |||
| 73 | Usage: |
||
| 74 | [%s] ./bin/laravels [options] <action> |
||
| 75 | |||
| 76 | Arguments: |
||
| 77 | action start|stop|restart|reload|info|help |
||
| 78 | |||
| 79 | Options: |
||
| 80 | -e, --env The environment the command should run under, this feature requires Laravel 5.2+ |
||
| 81 | -d, --daemonize Whether run as a daemon for "start & restart" |
||
| 82 | -i, --ignore Whether ignore checking process pid for "start & restart" |
||
| 83 | EOS; |
||
| 84 | |||
| 85 | $this->info(sprintf($help, PHP_BINARY)); |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } catch (\Exception $e) { |
||
| 89 | $error = sprintf( |
||
| 90 | 'Uncaught exception "%s"([%d]%s) at %s:%s, %s%s', |
||
| 91 | get_class($e), |
||
| 92 | $e->getCode(), |
||
| 93 | $e->getMessage(), |
||
| 94 | $e->getFile(), |
||
| 95 | $e->getLine(), |
||
| 96 | PHP_EOL, |
||
| 97 | $e->getTraceAsString() |
||
| 98 | ); |
||
| 99 | $this->error($error); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function start() |
||
| 140 | } |
||
| 141 | |||
| 142 | public function stop() |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | public function restart() |
||
| 197 | } |
||
| 198 | |||
| 199 | public function reload() |
||
| 200 | { |
||
| 201 | $config = $this->getConfig(); |
||
| 202 | $pidFile = $config['server']['swoole']['pid_file']; |
||
| 203 | if (!file_exists($pidFile)) { |
||
| 204 | $this->error('It seems that Swoole is not running.'); |
||
| 205 | return; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Reload worker processes |
||
| 209 | $pid = file_get_contents($pidFile); |
||
| 210 | if (!$pid || !self::kill($pid, 0)) { |
||
| 211 | $this->error("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
| 212 | return; |
||
| 213 | } |
||
| 214 | if (self::kill($pid, SIGUSR1)) { |
||
| 215 | $this->info("Swoole [PID={$pid}] is reloaded."); |
||
| 216 | } else { |
||
| 217 | $this->error("Swoole [PID={$pid}] is reloaded failed."); |
||
| 218 | } |
||
| 219 | |||
| 220 | // Reload custom processes |
||
| 221 | $pidFile = dirname($pidFile) . '/laravels-custom-processes.pid'; |
||
| 222 | if (file_exists($pidFile)) { |
||
| 223 | $pids = (array)explode("\n", trim(file_get_contents($pidFile))); |
||
| 224 | unlink($pidFile); |
||
| 225 | foreach ($pids as $pid) { |
||
| 226 | if (!$pid || !self::kill($pid, 0)) { |
||
| 227 | $this->error("Custom process[PID={$pid}] does not exist, or permission denied."); |
||
| 228 | continue; |
||
| 229 | } |
||
| 230 | |||
| 231 | if (self::kill($pid, SIGUSR1)) { |
||
| 232 | $this->info("Custom process[PID={$pid}] is reloaded."); |
||
| 233 | } else { |
||
| 234 | $this->error("Custom process[PID={$pid}] is reloaded failed."); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | // Reload timer process |
||
| 240 | if (!empty($config['server']['timer']['enable'])) { |
||
| 241 | $pidFile = dirname($pidFile) . '/laravels-timer-process.pid'; |
||
| 242 | $pid = file_get_contents($pidFile); |
||
| 243 | if (!$pid || !self::kill($pid, 0)) { |
||
| 244 | $this->error("Timer process[PID={$pid}] does not exist, or permission denied."); |
||
| 245 | return; |
||
| 246 | } |
||
| 247 | |||
| 248 | if (self::kill($pid, SIGUSR1)) { |
||
| 249 | $this->info("Timer process[PID={$pid}] is reloaded."); |
||
| 250 | } else { |
||
| 251 | $this->error("Timer process[PID={$pid}] is reloaded failed."); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | public function showInfo() |
||
| 257 | { |
||
| 258 | $this->runArtisanCommand('laravels info'); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function artisanCmd($subCmd) |
||
| 262 | { |
||
| 263 | $phpCmd = sprintf('%s -c "%s"', PHP_BINARY, php_ini_loaded_file()); |
||
| 264 | $env = $this->input->getOption('env'); |
||
| 265 | $envs = $env ? "APP_ENV={$env}" : ''; |
||
| 266 | $artisanCmd = trim(sprintf('%s %s %s/artisan %s', $envs, $phpCmd, $this->basePath, $subCmd)); |
||
| 267 | return $artisanCmd; |
||
| 268 | } |
||
| 269 | |||
| 270 | public function runArtisanCommand($cmd) |
||
| 271 | { |
||
| 272 | $cmd = $this->artisanCmd($cmd); |
||
| 273 | self::runCommand($cmd); |
||
| 274 | } |
||
| 275 | |||
| 276 | public function getConfig() |
||
| 277 | { |
||
| 278 | $json = file_get_contents($this->basePath . '/storage/laravels.json'); |
||
| 279 | return (array)json_decode($json, true); |
||
| 280 | } |
||
| 281 | |||
| 282 | public static function runCommand($cmd, $input = null) |
||
| 283 | { |
||
| 284 | $fp = popen($cmd, 'w'); |
||
| 285 | if ($fp === false) { |
||
| 286 | return false; |
||
| 287 | } |
||
| 288 | if ($input !== null) { |
||
| 289 | fwrite($fp, $input); |
||
| 290 | } |
||
| 291 | pclose($fp); |
||
| 292 | return true; |
||
| 293 | } |
||
| 294 | |||
| 295 | public static function kill($pid, $sig) |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 |