gacela-project /
router
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gacela\Router\Configure; |
||
| 6 | |||
| 7 | use Gacela\Router\Middleware\MiddlewareInterface; |
||
| 8 | |||
| 9 | final class Middlewares |
||
| 10 | { |
||
| 11 | /** @var list<MiddlewareInterface|class-string<MiddlewareInterface>|string> */ |
||
| 12 | private array $globalMiddlewares = []; |
||
| 13 | |||
| 14 | /** @var array<string, list<MiddlewareInterface|class-string<MiddlewareInterface>>> */ |
||
| 15 | private array $groups = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param MiddlewareInterface|class-string<MiddlewareInterface>|string $middleware |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 19 | */ |
||
| 20 | public function add(MiddlewareInterface|string $middleware): self |
||
| 21 | { |
||
| 22 | $this->globalMiddlewares[] = $middleware; |
||
| 23 | return $this; |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param list<MiddlewareInterface|class-string<MiddlewareInterface>> $middlewares |
||
| 28 | */ |
||
| 29 | public function group(string $name, array $middlewares): self |
||
| 30 | { |
||
| 31 | $this->groups[$name] = $middlewares; |
||
| 32 | return $this; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return list<MiddlewareInterface|class-string<MiddlewareInterface>|string> |
||
| 37 | */ |
||
| 38 | public function getAll(): array |
||
| 39 | { |
||
| 40 | return $this->globalMiddlewares; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return array<string, list<MiddlewareInterface|class-string<MiddlewareInterface>>> |
||
| 45 | */ |
||
| 46 | public function getGroups(): array |
||
| 47 | { |
||
| 48 | return $this->groups; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param MiddlewareInterface|class-string<MiddlewareInterface>|string $middleware |
||
|
0 ignored issues
–
show
|
|||
| 53 | * |
||
| 54 | * @return list<MiddlewareInterface|class-string<MiddlewareInterface>> |
||
| 55 | */ |
||
| 56 | public function resolve(MiddlewareInterface|string $middleware): array |
||
| 57 | { |
||
| 58 | // If it's already an instance, return it as-is |
||
| 59 | if ($middleware instanceof MiddlewareInterface) { |
||
| 60 | return [$middleware]; |
||
|
0 ignored issues
–
show
|
|||
| 61 | } |
||
| 62 | |||
| 63 | // If it's a group name, return the group's middlewares |
||
| 64 | if (isset($this->groups[$middleware])) { |
||
| 65 | return $this->groups[$middleware]; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Otherwise, treat it as a class string |
||
| 69 | /** @var class-string<MiddlewareInterface> $middleware */ |
||
| 70 | return [$middleware]; |
||
|
0 ignored issues
–
show
|
|||
| 71 | } |
||
| 72 | } |
||
| 73 |