From a6d2d97fb5e445c875f45e2221ed25d94e609fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 16 Apr 2023 02:15:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Abstracts/AsyncServer.php | 12 ++++----- Abstracts/ProcessManager.php | 6 ++--- Abstracts/Server.php | 6 ++--- CoroutineServer.php | 11 +++++---- Handler/OnPipeMessage.php | 2 -- Handler/OnServerManager.php | 1 - Handler/OnServerWorker.php | 7 +----- Server.php | 47 +++++++++++++++--------------------- 8 files changed, 37 insertions(+), 55 deletions(-) diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index 3e94be8..e841543 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -14,13 +14,13 @@ use Psr\Log\LoggerInterface; use ReflectionException; use Kiri\Server\Config as SConfig; use Kiri\Di\LocalService; -use Swoole\Runtime; use Swoole\Server; use Kiri\Server\ServerInterface; use Kiri\Server\Constant; use Kiri\Events\EventDispatch; use Kiri\Exception\NotFindClassException; use Kiri\Server\Events\OnServerBeforeStart; +use Kiri\Di\Inject\Container; /** * @@ -44,11 +44,11 @@ class AsyncServer implements ServerInterface * @param LoggerInterface $logger * @param ProcessManager $processManager */ - public function __construct(public Config $config, - public ContainerInterface $container, - public EventDispatch $dispatch, - public LoggerInterface $logger, - public ProcessManager $processManager) + public function __construct(#[Container(Config::class)] public Config $config, + #[Container(ContainerInterface::class)] public ContainerInterface $container, + #[Container(EventDispatch::class)] public EventDispatch $dispatch, + #[Container(LoggerInterface::class)] public LoggerInterface $logger, + #[Container(ProcessManager::class)] public ProcessManager $processManager) { } diff --git a/Abstracts/ProcessManager.php b/Abstracts/ProcessManager.php index 5e779fd..e0d979b 100644 --- a/Abstracts/ProcessManager.php +++ b/Abstracts/ProcessManager.php @@ -12,10 +12,10 @@ use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Swoole\Coroutine; use Swoole\Process; -use Kiri\Annotation\Inject; use Psr\Container\ContainerInterface; use Kiri\Events\EventProvider; use Kiri\Server\ServerInterface; +use Kiri\Di\Inject\Container; use Kiri\Server\Events\OnServerBeforeStart; class ProcessManager extends Component @@ -29,11 +29,11 @@ class ProcessManager extends Component /** * @var ContainerInterface */ - #[Inject(ContainerInterface::class)] + #[Container(ContainerInterface::class)] public ContainerInterface $container; - #[Inject(EventProvider::class)] + #[Container(EventProvider::class)] public EventProvider $provider; /** diff --git a/Abstracts/Server.php b/Abstracts/Server.php index 44bac55..98c4404 100644 --- a/Abstracts/Server.php +++ b/Abstracts/Server.php @@ -4,12 +4,10 @@ namespace Kiri\Server\Abstracts; -use Kiri\Annotation\Inject; use Exception; -use Kiri\Abstracts\Config; -use Kiri\Exception\ConfigException; use Kiri; use Psr\Log\LoggerInterface; +use Kiri\Di\Inject\Container; /** @@ -23,7 +21,7 @@ abstract class Server /** * @var LoggerInterface */ - #[Inject(LoggerInterface::class)] + #[Container(LoggerInterface::class)] public LoggerInterface $logger; diff --git a/CoroutineServer.php b/CoroutineServer.php index 1e30394..e296876 100644 --- a/CoroutineServer.php +++ b/CoroutineServer.php @@ -4,6 +4,7 @@ namespace Kiri\Server; use Exception; use Kiri\Abstracts\Config; +use Kiri\Di\Inject\Container; use Psr\Container\ContainerInterface; use Kiri\Events\EventDispatch; use Kiri\Exception\ConfigException; @@ -50,11 +51,11 @@ class CoroutineServer implements ServerInterface * @param LoggerInterface $logger * @param ProcessManager $processManager */ - public function __construct(public Config $config, - public ContainerInterface $container, - public EventDispatch $dispatch, - public LoggerInterface $logger, - public ProcessManager $processManager) + public function __construct(#[Container(Config::class)] public Config $config, + #[Container(ContainerInterface::class)] public ContainerInterface $container, + #[Container(EventDispatch::class)] public EventDispatch $dispatch, + #[Container(LoggerInterface::class)] public LoggerInterface $logger, + #[Container(ProcessManager::class)] public ProcessManager $processManager) { } diff --git a/Handler/OnPipeMessage.php b/Handler/OnPipeMessage.php index a114079..55e3e00 100644 --- a/Handler/OnPipeMessage.php +++ b/Handler/OnPipeMessage.php @@ -3,8 +3,6 @@ namespace Kiri\Server\Handler; use Exception; -use Kiri\Annotation\Inject; -use Kiri\Events\EventDispatch; use Kiri\Server\Abstracts\Server; use Kiri\Server\Contract\OnPipeMessageInterface; diff --git a/Handler/OnServerManager.php b/Handler/OnServerManager.php index d9a6d34..79f03f1 100644 --- a/Handler/OnServerManager.php +++ b/Handler/OnServerManager.php @@ -3,7 +3,6 @@ namespace Kiri\Server\Handler; use Kiri; -use Kiri\Annotation\Inject; use Kiri\Events\EventDispatch; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; diff --git a/Handler/OnServerWorker.php b/Handler/OnServerWorker.php index 1d8b0bb..45ca733 100644 --- a/Handler/OnServerWorker.php +++ b/Handler/OnServerWorker.php @@ -7,8 +7,6 @@ use Kiri; use Kiri\Abstracts\Config; use Kiri\Core\Help; use Kiri\Events\EventDispatch; -use Kiri\Message\Handler\Router; -use Kiri\Server\Constant; use Kiri\Server\Events\OnAfterWorkerStart; use Kiri\Server\Events\OnBeforeWorkerStart; use Kiri\Server\Events\OnTaskerStart as OnTaskStart; @@ -19,12 +17,9 @@ use Kiri\Server\Events\OnWorkerStop; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use ReflectionException; -use Swoole\Runtime; use Swoole\Server; use Kiri\Server\Abstracts\StatusEnum; use Kiri\Server\WorkerStatus; -use Swoole\Timer; -use Kiri\Annotation\Inject; /** @@ -134,7 +129,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server Help::sendEmail($email, 'Service Error', $messageContent); } } catch (\Throwable $e) { - error($e, 'email'); + error($e, ['email']); } } diff --git a/Server.php b/Server.php index 1db050c..813af3b 100644 --- a/Server.php +++ b/Server.php @@ -20,6 +20,7 @@ use Kiri\Server\Events\OnWorkerStop; use Swoole\Coroutine; use Kiri\Server\Abstracts\ProcessManager; use Kiri\Server\Abstracts\AsyncServer; +use Kiri\Di\Inject\Container; defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid'); @@ -37,27 +38,23 @@ class Server public AsyncServer|CoroutineServer $manager; + #[Container(State::class)] + public State $state; - /** - * @param State $state - * @param ContainerInterface $container - * @param ProcessManager $processManager - * @param EventDispatch $dispatch - * @param EventProvider $provider - * @param Router $router - * @param array $config - * @throws Exception - */ - public function __construct(public State $state, - public ContainerInterface $container, - public ProcessManager $processManager, - public EventDispatch $dispatch, - public EventProvider $provider, - public Router $router, - array $config = []) - { - parent::__construct($config); - } + #[Container(ContainerInterface::class)] + public ContainerInterface $container; + + #[Container(ProcessManager::class)] + public ProcessManager $processManager; + + #[Container(EventDispatch::class)] + public EventDispatch $dispatch; + + #[Container(EventProvider::class)] + public EventProvider $provider; + + #[Container(Router::class)] + public Router $router; /** @@ -67,7 +64,7 @@ class Server */ public function init(): void { - $this->manager = $this->container->get(Config::get('server.type',AsyncServer::class)); + $this->manager = $this->container->get(Config::get('server.type', AsyncServer::class)); $enable_coroutine = Config::get('server.settings.enable_coroutine', false); if (!$enable_coroutine) { @@ -160,13 +157,7 @@ class Server public function onHotReload(): void { $this->onWorkerListener(); - $reload = Config::get('reload.hot', false); - if ($reload !== false) { - $this->provider->on(OnWorkerStart::class, [$this->router, 'scan_build_route']); - $this->manager->addProcess(Kiri\Reload\Inotify::class); - } else { - $this->router->scan_build_route(); - } + $this->router->scan_build_route(); }