1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Router; |
6
|
|
|
|
7
|
|
|
final class Route |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param object|class-string $controller |
|
|
|
|
11
|
|
|
*/ |
12
|
39 |
|
public static function get( |
13
|
|
|
string $path, |
14
|
|
|
object|string $controller, |
15
|
|
|
string $action = '__invoke', |
16
|
|
|
): void { |
17
|
39 |
|
self::runRoute(Request::METHOD_GET, $path, $controller, $action); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param object|class-string $controller |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public static function head( |
24
|
|
|
string $path, |
25
|
|
|
object|string $controller, |
26
|
|
|
string $action = '__invoke', |
27
|
|
|
): void { |
28
|
|
|
self::runRoute(Request::METHOD_HEAD, $path, $controller, $action); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param object|class-string $controller |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
public static function connect( |
35
|
|
|
string $path, |
36
|
|
|
object|string $controller, |
37
|
|
|
string $action = '__invoke', |
38
|
|
|
): void { |
39
|
|
|
self::runRoute(Request::METHOD_CONNECT, $path, $controller, $action); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param object|class-string $controller |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public static function delete( |
46
|
|
|
string $path, |
47
|
|
|
object|string $controller, |
48
|
|
|
string $action = '__invoke', |
49
|
|
|
): void { |
50
|
|
|
self::runRoute(Request::METHOD_DELETE, $path, $controller, $action); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param object|class-string $controller |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public static function options( |
57
|
|
|
string $path, |
58
|
|
|
object|string $controller, |
59
|
|
|
string $action = '__invoke', |
60
|
|
|
): void { |
61
|
|
|
self::runRoute(Request::METHOD_OPTIONS, $path, $controller, $action); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param object|class-string $controller |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public static function patch( |
68
|
|
|
string $path, |
69
|
|
|
object|string $controller, |
70
|
|
|
string $action = '__invoke', |
71
|
|
|
): void { |
72
|
|
|
self::runRoute(Request::METHOD_PATCH, $path, $controller, $action); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param object|class-string $controller |
|
|
|
|
77
|
|
|
*/ |
78
|
1 |
|
public static function post( |
79
|
|
|
string $path, |
80
|
|
|
object|string $controller, |
81
|
|
|
string $action = '__invoke', |
82
|
|
|
): void { |
83
|
1 |
|
self::runRoute(Request::METHOD_POST, $path, $controller, $action); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param object|class-string $controller |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public static function put( |
90
|
|
|
string $path, |
91
|
|
|
object|string $controller, |
92
|
|
|
string $action = '__invoke', |
93
|
|
|
): void { |
94
|
|
|
self::runRoute(Request::METHOD_PUT, $path, $controller, $action); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param object|class-string $controller |
|
|
|
|
99
|
|
|
*/ |
100
|
|
|
public static function trace( |
101
|
|
|
string $path, |
102
|
|
|
object|string $controller, |
103
|
|
|
string $action = '__invoke', |
104
|
|
|
): void { |
105
|
|
|
self::runRoute(Request::METHOD_TRACE, $path, $controller, $action); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param object|class-string $controller |
|
|
|
|
110
|
|
|
*/ |
111
|
40 |
|
private static function runRoute( |
112
|
|
|
string $method, |
113
|
|
|
string $path, |
114
|
|
|
object|string $controller, |
115
|
|
|
string $action = '__invoke', |
116
|
|
|
): void { |
117
|
40 |
|
$path = ($path === '/') ? '' : $path; |
118
|
|
|
|
119
|
40 |
|
$route = new RouteEntity($method, $path, $controller, $action); |
120
|
|
|
|
121
|
40 |
|
if ($route->requestMatches()) { |
122
|
38 |
|
echo $route->run(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|