1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Router; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @method get(string $path, object|string $controller, string $action = '__invoke') |
9
|
|
|
* @method head(string $path, object|string $controller, string $action = '__invoke') |
10
|
|
|
* @method connect(string $path, object|string $controller, string $action = '__invoke') |
11
|
|
|
* @method post(string $path, object|string $controller, string $action = '__invoke') |
12
|
|
|
* @method delete(string $path, object|string $controller, string $action = '__invoke') |
13
|
|
|
* @method options(string $path, object|string $controller, string $action = '__invoke') |
14
|
|
|
* @method patch(string $path, object|string $controller, string $action = '__invoke') |
15
|
|
|
* @method put(string $path, object|string $controller, string $action = '__invoke') |
16
|
|
|
* @method trace(string $path, object|string $controller, string $action = '__invoke') |
17
|
|
|
* @method any(string $path, object|string $controller, string $action = '__invoke') |
18
|
|
|
*/ |
19
|
|
|
final class RoutingConfigurator |
20
|
|
|
{ |
21
|
|
|
/** @var list<Route> */ |
22
|
|
|
private array $routes = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @psalm-suppress MixedArgument |
26
|
|
|
*/ |
27
|
51 |
|
public function __call(string $name, array $arguments): void |
28
|
|
|
{ |
29
|
51 |
|
if ($name === 'any') { |
30
|
9 |
|
$this->addRoutesForAllMethods($arguments); |
31
|
|
|
} else { |
32
|
42 |
|
$this->addRouteByName($name, $arguments); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return list<Route> |
38
|
|
|
*/ |
39
|
51 |
|
public function routes(): array |
40
|
|
|
{ |
41
|
51 |
|
return $this->routes; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @psalm-suppress MixedArgument |
46
|
|
|
*/ |
47
|
9 |
|
private function addRoutesForAllMethods(array $arguments): void |
48
|
|
|
{ |
49
|
9 |
|
foreach (Request::ALL_METHODS as $methodName) { |
50
|
9 |
|
$this->routes[] = $this->createRoute($methodName, ...$arguments); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @psalm-suppress MixedArgument |
56
|
|
|
*/ |
57
|
42 |
|
private function addRouteByName(string $name, array $arguments): void |
58
|
|
|
{ |
59
|
42 |
|
$this->routes[] = match ($name) { |
60
|
42 |
|
'head' => $this->createRoute(Request::METHOD_HEAD, ...$arguments), |
61
|
42 |
|
'connect' => $this->createRoute(Request::METHOD_CONNECT, ...$arguments), |
62
|
42 |
|
'post' => $this->createRoute(Request::METHOD_POST, ...$arguments), |
63
|
42 |
|
'delete' => $this->createRoute(Request::METHOD_DELETE, ...$arguments), |
64
|
42 |
|
'options' => $this->createRoute(Request::METHOD_OPTIONS, ...$arguments), |
65
|
42 |
|
'patch' => $this->createRoute(Request::METHOD_PATCH, ...$arguments), |
66
|
42 |
|
'put' => $this->createRoute(Request::METHOD_PUT, ...$arguments), |
67
|
42 |
|
'trace' => $this->createRoute(Request::METHOD_TRACE, ...$arguments), |
68
|
42 |
|
default => $this->createRoute(Request::METHOD_GET, ...$arguments), |
69
|
42 |
|
}; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param object|class-string $controller |
|
|
|
|
74
|
|
|
*/ |
75
|
51 |
|
private function createRoute( |
76
|
|
|
string $method, |
77
|
|
|
string $path, |
78
|
|
|
object|string $controller, |
79
|
|
|
string $action = '__invoke', |
80
|
|
|
): Route { |
81
|
51 |
|
$path = ($path === '/') ? '' : $path; |
82
|
|
|
|
83
|
51 |
|
return new Route($method, $path, $controller, $action); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|