|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Selami Router |
|
4
|
|
|
* PHP version 7+ |
|
5
|
|
|
* |
|
6
|
|
|
* @license https://github.com/selamiphp/router/blob/master/LICENSE (MIT License) |
|
7
|
|
|
* @link https://github.com/selamiphp/router |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Selami; |
|
13
|
|
|
|
|
14
|
|
|
use FastRoute; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Router |
|
19
|
|
|
* |
|
20
|
|
|
* This class is responsible for registering route objects, |
|
21
|
|
|
* determining aliases if available and finding requested route |
|
22
|
|
|
*/ |
|
23
|
|
|
final class Router |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* routes array to be registered. |
|
27
|
|
|
* Some routes may have aliases to be used in templating system |
|
28
|
|
|
* Route item can be defined using array key as an alias key. |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $routes = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* aliases array to be registered. |
|
35
|
|
|
* Each route item is an array has items respectively : Request Method, Request Uri, Controller/Action, Return Type. |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $aliases = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* HTTP request Method |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $method; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Request Uri |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $requestedPath; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Default return type if not noted in the $routes |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $defaultReturnType; |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Translation array. |
|
61
|
|
|
* Make sures about return type. |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
private static $translations = [ |
|
65
|
|
|
'h' => 'html', |
|
66
|
|
|
'html' => 'html', |
|
67
|
|
|
'r' => 'redirect', |
|
68
|
|
|
'redirect' => 'redirect', |
|
69
|
|
|
'j' => 'json', |
|
70
|
|
|
'json' => 'json', |
|
71
|
|
|
't' => 'text', |
|
72
|
|
|
'text' => 'text', |
|
73
|
|
|
'd' => 'download', |
|
74
|
|
|
'download' => 'download' |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Valid Request Methods array. |
|
79
|
8 |
|
* Make sures about requested methods. |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
private static $validRequestMethods = [ |
|
83
|
|
|
'GET', |
|
84
|
|
|
'OPTIONS', |
|
85
|
|
|
'HEAD', |
|
86
|
|
|
'POST', |
|
87
|
8 |
|
'PUT', |
|
88
|
8 |
|
'DELETE', |
|
89
|
8 |
|
'PATCH' |
|
90
|
8 |
|
]; |
|
91
|
8 |
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Valid Request Methods array. |
|
95
|
|
|
* Make sures about return type. |
|
96
|
|
|
* @var array |
|
97
|
|
|
*/ |
|
98
|
|
|
private static $validReturnTypes = [ |
|
99
|
8 |
|
'html', |
|
100
|
|
|
'json', |
|
101
|
8 |
|
'text', |
|
102
|
1 |
|
'redirect', |
|
103
|
|
|
'download' |
|
104
|
8 |
|
]; |
|
105
|
1 |
|
|
|
106
|
|
|
/** |
|
107
|
8 |
|
* Router constructor. |
|
108
|
|
|
* Create new router. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $routes |
|
|
|
|
|
|
111
|
|
|
* @param string $defaultReturnType |
|
112
|
|
|
* @param string $method |
|
113
|
|
|
* @param string $requestedPath |
|
114
|
3 |
|
* @param string $folder |
|
115
|
|
|
* @throws InvalidArgumentException |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public function __construct( |
|
118
|
|
|
string $defaultReturnType, |
|
119
|
|
|
string $method, |
|
120
|
|
|
string $requestedPath, |
|
121
|
|
|
string $folder = '' |
|
122
|
|
|
) { |
|
123
|
3 |
|
if (!in_array($method, self::$validRequestMethods, true)) { |
|
124
|
3 |
|
$message = sprintf('%s is nat valid Http request method.', $method); |
|
125
|
|
|
throw new InvalidArgumentException($message); |
|
126
|
3 |
|
} |
|
127
|
3 |
|
$this->method = $method; |
|
128
|
|
|
$this->requestedPath = $this->extractFolder($requestedPath, $folder); |
|
129
|
|
|
$this->defaultReturnType = self::$translations[$defaultReturnType] ?? self::$defaultReturnType[0]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Remove sub folder from requestedPath if defined |
|
134
|
3 |
|
* @param string $requestPath |
|
135
|
|
|
* @param string $folder |
|
136
|
3 |
|
* @return string |
|
137
|
3 |
|
*/ |
|
138
|
3 |
|
private function extractFolder(string $requestPath, string $folder) |
|
139
|
1 |
|
{ |
|
140
|
1 |
|
if (!empty($folder)) { |
|
141
|
1 |
|
$requestPath = '/' . trim(preg_replace('#^/' . $folder . '#msi', '/', $requestPath), '/'); |
|
142
|
3 |
|
} |
|
143
|
|
|
if ($requestPath === '') { |
|
144
|
3 |
|
$requestPath = '/'; |
|
145
|
|
|
} |
|
146
|
|
|
return $requestPath; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
4 |
|
* add route to routes list |
|
151
|
|
|
* @param string|array requestMethods |
|
152
|
4 |
|
* @param string $route |
|
153
|
4 |
|
* @param string $action |
|
154
|
4 |
|
* @param string $returnType |
|
155
|
4 |
|
* @param string $alias |
|
156
|
|
|
* @return string |
|
157
|
|
|
* @throws InvalidArgumentException |
|
158
|
4 |
|
*/ |
|
159
|
|
|
public function add($requestMethods, string $route, string $action, string $returnType=null, string $alias=null) |
|
160
|
|
|
{ |
|
161
|
|
|
$requestMethodParameterType = gettype($requestMethods); |
|
162
|
|
|
if (!in_array($requestMethodParameterType, ['array', 'string'], true)) { |
|
163
|
|
|
$message = sprintf( |
|
164
|
3 |
|
'Request method must be either string or array but %s given.', |
|
165
|
|
|
$requestMethodParameterType); |
|
166
|
3 |
|
throw new InvalidArgumentException($message); |
|
167
|
3 |
|
} |
|
168
|
|
|
$requestMethodsGiven = is_array($requestMethods) ? (array) $requestMethods : [0 => $requestMethods]; |
|
169
|
3 |
|
$returnType = $returnType === null ? $this->defaultReturnType: self::$validReturnTypes[$returnType]?? $this->defaultReturnType; |
|
170
|
3 |
|
foreach ($requestMethodsGiven as $requestMethod) { |
|
171
|
|
|
if ($alias !== null) { |
|
172
|
3 |
|
$this->aliases[$alias] = $route; |
|
173
|
|
|
} |
|
174
|
|
|
$this->routes[] = [strtoupper($requestMethod), $route, $action, $returnType]; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Dispatch against the provided HTTP method verb and URI. |
|
180
|
3 |
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
3 |
|
private function dispatcher() |
|
183
|
|
|
{ |
|
184
|
3 |
|
$options = [ |
|
185
|
|
|
'routeParser' => 'FastRoute\\RouteParser\\Std', |
|
186
|
3 |
|
'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', |
|
187
|
3 |
|
'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', |
|
188
|
|
|
'routeCollector' => 'FastRoute\\RouteCollector', |
|
189
|
|
|
]; |
|
190
|
3 |
|
/** @var RouteCollector $routeCollector */ |
|
191
|
|
|
$routeCollector = new $options['routeCollector']( |
|
192
|
|
|
new $options['routeParser'], new $options['dataGenerator'] |
|
193
|
|
|
); |
|
194
|
3 |
|
$this->addRoutes($routeCollector); |
|
195
|
|
|
return new $options['dispatcher']($routeCollector->getData()); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Define Closures for all routes that returns controller info to be used. |
|
200
|
|
|
* @param FastRoute\RouteCollector $route |
|
201
|
|
|
*/ |
|
202
|
3 |
|
private function addRoutes(FastRoute\RouteCollector $route) |
|
203
|
|
|
{ |
|
204
|
3 |
|
foreach ($this->routes as $definedRoute) { |
|
205
|
1 |
|
$definedRoute[3] = $definedRoute[3] ?? $this->defaultReturnType; |
|
206
|
1 |
|
$route->addRoute(strtoupper($definedRoute[0]), $definedRoute[1], function ($args) use ($definedRoute) { |
|
207
|
|
|
list(,,$controller, $returnType) = $definedRoute; |
|
208
|
|
|
$returnType = Router::$translations[$returnType] ?? $this->defaultReturnType; |
|
209
|
2 |
|
return ['controller' => $controller, 'returnType'=> $returnType, 'args'=> $args]; |
|
210
|
|
|
}); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get router data that includes route info and aliases |
|
218
|
|
|
*/ |
|
219
|
|
|
public function getRoute() |
|
220
|
|
|
{ |
|
221
|
|
|
$dispatcher = $this->dispatcher(); |
|
222
|
|
|
$routeInfo = $dispatcher->dispatch($this->method, $this->requestedPath); |
|
223
|
|
|
$routerData = [ |
|
224
|
|
|
'route' => $this->runDispatcher($routeInfo), |
|
225
|
|
|
'aliases' => $this->aliases |
|
226
|
|
|
]; |
|
227
|
|
|
return $routerData; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get route info for requested uri |
|
233
|
|
|
* @param array $routeInfo |
|
234
|
|
|
* @return array $routerData |
|
235
|
|
|
*/ |
|
236
|
|
|
private function runDispatcher(array $routeInfo) |
|
237
|
|
|
{ |
|
238
|
|
|
$routeData = $this->getRouteData($routeInfo); |
|
239
|
|
|
$dispatchResults = [ |
|
240
|
|
|
FastRoute\Dispatcher::METHOD_NOT_ALLOWED => [ |
|
241
|
|
|
'status' => 405 |
|
242
|
|
|
], |
|
243
|
|
|
FastRoute\Dispatcher::FOUND => [ |
|
244
|
|
|
'status' => 200 |
|
245
|
|
|
], |
|
246
|
|
|
FastRoute\Dispatcher::NOT_FOUND => [ |
|
247
|
|
|
'status' => 404 |
|
248
|
|
|
] |
|
249
|
|
|
]; |
|
250
|
|
|
return array_merge($routeData, $dispatchResults[$routeInfo[0]]); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Get routeData according to dispatcher's results |
|
255
|
|
|
* @param array $routeInfo |
|
256
|
|
|
* @return array |
|
257
|
|
|
*/ |
|
258
|
|
|
private function getRouteData(array $routeInfo) |
|
259
|
|
|
{ |
|
260
|
|
|
if ($routeInfo[0] === FastRoute\Dispatcher::FOUND) { |
|
261
|
|
|
list(, $handler, $vars) = $routeInfo; |
|
262
|
|
|
return $handler($vars); |
|
263
|
|
|
} |
|
264
|
|
|
return [ |
|
265
|
|
|
'status' => 200, |
|
266
|
|
|
'returnType' => 'html', |
|
267
|
|
|
'definedRoute' => null, |
|
268
|
|
|
'args' => [] |
|
269
|
|
|
]; |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.