-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRouter.php
649 lines (554 loc) · 20.5 KB
/
Router.php
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
<?php
/**
* @author Marwan Al-Soltany <[email protected]>
* @copyright Marwan Al-Soltany 2021
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MAKS\Velox\Backend;
use MAKS\Velox\App;
use MAKS\Velox\Backend\Exception;
use MAKS\Velox\Backend\Event;
use MAKS\Velox\Backend\Config;
use MAKS\Velox\Backend\Globals;
/**
* A class that serves as a router and an entry point for the application.
*
* Example:
* ```
* // register a middleware
* Router::middleware('/pages/{pageId}', function ($path, $match, $previous) {
* return 'I am working as expected!';
* }, 'POST');
*
* // register a route handler
* Router::handle('/pages/{pageId}', function ($path, $match, $previous) {
* return sprintf('Hi from "%s" handler, Page ID is: %s, also the middleware said: %s', $path, $match, $previous ?? 'Nothing!');
* }, ['GET', 'POST']);
*
* // register a route handler using an HTTP verb
* Router::get('/another-page', function () {
* return View::render('another-page');
* });
*
* // start the application
* Router::start();
* ```
*
* @package Velox\Backend
* @since 1.0.0
* @api
*
* @method static self get(string $expression, callable $handler) Handles a `GET` request method.
* @method static self head(string $expression, callable $handler) Handles a `HEAD` request method.
* @method static self post(string $expression, callable $handler) Handles a `POST` request method.
* @method static self put(string $expression, callable $handler) Handles a `PUT` request method.
* @method static self patch(string $expression, callable $handler) Handles a `PATCH` request method.
* @method static self delete(string $expression, callable $handler) Handles a `DELETE` request method.
* @method static self connect(string $expression, callable $handler) Handles a `CONNECT` request method.
* @method static self options(string $expression, callable $handler) Handles a `OPTIONS` request method.
* @method static self trace(string $expression, callable $handler) Handles a `TRACE` request method.
* @method static self any(string $expression, callable $handler) Handles any request method.
*/
class Router
{
/**
* This event will be dispatched when a handler is registered.
* This event will be passed a reference to the route config array.
*
* @var string
*/
public const ON_REGISTER_HANDLER = 'router.on.registerHandler';
/**
* This event will be dispatched when a middleware is registered.
* This event will be passed a reference to the route config array.
*
* @var string
*/
public const ON_REGISTER_MIDDLEWARE = 'router.on.registerMiddleware';
/**
* This event will be dispatched when the router is started.
* This event will be passed a reference to the router parameters.
*
* @var string
*/
public const ON_START = 'router.on.start';
/**
* This event will be dispatched when a redirect is attempted.
* This event will be passed the redirection path/URL and the status code.
*
* @var string
*/
public const BEFORE_REDIRECT = 'router.before.redirect';
/**
* This event will be dispatched when a forward is attempted.
* This event will be passed the forward path.
*
* @var string
*/
public const BEFORE_FORWARD = 'router.before.forward';
/**
* The default values of class parameters.
*
* @var array
*/
public const DEFAULTS = [
'base' => '/',
'allowMultiMatch' => true,
'caseMatters' => false,
'slashMatters' => true,
'allowAutoStart' => true,
];
/**
* Supported HTTP methods.
*
* @var array
*/
public const SUPPORTED_METHODS = [
'GET',
'HEAD',
'POST',
'PUT',
'PATCH',
'DELETE',
'CONNECT',
'OPTIONS',
'TRACE'
];
/**
* Route type handler.
*
* @var string
*
* @since 1.5.2
*/
protected const HANDLER_ROUTE = 'HANDLER';
/**
* Route type handler.
*
* @var string
*
* @since 1.5.2
*/
protected const MIDDLEWARE_ROUTE = 'MIDDLEWARE';
/**
* The parameters the application started with.
*/
private static ?array $params = null;
/**
* The current base URL of the application.
*/
protected static ?string $base = null;
/**
* The currently requested path.
*/
protected static ?string $path = null;
/**
* The currently registered routes.
*/
protected static array $routes = [];
/**
* Registers a route.
*
* @param string $type
* @param string $expression
* @param callable $handler
* @param array $arguments
* @param string|array $method
*
* @return static
*/
private static function registerRoute(string $type, string $expression, callable $handler, array $arguments, $method)
{
$route = [
'type' => $type,
'expression' => $expression,
'handler' => $handler,
'arguments' => $arguments,
'method' => $method
];
static::$routes[] = &$route;
Event::dispatch('router.on.register' . ucfirst(strtolower($type)), [&$route]);
return new static();
}
/**
* Registers a handler for a route.
*
* @param string $expression A route like `/page`, `/page/{id}` (`id` is required), or `/page/{id?}` (`id` is optional), or `page*` (`*` is a wildcard for anything).
* For more flexibility, pass an expression like `/page/([\d]+|[0-9]*)` (regex capture group).
* @param callable $handler A function to call if route has matched.
* It will be passed the current `$path`, the `$match` or `...$match` from the expression if there was any, and lastly the `$previous` result
* (the return of the last middleware or route with a matching expression) if `$allowMultiMatch` is set to `true`.
* @param string|string[] $method [optional] Either a string or an array of the allowed method.
*
* @return static
*/
public static function handle(string $expression, callable $handler, $method = 'GET')
{
return static::registerRoute(static::HANDLER_ROUTE, $expression, $handler, [], $method);
}
/**
* Registers a middleware for a route. This method has no effect if `$allowMultiMatch` is set to `false`.
*
* @param string $expression A route like `/page`, `/page/{id}` (`id` is required), or `/page/{id?}` (`id` is optional), or `page*` (`*` is a wildcard for anything).
* For more flexibility, pass an expression like `/page/([\d]+|[0-9]*)` (regex capture group).
* @param callable $handler A function to call if route has matched.
* It will be passed the current `$path`, the `$match` or `...$match` from the expression if there was any, and lastly the `$previous` result
* (the return of the last middleware or route with a matching expression) if `$allowMultiMatch` is set to `true`.
* @param string|string[] $method [optional] Either a string or an array of the allowed method.
*
* @return static
*/
public static function middleware(string $expression, callable $handler, $method = 'GET')
{
return static::registerRoute(static::MIDDLEWARE_ROUTE, $expression, $handler, [], $method);
}
/**
* Redirects the request to another route.
* Note that this function will exit the script (code that comes after it will not be executed).
*
* @param string $to A route like `/page` or a URL like `http://domain.tld`.
* @param int $status [optional] The HTTP status code to send.
*
* @return void
*/
public static function redirect(string $to, int $status = 302): void
{
Event::dispatch(self::BEFORE_REDIRECT, [$to, $status]);
if (filter_var($to, FILTER_VALIDATE_URL)) {
$header = sprintf('Location: %s', $to);
} else {
$scheme = Globals::getServer('HTTPS') == 'on' ? 'https' : 'http';
$host = Globals::getServer('HTTP_HOST');
$path = preg_replace('/(\/+)/', '/', static::$base . '/' . $to);
$base = Config::get('global.baseUrl', $scheme . '://' . $host);
$header = sprintf('Location: %s/%s', trim($base, '/'), trim($path, '/'));
}
header($header, false, $status);
App::terminate(); // @codeCoverageIgnore
}
/**
* Forwards the request to another route.
* Note that this function will exit the script (code that comes after it will not be executed).
*
* @param string $to A route like `/page`.
*
* @return void
*/
public static function forward(string $to): void
{
Event::dispatch(self::BEFORE_FORWARD, [$to]);
$base = static::$base ?? '';
$path = trim($base, '/') . '/' . ltrim($to, '/');
Globals::setServer('REQUEST_URI', $path);
static::start(...self::$params);
App::terminate(); // @codeCoverageIgnore
}
/**
* Starts the router.
*
* @param string|null [optional] $base App base path, this will prefix all routes.
* @param bool|null [optional] $allowMultiMatch Whether the router should execute handlers of all matches. Useful to make middleware-like functionality, the first match will act as a middleware.
* @param bool|null [optional] $caseMatters Whether the route matching should be case sensitive or not.
* @param bool|null [optional] $slashMatters Whether trailing slash should be taken in consideration with route matching or not.
*
* @return void
*
* @throws \LogicException If route handler failed or returned false.
*/
public static function start(?string $base = null, ?bool $allowMultiMatch = null, ?bool $caseMatters = null, ?bool $slashMatters = null): void
{
self::$params = func_get_args();
Event::dispatch(self::ON_START, [&self::$params]);
Session::csrf()->check();
[$base, $allowMultiMatch, $caseMatters, $slashMatters] = static::getValidParameters($base, $allowMultiMatch, $caseMatters, $slashMatters);
static::$base = $base = '/' . trim($base, '/');
static::$path = $path = static::getRoutePath($slashMatters);
$routeMatchFound = false;
$pathMatchFound = false;
$result = null;
self::sort();
foreach (static::$routes as &$route) {
$expression = $base === '/' ? $route['expression'] : sprintf('%s/%s', $base, ltrim($route['expression'], '/'));
$regex = static::getRouteRegex($expression, $slashMatters, $caseMatters);
if (preg_match($regex, $path, $matches, PREG_UNMATCHED_AS_NULL)) {
$pathMatchFound = true;
$currentMethod = static::getRequestMethod();
$allowedMethods = (array)$route['method'];
foreach ($allowedMethods as $allowedMethod) {
if (strtoupper($currentMethod) !== strtoupper($allowedMethod)) {
continue;
}
$routeMatchFound = true;
$route['arguments'] = static::getRouteArguments($route['arguments'], $matches, $result);
$result = call_user_func_array($route['handler'], $route['arguments']);
if ($result === false) {
Exception::throw(
'InvalidResponseException:LogicException',
"Something went wrong when trying to respond to '{$path}'! " .
"Check the handler for this route, was expecting 'string' as a response but got 'false' instead"
);
}
}
}
if ($routeMatchFound && !$allowMultiMatch) {
break;
}
}
unset($route);
static::respond($result, $routeMatchFound, $pathMatchFound);
}
/**
* Sorts registered routes to make middlewares come before handlers.
*
* @return void
*/
private static function sort(): void
{
usort(static::$routes, function ($routeA, $routeB) {
if ($routeA['type'] === static::MIDDLEWARE_ROUTE && $routeB['type'] !== static::MIDDLEWARE_ROUTE) {
return -1;
}
if ($routeA['type'] !== static::MIDDLEWARE_ROUTE && $routeB['type'] === static::MIDDLEWARE_ROUTE) {
return 1;
}
return 0;
});
}
/**
* Echos the response according to the passed parameters.
*
* @param mixed $result
* @param bool $routeMatchFound
* @param bool $pathMatchFound
*
* @return void
*/
protected static function respond($result, bool $routeMatchFound, bool $pathMatchFound): void
{
$code = 200;
if (!$routeMatchFound) {
$code = $pathMatchFound ? 405 : 404;
$path = static::$path;
$method = static::getRequestMethod();
$responses = [
404 => [
'title' => sprintf('%d Not Found', $code),
'message' => sprintf('The "%s" route is not found!', $path),
],
405 => [
'title' => sprintf('%d Not Allowed', $code),
'message' => sprintf('The "%s" route is found, but the request method "%s" is not allowed!', $path, $method),
],
];
App::log("Responded with {$code} to the request for '{$path}' with method '{$method}'", null, 'system');
// this function will exit the script
App::abort(
$code,
$responses[$code]['title'],
$responses[$code]['message']
);
}
http_response_code() || http_response_code($code);
echo $result;
}
/**
* Returns valid parameters for `self::start()` by validating the passed parameters and adding the deficiency from router config.
*
* @param string|null $base
* @param bool|null $allowMultiMatch
* @param bool|null $caseMatters
* @param bool|null $slashMatters
*
* @return array
*/
protected static function getValidParameters(?string $base, ?bool $allowMultiMatch, ?bool $caseMatters, ?bool $slashMatters): array
{
$routerConfig = Config::get('router');
$base ??= $routerConfig['base'];
$allowMultiMatch ??= $routerConfig['allowMultiMatch'];
$caseMatters ??= $routerConfig['caseMatters'];
$slashMatters ??= $routerConfig['slashMatters'];
return [
$base,
$allowMultiMatch,
$caseMatters,
$slashMatters,
];
}
/**
* Returns a valid decoded route path.
*
* @param string $base
*
* @return string
*/
protected static function getRoutePath(bool $slashMatters): string
{
$url = static::getParsedUrl();
$path = $url['path'] ?? '/';
$path = !$slashMatters && $path !== '/' ? rtrim($path, '/') : $path;
return urldecode($path);
}
/**
* Returns a valid route regex.
*
* @param string $expression
* @param bool $slashMatters
* @param bool $caseMatters
*
* @return string
*/
protected static function getRouteRegex(string $expression, bool $slashMatters, bool $caseMatters): string
{
$asteriskRegex = '/(?<!\()\*(?!\))/';
$placeholderRegex = '/{([a-zA-Z0-9_\-\.?]+)}/';
// replace asterisk only if it's not a part of a regex capturing group
$expression = preg_replace($asteriskRegex, '.*?', $expression);
// replace placeholders with their corresponding regex
if (preg_match_all($placeholderRegex, $expression, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$placeholder = $match[0];
$replacement = strpos($placeholder, '?') !== false ? '(.*)?' : '(.+)';
$expression = strtr($expression, [
$placeholder => $replacement
]);
}
}
return sprintf(
'<^%s$>%s',
(!$slashMatters && $expression !== '/' ? rtrim($expression, '/') : $expression),
(!$caseMatters ? 'iu' : 'u')
);
}
/**
* Returns valid arguments for route handler in the order that the handler expect.
*
* @param array $current
* @param array $matches
* @param mixed $result
*
* @return array
*/
protected static function getRouteArguments(array $current, array $matches, $result): array
{
$arguments = array_merge($current, $matches);
$arguments = array_filter($arguments);
if (count($arguments) > 1) {
array_push($arguments, $result);
} else {
array_push($arguments, null, $result);
}
return $arguments;
}
/**
* Returns the current request method via `$_SERVER` or `$_POST['_method']`.
*
* @return string
*/
protected static function getRequestMethod(): string
{
$method = Globals::cutPost('_method') ?? '';
$methods = static::SUPPORTED_METHODS;
$methodAllowed = in_array(
strtoupper($method),
array_map('strtoupper', $methods)
);
if ($methodAllowed) {
Globals::setServer('REQUEST_METHOD', $method);
}
return Globals::getServer('REQUEST_METHOD');
}
/**
* Returns query parameters.
*
* @return array
*/
public static function getParsedQuery(): array
{
$url = static::getParsedUrl();
parse_str($url['query'] ?? '', $query);
return $query;
}
/**
* Returns components of the current URL.
*
* @return array
*/
public static function getParsedUrl(): array
{
$uri = Globals::getServer('REQUEST_URI');
// remove double slashes as they make parse_url() behave unexpectedly
$url = preg_replace('/(\/+)/', '/', $uri);
$url = parse_url($url);
return $url;
}
/**
* Returns all registered routes with their `expression`, `handler`, `arguments`, and `method`.
*
* @return array
*/
public static function getRegisteredRoutes(): array
{
return static::$routes;
}
/**
* Class constructor.
*/
final public function __construct()
{
// prevent overwriting constructor in subclasses to allow to use
// "return new static()" without caring about dependencies.
static $isListening = false;
// start the router if it's not started explicitly
// @codeCoverageIgnoreStart
if (Config::get('router.allowAutoStart') && !$isListening) {
Event::listen(App::ON_SHUTDOWN, static function () {
// $params should be an array if the router has been started
if (self::$params === null && PHP_SAPI !== 'cli') {
try {
static::start();
} catch (\Throwable $exception) {
App::handleException($exception);
}
}
});
$isListening = true;
}
// @codeCoverageIgnoreEnd
}
/**
* Aliases `self::handle()` method with common HTTP verbs.
*/
public static function __callStatic(string $method, array $arguments)
{
$methods = static::SUPPORTED_METHODS;
$methodAllowed = in_array(
strtoupper($method),
array_map('strtoupper', ['ANY', ...$methods])
);
if (!$methodAllowed) {
Exception::throw(
'UndefinedMethodException:BadMethodCallException',
sprintf('Call to undefined method %s::%s()', static::class, $method)
);
}
if (count($arguments) > 2) {
$arguments = array_slice($arguments, 0, 2);
}
if (strtoupper($method) === 'ANY') {
array_push($arguments, $methods);
} else {
array_push($arguments, $method);
}
return static::handle(...$arguments);
}
/**
* Allows static methods handled by `self::__callStatic()` to be accessible via object operator `->`.
*/
public function __call(string $method, array $arguments)
{
return static::__callStatic($method, $arguments);
}
}