2023-04-15 23:29:27 +08:00
|
|
|
<?php
|
2023-04-16 01:24:30 +08:00
|
|
|
declare(strict_types=1);
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
namespace Kiri\Router\Base;
|
|
|
|
|
|
2023-12-19 17:50:59 +08:00
|
|
|
use Kiri;
|
2024-08-29 17:01:08 +08:00
|
|
|
use Kiri\Router\Validator\Validator;
|
2024-08-29 17:09:26 +08:00
|
|
|
use Kiri\Router\Validator\ValidatorMiddleware;
|
2023-04-15 23:31:16 +08:00
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
class Middleware
|
|
|
|
|
{
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
|
2023-08-29 22:53:31 +08:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-12-19 17:50:59 +08:00
|
|
|
protected static array $mapping = [];
|
2023-08-29 22:53:31 +08:00
|
|
|
|
|
|
|
|
|
2024-08-29 17:01:08 +08:00
|
|
|
protected static array $validators = [];
|
|
|
|
|
|
2023-08-29 22:53:31 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $method
|
2023-12-19 17:50:59 +08:00
|
|
|
* @param string $middleware
|
2023-08-29 22:53:31 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2024-08-29 15:50:28 +08:00
|
|
|
public static function set(string $className, string $method, string $middleware): void
|
2023-08-29 22:53:31 +08:00
|
|
|
{
|
|
|
|
|
$path = $className . '::' . $method;
|
2024-08-29 16:24:45 +08:00
|
|
|
if (!isset(static::$mapping[$path])) {
|
2024-08-29 16:19:24 +08:00
|
|
|
static::$mapping[$path] = [];
|
2023-12-19 17:50:59 +08:00
|
|
|
}
|
2024-08-29 15:50:28 +08:00
|
|
|
if (!in_array($middleware, static::$mapping[$path])) {
|
|
|
|
|
static::$mapping[$path][] = $middleware;
|
2023-08-29 22:53:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-12-19 17:50:59 +08:00
|
|
|
public static function get(string $className, string $method): array
|
2023-08-29 22:53:31 +08:00
|
|
|
{
|
2024-08-29 16:25:25 +08:00
|
|
|
return static::$mapping[$className . '::' . $method] ?? [];
|
2023-08-29 22:53:31 +08:00
|
|
|
}
|
2023-04-16 12:44:43 +08:00
|
|
|
|
|
|
|
|
|
2024-08-29 17:01:08 +08:00
|
|
|
/**
|
2024-08-29 17:09:26 +08:00
|
|
|
* @param string $class
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param Validator $validators
|
2024-08-29 17:01:08 +08:00
|
|
|
*/
|
2024-08-29 17:09:26 +08:00
|
|
|
public static function setValidator(string $class, string $method, Validator $validators): void
|
2024-08-29 17:01:08 +08:00
|
|
|
{
|
2024-08-29 17:09:26 +08:00
|
|
|
self::$validators[$class . '::' . $method] = $validators;
|
2024-08-29 17:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-29 17:09:26 +08:00
|
|
|
* @param string $class
|
|
|
|
|
* @param string $method
|
2024-08-29 17:01:08 +08:00
|
|
|
* @return Validator|null
|
|
|
|
|
*/
|
2024-08-29 17:09:26 +08:00
|
|
|
public static function getValidator(string $class, string $method): ?Validator
|
2024-08-29 17:01:08 +08:00
|
|
|
{
|
2024-08-29 17:09:26 +08:00
|
|
|
return static::$validators[$class . '::' . $method] ?? null;
|
2024-08-29 17:01:08 +08:00
|
|
|
}
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|