manager = $container->get(AsyncServer::class); $this->state = $container->get(State::class); $this->dispatch = $container->get(EventDispatch::class); $this->router = $container->get(Router::class); } /** * @return void */ protected function configure(): void { $this->setName('sw:server') ->setDescription('server start|stop|reload|restart') ->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start') ->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize'); } /** * @param InputInterface $input * @param OutputInterface $output * @return int * @throws Exception */ public function execute(InputInterface $input, OutputInterface $output): int { return match ($input->getArgument('action')) { 'restart' => $this->restart($input), 'stop' => $this->stop(), 'start' => $this->start($input), default => throw new Exception('I don\'t know what I want to do.') }; } /** * @param InputInterface $input * @return int * @throws ReflectionException */ protected function restart(InputInterface $input): int { $this->stop(); $this->start($input); return 1; } /** * @return int * @throws ReflectionException * @throws Exception */ protected function stop(): int { $configs = \config('server', []); $instances = $this->manager->sortService($configs['ports'] ?? []); foreach ($instances as $config) { $this->state->exit($config->port); } $this->dispatch->dispatch(new OnShutdown()); return 1; } /** * @param InputInterface $input * @return int * @throws */ protected function start(InputInterface $input): int { $daemon = (int)$input->getOption('daemon'); if (\config('reload.hot', false) === true) { $this->manager->addProcess(HotReload::class); } else { $this->router->scan_build_route(); } $this->manager->initCoreServers(\config('server', []), $daemon); $this->manager->start(); return 1; } }