1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gacela\Router\Configure; |
||
6 | |||
7 | use Gacela\Router\Controllers\RedirectController; |
||
8 | use Gacela\Router\Entities\Request; |
||
9 | use Gacela\Router\Entities\Route; |
||
10 | use Gacela\Router\Exceptions\MalformedPathException; |
||
11 | use Gacela\Router\Exceptions\UnsupportedHttpMethodException; |
||
12 | |||
13 | use Gacela\Router\Validators\PathValidator; |
||
14 | |||
15 | use function in_array; |
||
16 | use function is_array; |
||
17 | |||
18 | /** |
||
19 | * @method head(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
20 | * @method connect(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
21 | * @method get(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
22 | * @method post(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
23 | * @method put(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
24 | * @method patch(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
25 | * @method delete(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
26 | * @method options(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
27 | * @method trace(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
28 | * @method any(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null) |
||
29 | */ |
||
30 | final class Routes |
||
31 | { |
||
32 | /** @var list<Route> */ |
||
33 | private array $routes = []; |
||
34 | |||
35 | /** |
||
36 | * @psalm-suppress MixedArgument |
||
37 | */ |
||
38 | 86 | public function __call(string $method, array $arguments): void |
|
39 | { |
||
40 | 86 | if ($method === 'any') { |
|
41 | 9 | $this->addRoute(Request::ALL_METHODS, ...$arguments); |
|
42 | } else { |
||
43 | 77 | $this->addRoute($method, ...$arguments); |
|
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @psalm-suppress MixedArgument |
||
49 | * |
||
50 | * @param string[] $methods |
||
51 | * @param object|class-string $controller |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
52 | */ |
||
53 | 8 | public function match( |
|
54 | array $methods, |
||
55 | string $path, |
||
56 | object|string $controller, |
||
57 | string $action = '__invoke', |
||
58 | ?string $pathPattern = null, |
||
59 | ): void { |
||
60 | 8 | $this->addRoute($methods, $path, $controller, $action, $pathPattern); |
|
61 | } |
||
62 | |||
63 | 15 | public function redirect( |
|
64 | string $uri, |
||
65 | string $destination, |
||
66 | int $status = 302, |
||
67 | string $method = null, |
||
68 | ): void { |
||
69 | 15 | if ($method === null) { |
|
70 | 10 | $this->addRoute(Request::ALL_METHODS, $uri, new RedirectController($destination, $status)); |
|
71 | } else { |
||
72 | 5 | $this->addRoute([$method], $uri, new RedirectController($destination, $status)); |
|
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return list<Route> |
||
78 | */ |
||
79 | 104 | public function getAllRoutes(): array |
|
80 | { |
||
81 | 104 | return $this->routes; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string[]|string $methods |
||
86 | * @param object|class-string $controller |
||
0 ignored issues
–
show
|
|||
87 | */ |
||
88 | 109 | private function addRoute( |
|
89 | array|string $methods, |
||
90 | string $path, |
||
91 | object|string $controller, |
||
92 | string $action = '__invoke', |
||
93 | ?string $pathPattern = null, |
||
94 | ): void { |
||
95 | 109 | if (!PathValidator::isValid($path)) { |
|
96 | 6 | throw MalformedPathException::withPath($path); |
|
97 | } |
||
98 | |||
99 | 103 | if (!is_array($methods)) { |
|
0 ignored issues
–
show
|
|||
100 | 71 | $methods = [$methods]; |
|
101 | } |
||
102 | 103 | $methods = array_map(static fn ($method) => strtoupper($method), $methods); |
|
103 | |||
104 | 103 | $path = ($path === '/') ? '' : $path; |
|
105 | |||
106 | 103 | foreach ($methods as $method) { |
|
107 | 103 | if (!in_array($method, Request::ALL_METHODS, true)) { |
|
108 | 1 | throw UnsupportedHttpMethodException::withName($method); |
|
109 | } |
||
110 | |||
111 | 102 | $this->routes[] = new Route($method, $path, $controller, $action, $pathPattern); |
|
112 | } |
||
113 | } |
||
114 | } |
||
115 |