This commit is contained in:
2025-12-30 20:21:44 +08:00
parent 01ad7d7416
commit ea3eacdb84
2 changed files with 14 additions and 16 deletions
+9 -5
View File
@@ -37,7 +37,7 @@ class Router
/** /**
* @param string $name * @param string $name
* @param Closure $closure * @param Closure $closure
*/ */
public static function addServer(string $name, Closure $closure): void public static function addServer(string $name, Closure $closure): void
@@ -150,8 +150,8 @@ class Router
/** /**
* @param array|RequestMethod $methods * @param array|RequestMethod $methods
* @param string $route * @param string $route
* @param string|array $handler * @param string|array $handler
*/ */
public static function addRoute(array|RequestMethod $methods, string $route, string|array $handler): void public static function addRoute(array|RequestMethod $methods, string $route, string|array $handler): void
{ {
@@ -164,7 +164,7 @@ class Router
/** /**
* @param array $config * @param array $config
* @param Closure $closure * @param Closure $closure
* *
* @throws * @throws
@@ -211,11 +211,15 @@ class Router
foreach ($router->getMethods() as $name => $method) { foreach ($router->getMethods() as $name => $method) {
$middlewares = $method->getMiddlewares(); $middlewares = $method->getMiddlewares();
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod()); $validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
var_dump(sprintf("%s::%s(%s)", $method->getClass(), $method->getMethod(), json_encode($middlewares, JSON_UNESCAPED_UNICODE)));
if (!is_null($validator)) { if (!is_null($validator)) {
array_unshift($middlewares, new ValidatorMiddleware(di(ResponseInterface::class), $method->getClass(), $method->getMethod())); array_unshift($middlewares, new ValidatorMiddleware(di(ResponseInterface::class), $method->getClass(), $method->getMethod()));
} }
// var_dump(sprintf("%s::%s(%s)",$method->getClass(), $method->getMethod(), json_encode($middlewares,JSON_UNESCAPED_UNICODE)));
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method)); $router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
} }
} }
+5 -11
View File
@@ -206,10 +206,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
public function register(string $path, string $method, Handler $handler): void public function register(string $path, string $method, Handler $handler): void
{ {
$this->methods[$method . '_' . $path] = $handler; $this->methods[$method . '_' . $path] = $handler;
$middlewares = $this->registerMiddleware($handler->getClass(), $handler->getMethod()); $handler->setMiddlewares($this->registerMiddleware($handler->getClass(), $handler->getMethod()));
if (count($middlewares) > 0) {
$handler->setMiddlewares($middlewares);
}
} }
@@ -229,7 +226,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
$middlewares = array_column($this->groupTack, 'middleware'); $middlewares = array_column($this->groupTack, 'middleware');
$response = $this->appendMiddleware($response, $middlewares); $response = $this->appendMiddleware($response, $middlewares);
$reflect = \Kiri::getDi()->getReflectionClass($class); $reflect = \Kiri::getDi()->getReflectionClass($class);
$attributes = $reflect->getMethod($method)->getAttributes(Annotate\Middleware::class); $attributes = $reflect->getMethod($method)->getAttributes(Annotate\Middleware::class);
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
@@ -239,18 +236,15 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
$data = $instance->middleware; $data = $instance->middleware;
if (is_string($data)) { if (is_string($data)) {
$data = [$data]; $data = [$data];
} }
foreach ($data as $middleware) { foreach ($data as $middleware) {
if (!in_array($middleware, $response)) { if (!in_array($middleware, $response)) {
$response[] = $middleware; $response[] = $middleware;
} }
} }
} }
return $response;
var_dump($class.'::'.$method. '(' . json_encode($response, JSON_UNESCAPED_UNICODE) . ')');
return $response;
} }