2022-01-09 03:49:02 +08:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-01-10 11:39:55 +08:00
|
|
|
namespace Kiri\Server;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2022-02-21 11:02:10 +08:00
|
|
|
use Kiri;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2022-06-16 17:38:22 +08:00
|
|
|
use ReflectionException;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
2023-07-06 16:20:46 +08:00
|
|
|
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP','http');
|
|
|
|
|
|
2022-01-09 03:49:02 +08:00
|
|
|
/**
|
|
|
|
|
* Class Command
|
|
|
|
|
* @package Http
|
|
|
|
|
*/
|
|
|
|
|
class ServerCommand extends Command
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2022-06-22 16:29:41 +08:00
|
|
|
private Server $server;
|
|
|
|
|
|
|
|
|
|
|
2022-06-16 17:38:22 +08:00
|
|
|
/**
|
|
|
|
|
* @return void
|
2023-04-19 14:46:38 +08:00
|
|
|
* @throws ReflectionException
|
2022-06-16 17:38:22 +08:00
|
|
|
*/
|
|
|
|
|
protected function configure(): void
|
|
|
|
|
{
|
2022-06-22 16:29:41 +08:00
|
|
|
$this->server = Kiri::getDi()->get(Server::class);
|
2022-06-16 17:38:22 +08:00
|
|
|
$this->setName('sw:server')
|
|
|
|
|
->setDescription('server start|stop|reload|restart')
|
|
|
|
|
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
|
2023-07-06 15:38:03 +08:00
|
|
|
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
|
2022-06-16 17:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param InputInterface $input
|
|
|
|
|
* @param OutputInterface $output
|
|
|
|
|
* @return int
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
|
{
|
2023-04-21 22:26:43 +08:00
|
|
|
return match ($input->getArgument('action')) {
|
2022-06-16 17:38:22 +08:00
|
|
|
'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 ContainerExceptionInterface
|
2023-04-04 13:59:35 +08:00
|
|
|
* @throws NotFoundExceptionInterface
|
2022-06-16 17:38:22 +08:00
|
|
|
*/
|
|
|
|
|
protected function restart(InputInterface $input): int
|
|
|
|
|
{
|
|
|
|
|
$this->stop();
|
|
|
|
|
$this->start($input);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
*/
|
|
|
|
|
protected function stop(): int
|
|
|
|
|
{
|
2022-06-22 16:29:41 +08:00
|
|
|
$this->server->shutdown();
|
2022-06-16 17:38:22 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param InputInterface $input
|
|
|
|
|
* @return int
|
2023-04-23 18:15:25 +08:00
|
|
|
* @throws
|
2022-06-16 17:38:22 +08:00
|
|
|
*/
|
|
|
|
|
protected function start(InputInterface $input): int
|
|
|
|
|
{
|
2023-07-06 15:38:03 +08:00
|
|
|
$this->server->setDaemon((int)($input->getOption('daemon')));
|
2022-06-22 16:29:41 +08:00
|
|
|
$this->server->start();
|
2022-06-16 17:38:22 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 03:49:02 +08:00
|
|
|
}
|