1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Router; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @method head(string $path, object|string $controller, string $action = '__invoke') |
9
|
|
|
* @method connect(string $path, object|string $controller, string $action = '__invoke') |
10
|
|
|
* @method get(string $path, object|string $controller, string $action = '__invoke') |
11
|
|
|
* @method post(string $path, object|string $controller, string $action = '__invoke') |
12
|
|
|
* @method put(string $path, object|string $controller, string $action = '__invoke') |
13
|
|
|
* @method patch(string $path, object|string $controller, string $action = '__invoke') |
14
|
|
|
* @method delete(string $path, object|string $controller, string $action = '__invoke') |
15
|
|
|
* @method options(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
|
|
|
/** @var array<class-string, callable|class-string|object> */ |
|
|
|
|
25
|
|
|
private array $mappingInterfaces = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @psalm-suppress MixedArgument |
29
|
|
|
*/ |
30
|
53 |
|
public function __call(string $name, array $arguments): void |
31
|
|
|
{ |
32
|
53 |
|
if ($name === 'any') { |
33
|
9 |
|
$this->addRoutesForAllMethods($arguments); |
34
|
|
|
} else { |
35
|
44 |
|
$this->addRouteByName($name, $arguments); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return list<Route> |
41
|
|
|
*/ |
42
|
52 |
|
public function routes(): array |
43
|
|
|
{ |
44
|
52 |
|
return $this->routes; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array<class-string, callable|class-string|object> $array |
|
|
|
|
49
|
|
|
*/ |
50
|
1 |
|
public function setMappingInterfaces(array $array): self |
51
|
|
|
{ |
52
|
1 |
|
$this->mappingInterfaces = $array; |
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array<class-string, callable|class-string|object> |
|
|
|
|
58
|
|
|
*/ |
59
|
50 |
|
public function getMappingInterfaces(): array |
60
|
|
|
{ |
61
|
50 |
|
return $this->mappingInterfaces; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @psalm-suppress MixedArgument |
66
|
|
|
*/ |
67
|
9 |
|
private function addRoutesForAllMethods(array $arguments): void |
68
|
|
|
{ |
69
|
9 |
|
foreach (Request::ALL_METHODS as $methodName) { |
70
|
9 |
|
$this->routes[] = $this->createRoute($methodName, ...$arguments); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @psalm-suppress MixedArgument |
76
|
|
|
*/ |
77
|
44 |
|
private function addRouteByName(string $name, array $arguments): void |
78
|
|
|
{ |
79
|
44 |
|
$this->routes[] = match ($name) { |
80
|
44 |
|
'head' => $this->createRoute(Request::METHOD_HEAD, ...$arguments), |
81
|
44 |
|
'connect' => $this->createRoute(Request::METHOD_CONNECT, ...$arguments), |
82
|
44 |
|
'get' => $this->createRoute(Request::METHOD_GET, ...$arguments), |
83
|
44 |
|
'post' => $this->createRoute(Request::METHOD_POST, ...$arguments), |
84
|
44 |
|
'delete' => $this->createRoute(Request::METHOD_DELETE, ...$arguments), |
85
|
44 |
|
'options' => $this->createRoute(Request::METHOD_OPTIONS, ...$arguments), |
86
|
44 |
|
'patch' => $this->createRoute(Request::METHOD_PATCH, ...$arguments), |
87
|
44 |
|
'put' => $this->createRoute(Request::METHOD_PUT, ...$arguments), |
88
|
44 |
|
'trace' => $this->createRoute(Request::METHOD_TRACE, ...$arguments), |
89
|
44 |
|
default => throw new UnsupportedHttpMethodException($name), |
90
|
44 |
|
}; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param object|class-string $controller |
|
|
|
|
95
|
|
|
*/ |
96
|
52 |
|
private function createRoute( |
97
|
|
|
string $method, |
98
|
|
|
string $path, |
99
|
|
|
object|string $controller, |
100
|
|
|
string $action = '__invoke', |
101
|
|
|
): Route { |
102
|
52 |
|
$path = ($path === '/') ? '' : $path; |
103
|
|
|
|
104
|
52 |
|
return new Route($method, $path, $controller, $action); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|