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-04-15 23:31:16 +08:00
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
protected array $manager = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param string|object $middleware
|
|
|
|
|
* @return void
|
2023-12-12 15:35:35 +08:00
|
|
|
* @throws
|
2023-08-29 22:53:31 +08:00
|
|
|
*/
|
|
|
|
|
public function set(string $className, string $method, string|object $middleware): void
|
|
|
|
|
{
|
|
|
|
|
$path = $className . '::' . $method;
|
|
|
|
|
if (isset($this->manager[$path])) {
|
|
|
|
|
$values = $this->manager[$path];
|
|
|
|
|
if (in_array($middleware, $values)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!in_array(MiddlewareInterface::class, class_implements($middleware))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->manager[$path] = [];
|
|
|
|
|
}
|
|
|
|
|
$this->manager[$path][] = $middleware;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function get(string $className, string $method): array
|
|
|
|
|
{
|
|
|
|
|
return $this->manager[$className . '::' . $method] ?? [];
|
|
|
|
|
}
|
2023-04-16 12:44:43 +08:00
|
|
|
|
|
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|