From defcaec071fe059a736c9ccbe90c368762f4bf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 30 Aug 2021 17:34:57 +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 | 24 +++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/http-helper/Route/HandlerProviders.php b/http-helper/Route/HandlerProviders.php index 834e9fc4..3fd588b7 100644 --- a/http-helper/Route/HandlerProviders.php +++ b/http-helper/Route/HandlerProviders.php @@ -29,10 +29,11 @@ class HandlerProviders extends BaseObject * @param $method * @param $path * @param $handler + * @param $_injectParameters */ - public static function add($method, $path, $handler) + public static function add($method, $path, $handler, $_injectParameters) { - static::$handlers[$method][$path] = $handler; + static::$handlers[$method][$path] = [$handler, $_injectParameters]; } } diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index b95f16ff..d984bf77 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -130,17 +130,18 @@ class Node /** * @param string $method * @param $handler + * @param $_injectParameters * @throws NotFindClassException * @throws ReflectionException */ - private function injectMiddleware(string $method, $handler): void + private function injectMiddleware(string $method, $handler, $_injectParameters): void { if (!($handler instanceof Closure)) { $callback = $this->injectControllerMiddleware($handler); } else { $callback = $this->injectClosureMiddleware($handler); } - HandlerProviders::add($method, $this->sourcePath, $callback); + HandlerProviders::add($method, $this->sourcePath, $callback, $_injectParameters); } @@ -188,15 +189,15 @@ class Node } foreach ($this->_handler as $method => $dispatcher) { if ($dispatcher instanceof Closure) { - $this->_injectParameters = $container->resolveFunctionParameters($dispatcher); + $_injectParameters = $container->resolveFunctionParameters($dispatcher); } else { [$controller, $action] = $dispatcher; if (is_object($controller)) { $controller = get_class($controller); } - $this->_injectParameters = $container->getMethodParameters($controller, $action); + $_injectParameters = $container->getMethodParameters($controller, $action); } - $this->injectMiddleware($method, $dispatcher); + $this->injectMiddleware($method, $dispatcher, $_injectParameters); } $this->_handler = []; return $this; @@ -213,10 +214,8 @@ class Node if (is_null($reflect)) { return $this->normalHandler($handler); } - - $params = $this->_injectParameters; - return static function () use ($reflect, $handler, $params) { - return $reflect->invoke($handler, $params); + return static function () use ($reflect, $handler) { + return $reflect->invoke($handler, func_get_args()); }; } @@ -247,9 +246,8 @@ class Node */ private function normalHandler($handler): Closure { - $params = $this->_injectParameters; - return static function () use ($handler, $params) { - return call_user_func($handler, ...$params); + return static function () use ($handler) { + return call_user_func($handler, ...func_get_args()); }; } @@ -391,7 +389,7 @@ class Node if (empty($handlerProviders)) { throw new RequestException('

HTTP 404 Not Found


Powered by Swoole', 404); } - return call_user_func($handlerProviders, \request()); + return call_user_func($handlerProviders[0], ...($handlerProviders[1] ?? [])); } }