From a0e5eb03d29b9f9635bbd198068ddbe3ed50b5fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 30 Aug 2021 18:17:37 +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-helper/Route/HandlerProviders.php | 5 +- http-helper/Route/Node.php | 65 +++++++++++++++----------- http-helper/Route/Router.php | 4 +- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/http-helper/Route/HandlerProviders.php b/http-helper/Route/HandlerProviders.php index 3fd588b7..834e9fc4 100644 --- a/http-helper/Route/HandlerProviders.php +++ b/http-helper/Route/HandlerProviders.php @@ -29,11 +29,10 @@ class HandlerProviders extends BaseObject * @param $method * @param $path * @param $handler - * @param $_injectParameters */ - public static function add($method, $path, $handler, $_injectParameters) + public static function add($method, $path, $handler) { - static::$handlers[$method][$path] = [$handler, $_injectParameters]; + static::$handlers[$method][$path] = $handler; } } diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index 83b7726f..cf8a9b68 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -45,7 +45,11 @@ class Node public string $htmlSuffix = '.html'; public bool $enableHtmlSuffix = false; + + /** @var array */ public array $namespace = []; + + /** @var array */ public array $middleware = []; public string $sourcePath = ''; @@ -137,46 +141,49 @@ class Node private function injectMiddleware(string $method, $handler, $_injectParameters): void { if (!($handler instanceof Closure)) { - $callback = $this->injectControllerMiddleware($handler); + $callback = $this->injectControllerMiddleware($method, $handler, $_injectParameters); } else { - $callback = $this->injectClosureMiddleware($handler); + $callback = $this->injectClosureMiddleware($method, $handler, $_injectParameters); } - HandlerProviders::add($method, $this->sourcePath, $callback, $_injectParameters); + HandlerProviders::add($method, $this->sourcePath, $callback); } /** + * @param $method * @param $handler + * @param $_injectParameters * @return mixed * @throws NotFindClassException * @throws ReflectionException */ - private function injectControllerMiddleware($handler): mixed + private function injectControllerMiddleware($method, $handler, $_injectParameters): mixed { - MiddlewareManager::addMiddlewares($handler[0], $handler[1], $this->middleware); + MiddlewareManager::addMiddlewares($handler[0], $handler[1], $this->middleware[$method] ?? []); return MiddlewareManager::callerMiddlewares( - $handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler) + $handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler, $_injectParameters) ); } /** + * @param $method * @param $handler + * @param $_injectParameters * @return Closure */ - private function injectClosureMiddleware($handler): Closure + private function injectClosureMiddleware($method, $handler, $_injectParameters): Closure { - if (!empty($this->middleware)) { - return MiddlewareManager::closureMiddlewares($this->middleware, $this->normalHandler($handler)); + if (!empty($this->middleware[$method] ?? [])) { + return MiddlewareManager::closureMiddlewares($this->middleware[$method], + $this->normalHandler($handler, $_injectParameters) + ); } else { - return $this->normalHandler($handler); + return $this->normalHandler($handler, $_injectParameters); } } - private ?array $_injectParameters = []; - - /** * @throws ReflectionException * @throws NotFindClassException @@ -207,15 +214,16 @@ class Node /** * @param IAspect|null $reflect * @param $handler + * @param $_injectParameters * @return Closure */ - #[Pure] private function aopHandler(?IAspect $reflect, $handler): Closure + #[Pure] private function aopHandler(?IAspect $reflect, $handler, $_injectParameters): Closure { if (is_null($reflect)) { - return $this->normalHandler($handler); + return $this->normalHandler($handler, $_injectParameters); } - return static function () use ($reflect, $handler) { - return $reflect->invoke($handler, func_get_args()); + return static function () use ($reflect, $handler, $_injectParameters) { + return $reflect->invoke($handler, $_injectParameters); }; } @@ -242,12 +250,13 @@ class Node /** * @param $handler + * @param $_injectParameters * @return Closure */ - private function normalHandler($handler): Closure + private function normalHandler($handler, $_injectParameters): Closure { - return static function () use ($handler) { - return call_user_func($handler, ...func_get_args()); + return static function () use ($handler, $_injectParameters) { + return call_user_func($handler, ...$_injectParameters); }; } @@ -351,17 +360,21 @@ class Node /** + * @param $method * @param Closure|array $class * @return $this */ - public function addMiddleware(Closure|array $class): static + public function addMiddleware($method, Closure|array $class): static { if (empty($class)) return $this; + if (!isset($this->middleware[$method])) { + $this->middleware[$method] = []; + } foreach ($class as $closure) { - if (in_array($closure, $this->middleware)) { + if (in_array($closure, $this->middleware[$method])) { continue; } - $this->middleware[] = $closure; + $this->middleware[$method][] = $closure; } return $this; } @@ -389,11 +402,7 @@ class Node if (empty($handlerProviders)) { throw new RequestException('

HTTP 404 Not Found


Powered by Swoole', 404); } - if (!empty($handlerProviders[1])) { - var_dump($handlerProviders[1]); - return call_user_func($handlerProviders[0], ...$handlerProviders[1]); - } - return call_user_func($handlerProviders[0]); + return call_user_func($handlerProviders); } } diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 481b202f..c1212dc2 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -291,10 +291,10 @@ class Router extends HttpService implements RouterInterface $name = array_column($this->groupTacks, 'middleware'); if ($this->middleware instanceof \Closure) { - $node->addMiddleware([$this->middleware]); + $node->addMiddleware($method, [$this->middleware]); } if (is_array($name)) { - $node->addMiddleware($this->resolve_middleware($name)); + $node->addMiddleware($method, $this->resolve_middleware($name)); } return $node; }