From a6f056b8c817e4f770d3325259bcdf1328257c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sat, 18 Sep 2021 13:45:33 +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 | 21 +++++++++++++++++++-- http-server/Service/Http.php | 7 ++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index 1295a348..24f33369 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -19,12 +19,29 @@ abstract class Handler implements RequestHandlerInterface private int $offset = 0; + protected CHl $handler; + + protected ?array $middlewares; + /** * @param CHl $handler - * @param array|null $middlewares + * @return $this */ - public function __construct(protected CHl $handler, protected ?array $middlewares) + public function setHandler(CHl $handler): static { + $this->handler = $handler; + return $this; + } + + + /** + * @param array|null $middlewares + * @return $this + */ + public function setMiddlewares(?array $middlewares): static + { + $this->middlewares = $middlewares; + return $this; } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 257e31f9..12dc0d71 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -3,6 +3,7 @@ namespace Server\Service; +use Annotation\Inject; use Exception; use Http\Context\Context; use Http\Exception\RequestException; @@ -35,6 +36,10 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect { + #[Inject(Dispatcher::class)] + public Dispatcher $dispatcher; + + /** * @param Server $server * @param int $fd @@ -83,7 +88,7 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect { $middlewares = MiddlewareManager::get($handler->callback); - $dispatcher = new Dispatcher($handler, $middlewares); + $dispatcher = $this->dispatcher->setHandler($handler)->setMiddlewares($middlewares); return $dispatcher->handle($PsrRequest); }