|
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
|
|
|
*/ |
|
18
|
|
|
final class RoutingConfigurator |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var list<Route> */ |
|
21
|
|
|
private array $routes = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @psalm-suppress MixedArgument |
|
25
|
|
|
*/ |
|
26
|
42 |
|
public function __call(string $name, array $arguments): void |
|
27
|
|
|
{ |
|
28
|
42 |
|
$this->routes[] = match ($name) { |
|
29
|
42 |
|
'head' => $this->route(Request::METHOD_HEAD, ...$arguments), |
|
30
|
42 |
|
'connect' => $this->route(Request::METHOD_CONNECT, ...$arguments), |
|
31
|
42 |
|
'post' => $this->route(Request::METHOD_POST, ...$arguments), |
|
32
|
42 |
|
'delete' => $this->route(Request::METHOD_DELETE, ...$arguments), |
|
33
|
42 |
|
'options' => $this->route(Request::METHOD_OPTIONS, ...$arguments), |
|
34
|
42 |
|
'patch' => $this->route(Request::METHOD_PATCH, ...$arguments), |
|
35
|
42 |
|
'put' => $this->route(Request::METHOD_PUT, ...$arguments), |
|
36
|
42 |
|
'trace' => $this->route(Request::METHOD_TRACE, ...$arguments), |
|
37
|
42 |
|
default => $this->route(Request::METHOD_GET, ...$arguments), |
|
38
|
42 |
|
}; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return list<Route> |
|
43
|
|
|
*/ |
|
44
|
42 |
|
public function routes(): array |
|
45
|
|
|
{ |
|
46
|
42 |
|
return $this->routes; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param object|class-string $controller |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
42 |
|
private function route( |
|
53
|
|
|
string $method, |
|
54
|
|
|
string $path, |
|
55
|
|
|
object|string $controller, |
|
56
|
|
|
string $action = '__invoke', |
|
57
|
|
|
): Route { |
|
58
|
42 |
|
$path = ($path === '/') ? '' : $path; |
|
59
|
|
|
|
|
60
|
42 |
|
return new Route($method, $path, $controller, $action); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|