From 955f3585770807ea2942155cf67a59328bf04532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 1 Dec 2021 16:09:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server.php | 23 ++++++++++++++----- ServerManager.php | 44 ++++++++++--------------------------- State.php | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 39 deletions(-) create mode 100644 State.php diff --git a/Server.php b/Server.php index 7efa81f..feba72a 100644 --- a/Server.php +++ b/Server.php @@ -3,13 +3,15 @@ namespace Server; -use Note\Inject; use Exception; use Http\Handler\Abstracts\HttpService; use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Config; use Kiri\Events\EventDispatch; use Kiri\Exception\ConfigException; +use Note\Inject; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Server\Events\OnShutdown; @@ -41,6 +43,13 @@ class Server extends HttpService public EventDispatch $eventDispatch; + /** + * @var State + */ + #[Inject(State::class)] + public State $state; + + /** * */ @@ -59,10 +68,11 @@ class Server extends HttpService /** - * @return string start server - * - * start server + * @return string * @throws ConfigException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws \ReflectionException * @throws Exception */ public function start(): string @@ -94,7 +104,7 @@ class Server extends HttpService { $configs = Config::get('server', [], true); foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) { - $this->manager->stopServer($config['port']); + $this->state->exit($config['port']); } $this->eventDispatch->dispatch(new OnShutdown()); } @@ -103,10 +113,11 @@ class Server extends HttpService /** * @return bool * @throws ConfigException + * @throws Exception */ public function isRunner(): bool { - return $this->manager->isRunner(); + return $this->state->isRunner(); } diff --git a/ServerManager.php b/ServerManager.php index 9d20ab1..6aee1fa 100644 --- a/ServerManager.php +++ b/ServerManager.php @@ -52,10 +52,21 @@ class ServerManager public int $port = 0; + /** + * @var Logger + */ #[Inject(Logger::class)] public Logger $logger; + /** + * @var State + */ + #[Inject(State::class)] + public State $state; + + + /** @var array */ public array $ports = []; @@ -149,23 +160,6 @@ class ServerManager } - /** - * @return bool - * @throws ConfigException - * @throws Exception - */ - public function isRunner(): bool - { - $configs = Config::get('server', [], true); - foreach ($this->sortService($configs['ports']) as $config) { - if (checkPortIsAlready($config['port'])) { - return true; - } - } - return false; - } - - /** * @param string|OnProcessInterface|BaseProcess $customProcess * @throws Exception @@ -315,22 +309,6 @@ class ServerManager } - /** - * @param int $port - * @throws Exception - */ - public function stopServer(int $port) - { - if (!($pid = checkPortIsAlready($port))) { - return; - } - while (checkPortIsAlready($port)) { - Process::kill($pid, SIGTERM); - usleep(300); - } - } - - /** * @param array $settings * @throws ContainerExceptionInterface diff --git a/State.php b/State.php new file mode 100644 index 0000000..8f46996 --- /dev/null +++ b/State.php @@ -0,0 +1,56 @@ +servers = Config::get('servers.ports'); + } + + + /** + * @return bool + * @throws Exception + */ + public function isRunner(): bool + { + $ports = $this->sortService($this->servers); + foreach ($ports as $config) { + if (checkPortIsAlready($config['port'])) { + return true; + } + } + return false; + } + + + /** + * @param $port + * @throws Exception + */ + public function exit($port) + { + if (!($pid = checkPortIsAlready($port))) { + return; + } + while (checkPortIsAlready($port)) { + Process::kill($pid, SIGTERM); + usleep(300); + } + } + +}