This commit is contained in:
2021-08-30 17:34:57 +08:00
parent 767545935a
commit defcaec071
2 changed files with 14 additions and 15 deletions
+3 -2
View File
@@ -29,10 +29,11 @@ class HandlerProviders extends BaseObject
* @param $method * @param $method
* @param $path * @param $path
* @param $handler * @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];
} }
} }
+11 -13
View File
@@ -130,17 +130,18 @@ class Node
/** /**
* @param string $method * @param string $method
* @param $handler * @param $handler
* @param $_injectParameters
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function injectMiddleware(string $method, $handler): void private function injectMiddleware(string $method, $handler, $_injectParameters): void
{ {
if (!($handler instanceof Closure)) { if (!($handler instanceof Closure)) {
$callback = $this->injectControllerMiddleware($handler); $callback = $this->injectControllerMiddleware($handler);
} else { } else {
$callback = $this->injectClosureMiddleware($handler); $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) { foreach ($this->_handler as $method => $dispatcher) {
if ($dispatcher instanceof Closure) { if ($dispatcher instanceof Closure) {
$this->_injectParameters = $container->resolveFunctionParameters($dispatcher); $_injectParameters = $container->resolveFunctionParameters($dispatcher);
} else { } else {
[$controller, $action] = $dispatcher; [$controller, $action] = $dispatcher;
if (is_object($controller)) { if (is_object($controller)) {
$controller = get_class($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 = []; $this->_handler = [];
return $this; return $this;
@@ -213,10 +214,8 @@ class Node
if (is_null($reflect)) { if (is_null($reflect)) {
return $this->normalHandler($handler); return $this->normalHandler($handler);
} }
return static function () use ($reflect, $handler) {
$params = $this->_injectParameters; return $reflect->invoke($handler, func_get_args());
return static function () use ($reflect, $handler, $params) {
return $reflect->invoke($handler, $params);
}; };
} }
@@ -247,9 +246,8 @@ class Node
*/ */
private function normalHandler($handler): Closure private function normalHandler($handler): Closure
{ {
$params = $this->_injectParameters; return static function () use ($handler) {
return static function () use ($handler, $params) { return call_user_func($handler, ...func_get_args());
return call_user_func($handler, ...$params);
}; };
} }
@@ -391,7 +389,7 @@ class Node
if (empty($handlerProviders)) { if (empty($handlerProviders)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
} }
return call_user_func($handlerProviders, \request()); return call_user_func($handlerProviders[0], ...($handlerProviders[1] ?? []));
} }
} }