This commit is contained in:
2023-08-29 22:53:31 +08:00
parent 5067b4c9e0
commit 03c9ef26e8
+42 -38
View File
@@ -11,50 +11,54 @@ class Middleware
{ {
public HashMap $map; /**
* @var array
*/
protected array $manager = [];
/** /**
* *
*/ */
public function __construct() public function __construct()
{ {
$this->map = new HashMap(); }
}
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
* @param string|object $middleware * @param string|object $middleware
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function set(string $className, string $method, string|object $middleware): void public function set(string $className, string $method, string|object $middleware): void
{ {
$path = $className . '::' . $method; $path = $className . '::' . $method;
if ($this->map->has($path)) { if (isset($this->manager[$path])) {
$values = $this->map->get($path); $values = $this->manager[$path];
if (in_array($middleware, $values)) { if (in_array($middleware, $values)) {
return; return;
} }
if (!in_array(MiddlewareInterface::class, class_implements($middleware))) { if (!in_array(MiddlewareInterface::class, class_implements($middleware))) {
return; return;
} }
} } else {
$this->map->append($path, $middleware); $this->manager[$path] = [];
} }
$this->manager[$path][] = $middleware;
}
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
* @return array * @return array
*/ */
public function get(string $className, string $method): array public function get(string $className, string $method): array
{ {
return $this->map->get($className . '::' . $method) ?? []; return $this->manager[$className . '::' . $method] ?? [];
} }
} }