This commit is contained in:
2025-12-30 20:00:50 +08:00
parent 45ed584435
commit 1b08ae2be3
4 changed files with 53 additions and 16 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ class Middleware implements InjectMethodInterface
*/ */
public function dispatch(string $class, string $method): void public function dispatch(string $class, string $method): void
{ {
MiddlewareManager::set($class, $method, $this->middleware); // MiddlewareManager::set($class, $method, $this->middleware);
} }
+23
View File
@@ -27,6 +27,11 @@ class Handler implements RequestHandlerInterface
protected array $methods = []; protected array $methods = [];
/**
* @var array
*/
protected array $middlewares = [];
/** /**
* @param array|Closure $handler * @param array|Closure $handler
* @param array $parameters * @param array $parameters
@@ -100,6 +105,24 @@ class Handler implements RequestHandlerInterface
return $this->handler[1]; return $this->handler[1];
} }
/**
* @param array $middlewares
* @return void
*/
public function setMiddlewares(array $middlewares): void
{
$this->middlewares = $middlewares;
}
/**
* @return array
*/
public function getMiddlewares(): array
{
return $this->middlewares;
}
/** /**
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
+1 -1
View File
@@ -209,7 +209,7 @@ class Router
{ {
$router = $container->get(DataGrip::class)->get(static::$type); $router = $container->get(DataGrip::class)->get(static::$type);
foreach ($router->getMethods() as $name => $method) { foreach ($router->getMethods() as $name => $method) {
$middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod()); $middlewares = $method->getMiddlewares();
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod()); $validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
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()));
+28 -14
View File
@@ -8,6 +8,7 @@ namespace Kiri\Router;
use Closure; use Closure;
use Kiri\Router\Base\NotFoundController; use Kiri\Router\Base\NotFoundController;
use Kiri\Router\Constrict\RequestMethod; use Kiri\Router\Constrict\RequestMethod;
use ReflectionException;
use Throwable; use Throwable;
use Traversable; use Traversable;
use Kiri\Router\Base\Middleware; use Kiri\Router\Base\Middleware;
@@ -204,47 +205,60 @@ 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;
$this->registerMiddleware($handler->getClass(), $handler->getMethod()); $middlewares = $this->registerMiddleware($handler->getClass(), $handler->getMethod());
if (count($middlewares) > 0) {
$handler->setMiddlewares($middlewares);
}
} }
/** /**
* @param string $class * @param string $class
* @param string $method * @param string $method
* @return void * @return array
* @throws * @throws ReflectionException
*/ */
public function registerMiddleware(string $class, string $method): void public function registerMiddleware(string $class, string $method): array
{ {
$response = [];
$middlewares = \request()->middlewares; $middlewares = \request()->middlewares;
var_dump($class . '::' . $method . '('.json_encode($middlewares,JSON_UNESCAPED_UNICODE).')');
if (count($middlewares) > 0) { if (count($middlewares) > 0) {
$this->appendMiddleware($middlewares, $class, $method); $response = $this->appendMiddleware($response, $middlewares);
} }
$middlewares = array_column($this->groupTack, 'middleware'); $middlewares = array_column($this->groupTack, 'middleware');
if (count($middlewares) > 0) { $response = $this->appendMiddleware($response, $middlewares);
$this->appendMiddleware($middlewares, $class, $method);
$reflect = \Kiri::getDi()->getReflectionClass($class);
$attributes = $reflect->getMethod($method)->getAttributes(Annotate\Middleware::class);
foreach ($attributes as $attribute) {
if (!in_array($attribute->getName(), $response)) {
$response[] = $attribute->getName();
}
} }
return $response;
} }
/** /**
* @param array $response
* @param array $middlewares * @param array $middlewares
* @param $class * @return array
* @param $method
* @return void
* @throws
*/ */
private function appendMiddleware(array $middlewares, $class, $method): void private function appendMiddleware(array $response, array $middlewares): array
{ {
foreach ($middlewares as $middleware) { foreach ($middlewares as $middleware) {
if (is_string($middleware)) { if (is_string($middleware)) {
$middleware = [$middleware]; $middleware = [$middleware];
} }
foreach ($middleware as $value) { foreach ($middleware as $value) {
Middleware::set($class, $method, $value); if (!in_array($value, $response)) {
$response[] = $value;
}
} }
} }
return $response;
} }
/** /**