From 796b819fe53315682d1ed3c11d9914d6c65d26c0 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Mon, 29 Mar 2021 03:19:22 +0800 Subject: [PATCH] modify --- HttpServer/Route/Dispatch/Dispatch.php | 122 ++++++++++++------------- function.php | 15 +++ 2 files changed, 75 insertions(+), 62 deletions(-) diff --git a/HttpServer/Route/Dispatch/Dispatch.php b/HttpServer/Route/Dispatch/Dispatch.php index e66a1692..4706a3da 100644 --- a/HttpServer/Route/Dispatch/Dispatch.php +++ b/HttpServer/Route/Dispatch/Dispatch.php @@ -22,78 +22,76 @@ 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 - { - /** @var Aop $aop */ - $aop = Snowflake::app()->get('aop'); - return call_user_func([$aop, 'dispatch'], $this->handler, ...$this->request); - } + /** + * @return mixed + * 执行函数 + */ + 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/function.php b/function.php index 9bcaa78c..07e329d5 100644 --- a/function.php +++ b/function.php @@ -298,6 +298,21 @@ if (!function_exists('fire')) { } } +if (!function_exists('aop')) { + + + /** + * @param string $event + * @param array $params + * @throws Exception + * @throws Exception + */ + function aop(mixed $handler, array $params = []) + { + return Snowflake::app()->get('aop')->dispatch($handler, ...$params); + } +} + if (!function_exists('objectPool')) {