From 5ec3916ff1c00eb9402f1e29a357dfd9e21f33bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sun, 26 Sep 2021 15:56:19 +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 --- http-handler/Abstracts/Handler.php | 42 ++++++++++++++++++------------ http-handler/Handler.php | 11 ++++++++ http-server/Service/Http.php | 7 ++--- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index 95a9fa62..10ff1c75 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -6,38 +6,36 @@ use Annotation\Inject; use Http\Handler\Handler as CHl; use Http\Message\ServerRequest; use Kiri\Core\Help; -use Kiri\Events\EventDispatch; use Kiri\Kiri; use Kiri\Proxy\AspectProxy; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use Swoole\Coroutine\Iterator; abstract class Handler implements RequestHandlerInterface { - #[Inject(AspectProxy::class)] + #[Inject(AspectProxy::class)] protected AspectProxy $aspectProxy; - protected int $offset = 0; + protected int $offset = 0; - /** - * @param CHl $handler - * @param array|null $middlewares - */ - public function __construct(public CHl $handler, public ?array $middlewares) - { - $this->aspectProxy = Kiri::getDi()->get(AspectProxy::class); - } + /** + * @param CHl $handler + * @param array|null $middlewares + */ + public function __construct(public CHl $handler, public ?array $middlewares) + { + $this->aspectProxy = Kiri::getDi()->get(AspectProxy::class); + } - /** + /** * @param ServerRequestInterface $request * @return ResponseInterface * @throws \Exception @@ -53,12 +51,22 @@ abstract class Handler implements RequestHandlerInterface throw new \Exception('get_implements_class($middleware) not found method process.'); } - $this->offset++; + $this->offset++; return $middleware->process($request, $this); } + private function redecue() + { + return function ($stack, $pipe) { + return function ($passable) use ($stack, $pipe) { + return ([$pipe, 'process'])($passable, $stack); + }; + }; + } + + /** * @param ServerRequestInterface $request * @return mixed @@ -103,9 +111,9 @@ abstract class Handler implements RequestHandlerInterface $interface->withContentType('application/json;charset=utf-8'); } if (str_contains($interface->getContentType(), 'xml')) { - if (is_object($responseData)) { - $responseData = get_object_vars($responseData); - } + if (is_object($responseData)) { + $responseData = get_object_vars($responseData); + } $interface->getBody()->write(Help::toXml($responseData)); } else if (is_array($responseData)) { $interface->getBody()->write(json_encode($responseData)); diff --git a/http-handler/Handler.php b/http-handler/Handler.php index 8b241192..bd575866 100644 --- a/http-handler/Handler.php +++ b/http-handler/Handler.php @@ -3,7 +3,10 @@ namespace Http\Handler; use Closure; +use Http\Handler\Abstracts\MiddlewareManager; +use Kiri\Events\EventProvider; use Kiri\Kiri; +use Server\Events\OnAfterWorkerStart; class Handler { @@ -18,6 +21,9 @@ class Handler public ?array $params = []; + public array $_middlewares = []; + + /** * @param string $route * @param array|Closure $callback @@ -30,6 +36,11 @@ class Handler $this->_injectParams($callback); $this->callback = $callback; + + $dispatcher = Kiri::getDi()->get(EventProvider::class); + $dispatcher->on(OnAfterWorkerStart::class, function () { + $this->_middlewares = MiddlewareManager::get($this->route); + }); } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 3fdbd5de..7a41f093 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -4,10 +4,8 @@ namespace Server\Service; use Annotation\Inject; -use Co\Iterator; use Exception; use Http\Handler\Abstracts\HandlerManager; -use Http\Handler\Abstracts\MiddlewareManager; use Http\Handler\Context; use Http\Handler\Dispatcher; use Http\Handler\Handler; @@ -112,9 +110,8 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface */ protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface { - $middlewares = MiddlewareManager::get($handler->callback); - $dispatcher = new Dispatcher($handler, $middlewares); - return $dispatcher->handle($PsrRequest); + $dispatcher = new Dispatcher($handler, $handler->_middlewares); + return $dispatcher->handle($PsrRequest); }