Files
kiri-core/http-helper/Command.php
T

96 lines
2.2 KiB
PHP
Raw Normal View History

2020-09-03 00:15:57 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-09-03 00:15:57 +08:00
2021-08-17 16:43:50 +08:00
namespace Http;
2020-09-03 00:15:57 +08:00
2021-08-29 04:44:33 +08:00
use Annotation\Inject;
2020-09-07 18:11:36 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Input;
2021-08-29 01:18:42 +08:00
use Kiri\Events\EventProvider;
2021-08-11 01:04:57 +08:00
use Kiri\Exception\ConfigException;
2021-09-02 11:13:11 +08:00
use Kiri\Exception\NotFindClassException;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-09-02 11:13:11 +08:00
use ReflectionException;
2021-08-29 04:57:50 +08:00
use Server\Events\OnBeforeWorkerStart;
2021-09-02 11:13:11 +08:00
use Server\Events\OnWorkerStart;
2021-08-29 04:57:50 +08:00
use Server\Worker\OnServerWorker;
2021-08-29 01:18:42 +08:00
use Server\Worker\OnWorkerStart as WorkerDispatch;
2021-09-02 11:26:11 +08:00
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2020-09-03 00:15:57 +08:00
/**
* Class Command
2021-08-17 16:43:50 +08:00
* @package Http
2020-09-03 00:15:57 +08:00
*/
2021-09-02 11:13:11 +08:00
class Command extends \Symfony\Component\Console\Command\Command
2020-09-03 00:15:57 +08:00
{
2021-09-02 11:13:11 +08:00
const ACTIONS = ['start', 'stop', 'restart'];
/**
* @var EventProvider
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
*
*/
protected function configure()
{
2021-09-02 11:26:11 +08:00
$this->setName('sw:server')
2021-09-02 12:02:26 +08:00
->setDescription('server start|stop|reload|restart')
->addOption('action')
->addOption('daemon');
2021-09-02 11:13:11 +08:00
}
/**
2021-09-02 11:26:11 +08:00
* @param InputInterface $input
* @param OutputInterface $output
2021-09-02 11:13:11 +08:00
* @return string
* @throws ConfigException
2021-09-02 11:26:11 +08:00
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
2021-09-02 11:13:11 +08:00
*/
2021-09-02 11:26:11 +08:00
public function execute(InputInterface $input, OutputInterface $output): string
2021-09-02 11:13:11 +08:00
{
$manager = Kiri::app()->getServer();
2021-09-02 11:26:11 +08:00
$manager->setDaemon($input->getArgument('daemon'));
if (!in_array($input->getArgument('action'), self::ACTIONS)) {
return $output->write('I don\'t know what I want to do.');
2021-09-02 11:13:11 +08:00
}
2021-09-02 11:26:11 +08:00
if ($manager->isRunner() && $input->getArgument('action') == 'start') {
return $output->write('Service is running. Please use restart.');
2021-09-02 11:13:11 +08:00
}
$manager->shutdown();
2021-09-02 11:26:11 +08:00
if ($input->getArgument('action') == 'stop') {
return $output->write('shutdown success');
2021-09-02 11:13:11 +08:00
}
return $this->generate_runtime_builder($manager);
}
/**
* @param $manager
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private function generate_runtime_builder($manager): mixed
{
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
$this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']);
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
return $manager->start();
}
2021-04-30 11:15:57 +08:00
2020-09-03 00:15:57 +08:00
}