This commit is contained in:
2021-08-30 18:44:24 +08:00
parent 680480b9dc
commit b0120a2ed2
2 changed files with 26 additions and 6 deletions
+17 -3
View File
@@ -156,10 +156,17 @@ class Node
* @return mixed * @return mixed
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception
*/ */
private function injectControllerMiddleware($method, $handler, $_injectParameters): mixed private function injectControllerMiddleware($method, $handler, $_injectParameters): mixed
{ {
MiddlewareManager::addMiddlewares($handler[0], $handler[1], $this->middleware[$method] ?? []); $middleware = $this->middleware[$method] ?? [];
$allowMiddleware = router()->getMiddleware();
if (!empty($allowMiddleware)){
array_unshift($middleware, $allowMiddleware);
}
MiddlewareManager::addMiddlewares($handler[0], $handler[1], $middleware);
return MiddlewareManager::callerMiddlewares( return MiddlewareManager::callerMiddlewares(
$handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler, $_injectParameters) $handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler, $_injectParameters)
); );
@@ -171,11 +178,18 @@ class Node
* @param $handler * @param $handler
* @param $_injectParameters * @param $_injectParameters
* @return Closure * @return Closure
* @throws Exception
*/ */
private function injectClosureMiddleware($method, $handler, $_injectParameters): Closure private function injectClosureMiddleware($method, $handler, $_injectParameters): Closure
{ {
if (!empty($this->middleware[$method] ?? [])) { $middleware = $this->middleware[$method] ?? [];
return MiddlewareManager::closureMiddlewares($this->middleware[$method],
$allowMiddleware = router()->getMiddleware();
if (!empty($allowMiddleware)){
array_unshift($middleware, $allowMiddleware);
}
if (!empty($middleware)) {
return MiddlewareManager::closureMiddlewares($middleware,
$this->normalHandler($handler, $_injectParameters) $this->normalHandler($handler, $_injectParameters)
); );
} else { } else {
+9 -3
View File
@@ -290,9 +290,6 @@ class Router extends HttpService implements RouterInterface
$node->namespace = $this->loadNamespace($method); $node->namespace = $this->loadNamespace($method);
$name = array_column($this->groupTacks, 'middleware'); $name = array_column($this->groupTacks, 'middleware');
if ($this->middleware instanceof \Closure) {
$node->addMiddleware($method, [$this->middleware]);
}
if (is_array($name)) { if (is_array($name)) {
$node->addMiddleware($method, $this->resolve_middleware($name)); $node->addMiddleware($method, $this->resolve_middleware($name));
} }
@@ -300,6 +297,15 @@ class Router extends HttpService implements RouterInterface
} }
/**
* @return Closure
*/
public function getMiddleware(): Closure
{
return $this->middleware;
}
/** /**
* @param string|array $middleware * @param string|array $middleware
* @return array * @return array