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 Routes |
27
|
|
|
{ |
28
|
|
|
/** @var list<Route> */ |
29
|
|
|
private array $routes = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @psalm-suppress MixedArgument |
33
|
|
|
*/ |
34
|
53 |
|
public function __call(string $name, array $arguments): void |
35
|
|
|
{ |
36
|
53 |
|
if ($name === 'any') { |
37
|
9 |
|
$this->addRoutesForAllMethods($arguments); |
38
|
|
|
} else { |
39
|
44 |
|
$this->addRouteByName($name, $arguments); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return list<Route> |
45
|
|
|
*/ |
46
|
72 |
|
public function routes(): array |
47
|
|
|
{ |
48
|
72 |
|
return $this->routes; |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
public function redirect( |
52
|
|
|
string $uri, |
53
|
|
|
string $destination, |
54
|
|
|
int $status = 302, |
55
|
|
|
string $method = null, |
56
|
|
|
): void { |
57
|
20 |
|
if ($method === null) { |
58
|
10 |
|
$this->addRoutesForAllMethods([$uri, new RedirectController($destination, $status)]); |
59
|
|
|
} else { |
60
|
10 |
|
$this->addRouteByName($method, [$uri, new RedirectController($destination, $status)]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @psalm-suppress MixedArgument |
66
|
|
|
*/ |
67
|
19 |
|
private function addRoutesForAllMethods(array $arguments): void |
68
|
|
|
{ |
69
|
19 |
|
foreach (Request::ALL_METHODS as $methodName) { |
70
|
19 |
|
$this->routes[] = $this->createRoute($methodName, ...$arguments); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @psalm-suppress MixedArgument |
76
|
|
|
*/ |
77
|
54 |
|
private function addRouteByName(string $httpMethod, array $arguments): void |
78
|
|
|
{ |
79
|
54 |
|
$route = $this->createRoute(strtoupper(trim($httpMethod)), ...$arguments); |
80
|
|
|
|
81
|
53 |
|
$this->routes[] = $route; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param object|class-string $controller |
|
|
|
|
86
|
|
|
*/ |
87
|
73 |
|
private function createRoute( |
88
|
|
|
string $httpMethod, |
89
|
|
|
string $path, |
90
|
|
|
object|string $controller, |
91
|
|
|
string $action = '__invoke', |
92
|
|
|
): Route { |
93
|
73 |
|
if (!in_array($httpMethod, Request::ALL_METHODS)) { |
94
|
1 |
|
throw new UnsupportedHttpMethodException($httpMethod); |
95
|
|
|
} |
96
|
72 |
|
$path = ($path === '/') ? '' : $path; |
97
|
|
|
|
98
|
72 |
|
return new Route($httpMethod, $path, $controller, $action); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.