This commit is contained in:
xl
2024-08-29 17:09:26 +08:00
parent c435af1156
commit 080cd6ab92
3 changed files with 16 additions and 14 deletions
+10 -7
View File
@@ -5,6 +5,7 @@ namespace Kiri\Router\Base;
use Kiri; use Kiri;
use Kiri\Router\Validator\Validator; use Kiri\Router\Validator\Validator;
use Kiri\Router\Validator\ValidatorMiddleware;
class Middleware class Middleware
{ {
@@ -48,21 +49,23 @@ class Middleware
/** /**
* @param string $path * @param string $class
* @param array $validators * @param string $method
* @param Validator $validators
*/ */
public static function setValidator(string $path, array $validators): void public static function setValidator(string $class, string $method, Validator $validators): void
{ {
self::$validators[$path] = $validators; self::$validators[$class . '::' . $method] = $validators;
} }
/** /**
* @param string $path * @param string $class
* @param string $method
* @return Validator|null * @return Validator|null
*/ */
public static function getValidator(string $path): ?Validator public static function getValidator(string $class, string $method): ?Validator
{ {
return static::$validators[$path] ?? null; return static::$validators[$class . '::' . $method] ?? null;
} }
} }
+5 -3
View File
@@ -190,10 +190,12 @@ class Router
public function reset(ContainerInterface $container): void public function reset(ContainerInterface $container): void
{ {
$router = $container->get(DataGrip::class)->get(static::$type); $router = $container->get(DataGrip::class)->get(static::$type);
$middleware = $container->get(MiddlewareManager::class);
foreach ($router->getMethods() as $name => $method) { foreach ($router->getMethods() as $name => $method) {
$middlewares = $middleware->get($method->getClass(), $method->getMethod()); $middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod());
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
if (!is_null($validator)) {
$middlewares = array_unshift($middlewares, $validator);
}
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method)); $router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
} }
} }
+1 -4
View File
@@ -54,10 +54,7 @@ class BindForm implements InjectParameterInterface
} }
} }
$middleware = \instance(ValidatorMiddleware::class); Middleware::setValidator($class, $method, $validator);
$middleware->validator = $validator;
Middleware::set($class, $method, $middleware);
return $validator->getFormData(); return $validator->getFormData();
} }