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