diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index 01c0f23..81bba6b 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -134,8 +134,7 @@ class AsyncServer implements ServerInterface * @param $daemon * @return void * @throws ConfigException - * @throws ReflectionException - */ + */ private function initServer($match, $config, $daemon): void { $this->server = new $match($config->host, $config->port, $config->mode, $config->socket); @@ -145,7 +144,7 @@ class AsyncServer implements ServerInterface if (!isset($config->events[Constant::SHUTDOWN])) { $config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown']; } - Kiri::service()->set('server', $this->server); + Kiri::getDi()->bind(ServerInterface::class, $this->server); } diff --git a/Abstracts/ProcessManager.php b/Abstracts/ProcessManager.php index 96857d2..3f39175 100644 --- a/Abstracts/ProcessManager.php +++ b/Abstracts/ProcessManager.php @@ -9,6 +9,7 @@ use Kiri\Abstracts\Component; use Kiri\Server\Contract\OnProcessInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use ReflectionException; use Swoole\Coroutine; use Swoole\Process; use Psr\Container\ContainerInterface; @@ -36,15 +37,14 @@ class ProcessManager extends Component } - /** - * @param OnServerBeforeStart $beforeStart - * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface|Exception - */ + /** + * @param OnServerBeforeStart $beforeStart + * @return void + * @throws ReflectionException + */ public function OnServerBeforeStart(OnServerBeforeStart $beforeStart): void { - $server = Kiri::service()->get('server'); + $server = Kiri::getDi()->get(ServerInterface::class); foreach ($this->_process as $custom) { if (Kiri\Di\Context::inCoroutine()) { Coroutine::create(function () use ($custom) { @@ -180,7 +180,7 @@ class ProcessManager extends Component * @param Process $process * @return void * @throws Kiri\Exception\ConfigException - * @throws \ReflectionException + * @throws ReflectionException */ public function extracted(mixed $custom, Process $process): void { diff --git a/Handler/OnRequest.php b/Handler/OnRequest.php index 4c301bf..c88d221 100644 --- a/Handler/OnRequest.php +++ b/Handler/OnRequest.php @@ -91,14 +91,10 @@ class OnRequest implements OnRequestInterface $dispatcher = $this->router->query($PsrRequest->getUri()->getPath(), $PsrRequest->getMethod()); - $middleware = []; - if (!($dispatcher instanceof Kiri\Router\Base\NotFoundController)) { - $middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class); + $middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class); + $middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod()); - $middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod()); - } - - $PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest); + $PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest); } catch (\Throwable $throwable) { $PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class)); } finally { diff --git a/Task/Task.php b/Task/Task.php index 49edc6f..433db83 100644 --- a/Task/Task.php +++ b/Task/Task.php @@ -4,6 +4,7 @@ namespace Kiri\Server\Task; use Kiri\Server\Constant; +use Kiri\Server\ServerInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use ReflectionException; @@ -73,7 +74,7 @@ class Task implements TaskInterface public function dispatch(array|string|object $handler, ?int $workerId = null): void { /** @var Server $server */ - $server = \Kiri::service()->get('server'); + $server = \Kiri::getDi()->get(ServerInterface::class); if (is_null($workerId)) { $workerId = rand(0, $server->setting[Constant::OPTION_TASK_WORKER_NUM] - 1); }