-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
451 lines (391 loc) · 14.6 KB
/
Router.php
File metadata and controls
451 lines (391 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Routing;
use Opulence\Http\HttpException;
use Opulence\Http\Requests\Request;
use Opulence\Http\Requests\RequestMethods;
use Opulence\Http\Responses\Response;
use Opulence\Routing\Dispatchers\IRouteDispatcher;
use Opulence\Routing\Routes\CompiledRoute;
use Opulence\Routing\Routes\Compilers\ICompiler;
use Opulence\Routing\Routes\Compilers\Parsers\IParser;
use Opulence\Routing\Routes\ParsedRoute;
use Opulence\Routing\Routes\Route;
use Opulence\Routing\Routes\RouteCollection;
/**
* Defines a URL router
*/
class Router
{
/** @var ICompiler The compiler used by this router */
protected $compiler = null;
/** @var IParser The parser used by this router */
protected $parser = null;
/** @var IRouteDispatcher The route dispatcher */
protected $dispatcher = null;
/** @var RouteCollection The list of routes */
protected $routeCollection = null;
/** @var CompiledRoute|null The matched route if there is one, otherwise null */
protected $matchedRoute = null;
/** @var Controller|mixed|null The matched controller if there is one, otherwise null */
protected $matchedController = null;
/** @var array The list of options in the current group stack */
protected $groupOptionsStack = [];
/**
* @param IRouteDispatcher $dispatcher The route dispatcher
* @param ICompiler $compiler The route compiler
* @param IParser $parser The route parser
*/
public function __construct(
IRouteDispatcher $dispatcher,
ICompiler $compiler,
IParser $parser
) {
$this->dispatcher = $dispatcher;
$this->compiler = $compiler;
$this->parser = $parser;
$this->routeCollection = new RouteCollection();
}
/**
* Adds a route to the router
*
* @param Route $route The route to add
* @return ParsedRoute The route with the group settings applied
*/
public function addRoute(Route $route) : ParsedRoute
{
$route = $this->applyGroupSettings($route);
$parsedRoute = $this->parser->parse($route);
$this->routeCollection->add($parsedRoute);
return $parsedRoute;
}
/**
* Adds a route for the any method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute[] The list of generated routes
*/
public function any(string $path, $controller, array $options = []) : array
{
return $this->multiple(RouteCollection::getMethods(), $path, $controller, $options);
}
/**
* Adds a route for the DELETE method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function delete(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::DELETE, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* Adds a route for the GET method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function get(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::GET, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* @return Controller|mixed|null
*/
public function getMatchedController()
{
return $this->matchedController;
}
/**
* @return CompiledRoute|null
*/
public function getMatchedRoute()
{
return $this->matchedRoute;
}
/**
* Gets the reference to the list of routes
*
* @return RouteCollection The route collection
*/
public function &getRouteCollection() : RouteCollection
{
return $this->routeCollection;
}
/**
* Groups similar routes together so that you don't have to repeat route options
*
* @param array $options The list of options common to all routes added in the closure
* It can contain the following keys:
* "path" => The common path to be prepended to all the grouped routes,
* "middleware" => The middleware to be added to all the grouped routes,
* "https" => Whether or not all the grouped routes are HTTPS,
* "vars" => The list of path variable regular expressions all the routes must match
* @param callable $callback A function that adds routes to the router
*/
public function group(array $options, callable $callback)
{
array_push($this->groupOptionsStack, $options);
$callback($this);
array_pop($this->groupOptionsStack);
}
/**
* Adds a route for the HEAD method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function head(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::HEAD, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* Adds a route for multiple methods
*
* @param array $methods The list of methods to match on
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute[] The list of routes generated
*/
public function multiple(array $methods, string $path, $controller, array $options = []) : array
{
$routes = [];
foreach ($methods as $method) {
$route = $this->createRoute($method, $path, $controller, $options);
$parsedRoute = $this->addRoute($route);
$routes[] = $parsedRoute;
}
return $routes;
}
/**
* Adds a route for the OPTIONS method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function options(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::OPTIONS, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* Adds a route for the PATCH method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function patch(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::PATCH, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* Adds a route for the POST method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function post(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::POST, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* Adds a route for the PUT method at the given path
*
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return ParsedRoute The generated route
*/
public function put(string $path, $controller, array $options = []) : ParsedRoute
{
$route = $this->createRoute(RequestMethods::PUT, $path, $controller, $options);
return $this->addRoute($route);
}
/**
* Routes a request
*
* @param Request $request The request to route
* @return Response The response from the controller
* @throws RouteException Thrown if the controller or method could not be called
* @throws HttpException Thrown if there was no matching route
*/
public function route(Request $request) : Response
{
$method = $request->getMethod();
/** @var ParsedRoute $route */
foreach ($this->routeCollection->get($method) as $route) {
$compiledRoute = $this->compiler->compile($route, $request);
if ($compiledRoute->isMatch()) {
$this->matchedRoute = $compiledRoute;
return $this->dispatcher->dispatch($this->matchedRoute, $request, $this->matchedController);
}
}
// If we've gotten here, we've got a missing route
throw new HttpException(404);
}
/**
* @param RouteCollection $routeCollection
*/
public function setRouteCollection(RouteCollection $routeCollection)
{
$this->routeCollection = $routeCollection;
}
/**
* Applies any group settings to a route
*
* @param Route $route The route to apply the settings to
* @return Route The route with the applied settings
*/
private function applyGroupSettings(Route $route) : Route
{
$route->setRawPath($this->getGroupPath() . $route->getRawPath());
$route->setRawHost($this->getGroupHost() . $route->getRawHost());
if (!$route->usesCallable()) {
$route->setControllerName($this->getGroupControllerNamespace() . $route->getControllerName());
}
$route->setSecure($this->groupIsSecure() || $route->isSecure());
// The route's variable regexes take precedence over group regexes
$route->setVarRegexes(array_merge($this->getVarRegexes(), $route->getVarRegexes()));
$groupMiddleware = $this->getGroupMiddleware();
if (count($groupMiddleware) > 0) {
$route->addMiddleware($groupMiddleware, true);
}
return $route;
}
/**
* Creates a route from the input
*
* @param string $method The method whose route this is
* @param string $path The path to match on
* @param string|callable $controller The name of the controller/method or the callback
* @param array $options The list of options for this path
* @return Route The route from the input
*/
private function createRoute(string $method, string $path, $controller, array $options = []) : Route
{
return new Route([$method], $path, $controller, $options);
}
/**
* Gets the controller namespace from the current group stack
*
* @return string The controller namespace
*/
private function getGroupControllerNamespace() : string
{
$controllerNamespace = '';
foreach ($this->groupOptionsStack as $groupOptions) {
if (isset($groupOptions['controllerNamespace'])) {
// Add trailing slashes if they're not there already
if (mb_substr($groupOptions['controllerNamespace'], -1) !== '\\') {
$groupOptions['controllerNamespace'] .= '\\';
}
$controllerNamespace .= $groupOptions['controllerNamespace'];
}
}
return $controllerNamespace;
}
/**
* Gets the host from the current group stack
*
* @return string The host
*/
private function getGroupHost() : string
{
$host = '';
foreach ($this->groupOptionsStack as $groupOptions) {
if (isset($groupOptions['host'])) {
$host = $groupOptions['host'] . $host;
}
}
return $host;
}
/**
* Gets the middleware in the current group stack
*
* @return array The list of middleware of all the groups
*/
private function getGroupMiddleware() : array
{
$middleware = [];
foreach ($this->groupOptionsStack as $groupOptions) {
if (isset($groupOptions['middleware'])) {
if (!is_array($groupOptions['middleware'])) {
$groupOptions['middleware'] = [$groupOptions['middleware']];
}
$middleware = array_merge($middleware, $groupOptions['middleware']);
}
}
return $middleware;
}
/**
* Gets the path of the current group stack
*
* @return string The path of all the groups concatenated together
*/
private function getGroupPath() : string
{
$path = '';
foreach ($this->groupOptionsStack as $groupOptions) {
if (isset($groupOptions['path'])) {
$path .= $groupOptions['path'];
}
}
return $path;
}
/**
* Gets the variable regexes from the current group stack
*
* @return array The The mapping of variable names to regexes
*/
private function getVarRegexes() : array
{
$variableRegexes = [];
foreach ($this->groupOptionsStack as $groupOptions) {
if (isset($groupOptions['vars'])) {
$variableRegexes = array_merge($variableRegexes, $groupOptions['vars']);
}
}
return $variableRegexes;
}
/**
* Gets whether or not the current group stack is secure
* If ANY of the groups were marked as HTTPS, then this will return true even if a sub-group is not marked HTTPS
*
* @return bool True if the group is secure, otherwise false
*/
private function groupIsSecure() : bool
{
foreach ($this->groupOptionsStack as $groupOptions) {
if (isset($groupOptions['https']) && $groupOptions['https']) {
return true;
}
}
return false;
}
}