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\Abstracts\Config;
|
|
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
|
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-04-29 14:49:29 +08:00
|
|
|
const ACTIONS = ['start', 'stop', 'restart'];
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2022-04-29 14:49:29 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
protected function configure()
|
|
|
|
|
{
|
|
|
|
|
$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');
|
|
|
|
|
}
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
2022-04-29 14:49:29 +08:00
|
|
|
/**
|
|
|
|
|
* @param InputInterface $input
|
|
|
|
|
* @param OutputInterface $output
|
|
|
|
|
* @return int
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
|
{
|
|
|
|
|
$manager = Kiri::app()->getServer();
|
2022-06-08 15:34:34 +08:00
|
|
|
$manager->setDaemon((int)$input->hasOption('daemon'));
|
2022-01-09 17:56:47 +08:00
|
|
|
|
2022-04-29 14:49:29 +08:00
|
|
|
$this->scan_file();
|
2022-02-21 11:02:10 +08:00
|
|
|
|
2022-04-29 14:49:29 +08:00
|
|
|
$action = $input->getArgument('action');
|
|
|
|
|
if (is_null($action)) {
|
|
|
|
|
throw new Exception('I don\'t know what I want to do.');
|
|
|
|
|
}
|
|
|
|
|
if (!in_array($action, self::ACTIONS)) {
|
|
|
|
|
throw new Exception('I don\'t know what I want to do.');
|
|
|
|
|
}
|
|
|
|
|
if ($action == 'restart' || $action == 'stop') {
|
|
|
|
|
$manager->shutdown();
|
|
|
|
|
if ($action == 'stop') {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$manager->start();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-01-09 03:49:02 +08:00
|
|
|
|
2022-02-21 11:02:10 +08:00
|
|
|
|
2022-04-29 14:49:29 +08:00
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
protected function scan_file(): void
|
|
|
|
|
{
|
|
|
|
|
$config = Config::get('reload.scanner', []);
|
|
|
|
|
if (is_array($config)) foreach ($config as $key => $value) {
|
|
|
|
|
scan_directory($value, $key);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-21 11:02:10 +08:00
|
|
|
|
2022-01-09 03:49:02 +08:00
|
|
|
}
|