Files
kiri-core/HttpServer/Command.php
T

81 lines
1.9 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
namespace HttpServer;
2020-09-07 18:11:36 +08:00
use Exception;
2021-02-22 17:44:24 +08:00
use ReflectionException;
2020-09-07 18:11:36 +08:00
use Snowflake\Abstracts\Input;
2021-02-22 17:44:24 +08:00
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
2020-09-07 15:19:13 +08:00
use Snowflake\Exception\ConfigException;
2021-02-22 17:44:24 +08:00
use Snowflake\Exception\NotFindPropertyException;
2020-09-03 00:15:57 +08:00
use Snowflake\Snowflake;
/**
* Class Command
* @package HttpServer
*/
class Command extends \Console\Command
{
2020-10-29 11:19:02 +08:00
public string $command = 'sw:server';
2020-09-03 00:15:57 +08:00
2020-10-29 11:19:02 +08:00
public string $description = 'server start|stop|reload|restart';
2020-09-03 00:15:57 +08:00
2020-09-11 14:27:09 +08:00
const ACTIONS = ['start', 'stop', 'restart'];
2020-09-03 00:15:57 +08:00
/**
2020-09-07 18:11:36 +08:00
* @param Input $dtl
2020-12-17 14:09:14 +08:00
* @return string
2020-09-11 14:27:09 +08:00
* @throws Exception
* @throws ConfigException
2020-09-03 00:15:57 +08:00
*/
2020-12-17 14:09:14 +08:00
public function onHandler(Input $dtl): string
2020-09-03 00:15:57 +08:00
{
2021-02-20 13:08:54 +08:00
$manager = Snowflake::app()->getServer();
2020-09-07 18:11:36 +08:00
$manager->setDaemon($dtl->get('daemon', 0));
2020-09-11 14:27:09 +08:00
if (!in_array($dtl->get('action'), self::ACTIONS)) {
2020-09-11 14:42:52 +08:00
return 'I don\'t know what I want to do.';
2020-09-11 14:27:09 +08:00
}
if ($manager->isRunner() && $dtl->get('action') == 'start') {
2020-09-11 14:42:52 +08:00
return 'Service is running. Please use restart.';
2020-09-07 15:19:13 +08:00
}
2020-09-11 14:27:09 +08:00
$manager->shutdown();
2020-09-11 14:22:44 +08:00
if ($dtl->get('action') == 'stop') {
2020-09-11 14:42:52 +08:00
return 'shutdown success.';
2020-09-11 14:22:44 +08:00
}
2021-02-22 17:44:24 +08:00
listen(Event::SERVER_BEFORE_START, [$this, 'scan_system_annotation']);
2020-12-17 14:09:14 +08:00
return $manager->start();
2020-09-03 00:15:57 +08:00
}
2021-02-22 17:44:24 +08:00
/**
* @throws ReflectionException
* @throws ComponentException
* @throws NotFindPropertyException
*/
public function scan_system_annotation()
{
$annotation = Snowflake::app()->getAttributes();
$annotation->readControllers(__DIR__ . '/../Console', 'Console', 'system');
$annotation->readControllers(__DIR__ . '/../Database', 'Database', 'system');
$annotation->readControllers(__DIR__ . '/../Gii', 'Gii', 'system');
$annotation->readControllers(__DIR__ . '/../HttpServer', 'HttpServer', 'system');
$annotation->readControllers(__DIR__ . '/../Kafka', 'Kafka', 'system');
$annotation->readControllers(__DIR__ . '/../System', 'Snowflake', 'system');
$annotation->readControllers(__DIR__ . '/../Validator', 'Validator', 'system');
}
2020-09-03 00:15:57 +08:00
}