Files
kiri-http-server/ServerCommand.php
T

110 lines
2.4 KiB
PHP
Raw Normal View History

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;
/**
* Class Command
* @package Http
*/
class ServerCommand extends Command
{
2022-06-16 17:38:22 +08:00
const ACTIONS = ['start', 'stop', 'restart'];
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')
->addOption('daemon', 'd', InputOption::VALUE_OPTIONAL, 'is run daemonize');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws ConfigException
* @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 ConfigException
* @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
* @throws ConfigException
* @throws ContainerExceptionInterface
2023-03-30 23:02:12 +08:00
* @throws NotFoundExceptionInterface
2022-06-16 17:38:22 +08:00
*/
protected function start(InputInterface $input): int
{
2022-06-22 16:29:41 +08:00
$this->server->setDaemon((int)!is_null($input->getOption('daemon')));
$this->server->start();
2022-06-16 17:38:22 +08:00
return 1;
}
2022-01-09 03:49:02 +08:00
}