From 0b5ae6280352357c19e67e23aa643085883f90d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 19 Apr 2021 14:38:17 +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/Route/Dispatch/Dispatch.php | 124 ++++++++++++------------- System/Aop.php | 3 + 2 files changed, 64 insertions(+), 63 deletions(-) diff --git a/HttpServer/Route/Dispatch/Dispatch.php b/HttpServer/Route/Dispatch/Dispatch.php index 4706a3da..f95b1bf6 100644 --- a/HttpServer/Route/Dispatch/Dispatch.php +++ b/HttpServer/Route/Dispatch/Dispatch.php @@ -8,10 +8,7 @@ namespace HttpServer\Route\Dispatch; use Closure; use HttpServer\Controller; use HttpServer\Http\Context; -use HttpServer\Http\Request; use ReflectionException; -use Snowflake\Aop; -use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -22,76 +19,77 @@ use Snowflake\Snowflake; class Dispatch { - /** @var Closure|array */ - protected array|Closure $handler; + /** @var Closure|array */ + protected array|Closure $handler; - protected mixed $request; + protected mixed $request; - /** - * @param $handler - * @param $request - * @return static - * @throws NotFindClassException - * @throws ReflectionException - */ - public static function create($handler, $request): static - { - $class = new static(); - $class->handler = $handler; - $class->request = $request; - if ($handler instanceof Closure) { - $class->bind(); - } - $class->bindParam(); - return $class; - } + /** + * @param $handler + * @param $request + * @return static + * @throws NotFindClassException + * @throws ReflectionException + */ + public static function create($handler, $request): static + { + $class = new static(); + $class->handler = $handler; + $class->request = $request; + if ($handler instanceof Closure) { + $class->bind(); + } + $class->bindParam(); + return $class; + } - /** - * @return mixed - * 执行函数 - */ - public function dispatch(): mixed - { - return \aop($this->handler, $this->request); - } + /** + * @return mixed + * 执行函数 + * @throws \Exception + */ + public function dispatch(): mixed + { + return \aop($this->handler, $this->request); + } - /** - * @throws ReflectionException - * @throws NotFindClassException - */ - protected function bind() - { - $class = $this->bindRequest(Snowflake::createObject(Controller::class)); - $this->handler = Closure::bind($this->handler, $class); - } + /** + * @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; - } + /** + * @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); - } + /** + * 参数绑定 + */ + protected function bindParam() + { + if ($this->handler instanceof Closure) { + return; + } + $controller = $this->handler[0]; + $this->bindRequest($controller); + } } diff --git a/System/Aop.php b/System/Aop.php index bb06a737..04945a13 100644 --- a/System/Aop.php +++ b/System/Aop.php @@ -56,6 +56,9 @@ class Aop extends Component $aopName = get_class($close[0]) . '::' . $close[1]; if (!isset($this->_aop[$aopName])) { + if (!method_exists($close[0], $close[1])) { + return response()->close(404); + } return call_user_func($close, ...$get_args); }