|
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
|
|
|
use function is_array; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @method head(string $path, object|string $controller, string $action = '__invoke') |
|
17
|
|
|
* @method connect(string $path, object|string $controller, string $action = '__invoke') |
|
18
|
|
|
* @method get(string $path, object|string $controller, string $action = '__invoke') |
|
19
|
|
|
* @method post(string $path, object|string $controller, string $action = '__invoke') |
|
20
|
|
|
* @method put(string $path, object|string $controller, string $action = '__invoke') |
|
21
|
|
|
* @method patch(string $path, object|string $controller, string $action = '__invoke') |
|
22
|
|
|
* @method delete(string $path, object|string $controller, string $action = '__invoke') |
|
23
|
|
|
* @method options(string $path, object|string $controller, string $action = '__invoke') |
|
24
|
|
|
* @method trace(string $path, object|string $controller, string $action = '__invoke') |
|
25
|
|
|
* @method any(string $path, object|string $controller, string $action = '__invoke') |
|
26
|
|
|
*/ |
|
27
|
|
|
final class Routes |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var list<Route> */ |
|
30
|
|
|
private array $routes = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @psalm-suppress MixedArgument |
|
34
|
|
|
*/ |
|
35
|
54 |
|
public function __call(string $method, array $arguments): void |
|
36
|
|
|
{ |
|
37
|
54 |
|
if ($method === 'any') { |
|
38
|
9 |
|
$this->addRoute(Request::ALL_METHODS, ...$arguments); |
|
39
|
|
|
} else { |
|
40
|
45 |
|
$this->addRoute($method, ...$arguments); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @psalm-suppress MixedArgument |
|
46
|
|
|
* |
|
47
|
|
|
* @param string[] $methods |
|
48
|
|
|
* @param object|class-string $controller |
|
|
|
|
|
|
49
|
|
|
*/ |
|
50
|
8 |
|
public function match(array $methods, string $path, object|string $controller, string $action = '__invoke'): void |
|
51
|
|
|
{ |
|
52
|
8 |
|
$this->addRoute($methods, $path, $controller, $action); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
20 |
|
public function redirect( |
|
56
|
|
|
string $uri, |
|
57
|
|
|
string $destination, |
|
58
|
|
|
int $status = 302, |
|
59
|
|
|
string $method = null, |
|
60
|
|
|
): void { |
|
61
|
20 |
|
if ($method === null) { |
|
62
|
10 |
|
$this->addRoute(Request::ALL_METHODS, $uri, new RedirectController($destination, $status)); |
|
63
|
|
|
} else { |
|
64
|
10 |
|
$this->addRoute([$method], $uri, new RedirectController($destination, $status)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return list<Route> |
|
70
|
|
|
*/ |
|
71
|
81 |
|
public function routes(): array |
|
72
|
|
|
{ |
|
73
|
81 |
|
return $this->routes; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string[]|string $methods |
|
78
|
|
|
* @param object|class-string $controller |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
82 |
|
private function addRoute( |
|
81
|
|
|
array|string $methods, |
|
82
|
|
|
string $path, |
|
83
|
|
|
object|string $controller, |
|
84
|
|
|
string $action = '__invoke', |
|
85
|
|
|
): void { |
|
86
|
82 |
|
if (!is_array($methods)) { |
|
|
|
|
|
|
87
|
45 |
|
$methods = [$methods]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
82 |
|
$methods = array_map(static fn ($method) => strtoupper(trim($method)), $methods); |
|
91
|
|
|
|
|
92
|
82 |
|
foreach ($methods as $method) { |
|
93
|
82 |
|
if (!in_array($method, Request::ALL_METHODS, true)) { |
|
94
|
1 |
|
throw new UnsupportedHttpMethodException($method); |
|
95
|
|
|
} |
|
96
|
81 |
|
$path = ($path === '/') ? '' : $path; |
|
97
|
|
|
|
|
98
|
81 |
|
$this->routes[] = new Route($method, $path, $controller, $action); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|