This commit is contained in:
2023-08-29 22:53:31 +08:00
parent 5067b4c9e0
commit 03c9ef26e8
+10 -6
View File
@@ -11,7 +11,10 @@ class Middleware
{ {
public HashMap $map; /**
* @var array
*/
protected array $manager = [];
/** /**
@@ -19,7 +22,6 @@ class Middleware
*/ */
public function __construct() public function __construct()
{ {
$this->map = new HashMap();
} }
@@ -33,16 +35,18 @@ class Middleware
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->manager[$path] = [];
} }
$this->map->append($path, $middleware); $this->manager[$path][] = $middleware;
} }
@@ -53,7 +57,7 @@ class Middleware
*/ */
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] ?? [];
} }