This commit is contained in:
2021-08-30 18:17:37 +08:00
parent 4015bf30e6
commit a0e5eb03d2
3 changed files with 41 additions and 33 deletions
+2 -3
View File
@@ -29,11 +29,10 @@ 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, $_injectParameters) public static function add($method, $path, $handler)
{ {
static::$handlers[$method][$path] = [$handler, $_injectParameters]; static::$handlers[$method][$path] = $handler;
} }
} }
+37 -28
View File
@@ -45,7 +45,11 @@ class Node
public string $htmlSuffix = '.html'; public string $htmlSuffix = '.html';
public bool $enableHtmlSuffix = false; public bool $enableHtmlSuffix = false;
/** @var array<string,mixed> */
public array $namespace = []; public array $namespace = [];
/** @var array<string,mixed> */
public array $middleware = []; public array $middleware = [];
public string $sourcePath = ''; public string $sourcePath = '';
@@ -137,46 +141,49 @@ class Node
private function injectMiddleware(string $method, $handler, $_injectParameters): void private function injectMiddleware(string $method, $handler, $_injectParameters): void
{ {
if (!($handler instanceof Closure)) { if (!($handler instanceof Closure)) {
$callback = $this->injectControllerMiddleware($handler); $callback = $this->injectControllerMiddleware($method, $handler, $_injectParameters);
} else { } 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 $handler
* @param $_injectParameters
* @return mixed * @return mixed
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @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( 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 $handler
* @param $_injectParameters
* @return Closure * @return Closure
*/ */
private function injectClosureMiddleware($handler): Closure private function injectClosureMiddleware($method, $handler, $_injectParameters): Closure
{ {
if (!empty($this->middleware)) { if (!empty($this->middleware[$method] ?? [])) {
return MiddlewareManager::closureMiddlewares($this->middleware, $this->normalHandler($handler)); return MiddlewareManager::closureMiddlewares($this->middleware[$method],
$this->normalHandler($handler, $_injectParameters)
);
} else { } else {
return $this->normalHandler($handler); return $this->normalHandler($handler, $_injectParameters);
} }
} }
private ?array $_injectParameters = [];
/** /**
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
@@ -207,15 +214,16 @@ class Node
/** /**
* @param IAspect|null $reflect * @param IAspect|null $reflect
* @param $handler * @param $handler
* @param $_injectParameters
* @return Closure * @return Closure
*/ */
#[Pure] private function aopHandler(?IAspect $reflect, $handler): Closure #[Pure] private function aopHandler(?IAspect $reflect, $handler, $_injectParameters): Closure
{ {
if (is_null($reflect)) { if (is_null($reflect)) {
return $this->normalHandler($handler); return $this->normalHandler($handler, $_injectParameters);
} }
return static function () use ($reflect, $handler) { return static function () use ($reflect, $handler, $_injectParameters) {
return $reflect->invoke($handler, func_get_args()); return $reflect->invoke($handler, $_injectParameters);
}; };
} }
@@ -242,12 +250,13 @@ class Node
/** /**
* @param $handler * @param $handler
* @param $_injectParameters
* @return Closure * @return Closure
*/ */
private function normalHandler($handler): Closure private function normalHandler($handler, $_injectParameters): Closure
{ {
return static function () use ($handler) { return static function () use ($handler, $_injectParameters) {
return call_user_func($handler, ...func_get_args()); return call_user_func($handler, ...$_injectParameters);
}; };
} }
@@ -351,17 +360,21 @@ class Node
/** /**
* @param $method
* @param Closure|array $class * @param Closure|array $class
* @return $this * @return $this
*/ */
public function addMiddleware(Closure|array $class): static public function addMiddleware($method, Closure|array $class): static
{ {
if (empty($class)) return $this; if (empty($class)) return $this;
if (!isset($this->middleware[$method])) {
$this->middleware[$method] = [];
}
foreach ($class as $closure) { foreach ($class as $closure) {
if (in_array($closure, $this->middleware)) { if (in_array($closure, $this->middleware[$method])) {
continue; continue;
} }
$this->middleware[] = $closure; $this->middleware[$method][] = $closure;
} }
return $this; return $this;
} }
@@ -389,11 +402,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);
} }
if (!empty($handlerProviders[1])) { return call_user_func($handlerProviders);
var_dump($handlerProviders[1]);
return call_user_func($handlerProviders[0], ...$handlerProviders[1]);
}
return call_user_func($handlerProviders[0]);
} }
} }
+2 -2
View File
@@ -291,10 +291,10 @@ class Router extends HttpService implements RouterInterface
$name = array_column($this->groupTacks, 'middleware'); $name = array_column($this->groupTacks, 'middleware');
if ($this->middleware instanceof \Closure) { if ($this->middleware instanceof \Closure) {
$node->addMiddleware([$this->middleware]); $node->addMiddleware($method, [$this->middleware]);
} }
if (is_array($name)) { if (is_array($name)) {
$node->addMiddleware($this->resolve_middleware($name)); $node->addMiddleware($method, $this->resolve_middleware($name));
} }
return $node; return $node;
} }