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