From 7024e0711504754b4cbf80d70cd142cfdaea7c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 22 Apr 2021 14:37:28 +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 --- HttpServer/Controller.php | 52 +++----------------------- HttpServer/Route/Dispatch/Dispatch.php | 39 +------------------ 2 files changed, 8 insertions(+), 83 deletions(-) diff --git a/HttpServer/Controller.php b/HttpServer/Controller.php index 214c0ac3..d602e672 100644 --- a/HttpServer/Controller.php +++ b/HttpServer/Controller.php @@ -18,23 +18,15 @@ use Snowflake\Snowflake; /** * Class WebController * @package Snowflake\Snowflake\Web + * @property-read HttpParams $input + * @property-read HttpHeaders $headers + * @property-read Request $request */ class Controller extends HttpService { use TraitApplication; - /** @var null|HttpParams $input */ - public null|HttpParams $input; - - - /** @var null|HttpHeaders */ - public null|HttpHeaders $headers; - - - /** @var null|Request */ - public null|Request $request; - /** * Controller constructor. @@ -57,40 +49,13 @@ class Controller extends HttpService } - /** - * @param null|HttpParams $input - */ - public function setInput(?HttpParams $input): void - { - $this->input = $input; - } - - /** - * @param ?HttpHeaders $headers - */ - public function setHeaders(?HttpHeaders $headers): void - { - $this->headers = $headers; - } - - /** - * @param ?Request $request - */ - public function setRequest(?Request $request): void - { - $this->request = $request; - } - /** * @return ?HttpParams * @throws Exception */ public function getInput(): ?HttpParams { - if (!$this->input) { - $this->input = $this->getRequest()->params; - } - return $this->input; + return \request()->params; } /** @@ -99,10 +64,7 @@ class Controller extends HttpService */ public function getHeaders(): ?HttpHeaders { - if (!$this->headers && $this->getRequest()) { - $this->headers = $this->getRequest()->headers; - } - return $this->headers; + return \request()->headers; } /** @@ -120,9 +82,7 @@ class Controller extends HttpService /** * @param $name * @return mixed - * @throws ComponentException - * @throws ReflectionException - * @throws NotFindClassException + * @throws Exception */ public function __get($name): mixed { diff --git a/HttpServer/Route/Dispatch/Dispatch.php b/HttpServer/Route/Dispatch/Dispatch.php index da080747..37337e87 100644 --- a/HttpServer/Route/Dispatch/Dispatch.php +++ b/HttpServer/Route/Dispatch/Dispatch.php @@ -8,9 +8,6 @@ namespace HttpServer\Route\Dispatch; use Closure; use HttpServer\Controller; use HttpServer\Http\Context; -use ReflectionException; -use Snowflake\Exception\NotFindClassException; -use Snowflake\Snowflake; /** * Class Dispatch @@ -27,10 +24,7 @@ class Dispatch /** * @param $handler - * @param $request * @return static - * @throws NotFindClassException - * @throws ReflectionException */ public static function create($handler): static { @@ -39,7 +33,6 @@ class Dispatch if ($handler instanceof Closure) { $class->bind(); } - $class->bindParam(); return $class; } @@ -60,39 +53,11 @@ class Dispatch /** - * @throws ReflectionException - * @throws NotFindClassException + * */ protected function bind() { - $class = $this->bindRequest(Snowflake::createObject(Controller::class)); - $this->handler = Closure::bind($this->handler, $class); - } - - - /** - * @param $controller - * @return mixed - */ - protected function bindRequest($controller): mixed - { - $controller->request = Context::getContext('request'); - $controller->headers = $controller->request?->headers; - $controller->input = $controller->request?->params; - return $controller; - } - - - /** - * 参数绑定 - */ - protected function bindParam() - { - if ($this->handler instanceof Closure) { - return; - } - $controller = $this->handler[0]; - $this->bindRequest($controller); + $this->handler = Closure::bind($this->handler, new Controller()); } }