|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing; |
|
19
|
|
|
|
|
20
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
|
21
|
|
|
use Laminas\Stratigility\{MiddlewarePipe, MiddlewarePipeInterface}; |
|
22
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
|
23
|
|
|
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Aggregate routes for matching and Dispatching. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Router extends RouteMatcher implements \IteratorAggregate, RequestMethodInterface, MiddlewareInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Standard HTTP methods for browser requests. |
|
34
|
|
|
*/ |
|
35
|
|
|
public const HTTP_METHODS_STANDARD = [ |
|
36
|
|
|
self::METHOD_HEAD, |
|
37
|
|
|
self::METHOD_GET, |
|
38
|
|
|
self::METHOD_POST, |
|
39
|
|
|
self::METHOD_PUT, |
|
40
|
|
|
self::METHOD_PATCH, |
|
41
|
|
|
self::METHOD_DELETE, |
|
42
|
|
|
self::METHOD_PURGE, |
|
43
|
|
|
self::METHOD_OPTIONS, |
|
44
|
|
|
self::METHOD_TRACE, |
|
45
|
|
|
self::METHOD_CONNECT, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** @var MiddlewarePipeInterface */ |
|
49
|
|
|
private $pipeline; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(RouteCollection $collection, ?MiddlewarePipeInterface $dispatcher = null) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct($collection); |
|
54
|
|
|
|
|
55
|
|
|
// Add Middleware support. |
|
56
|
|
|
$this->pipeline = $dispatcher ?? new MiddlewarePipe(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
73 |
|
/** |
|
60
|
|
|
* Attach middleware to the pipeline. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function pipe(MiddlewareInterface ...$middlewares): void |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($middlewares as $middleware) { |
|
65
|
73 |
|
$this->pipeline->pipe($middleware); |
|
66
|
73 |
|
} |
|
67
|
73 |
|
} |
|
68
|
|
|
|
|
69
|
73 |
|
/** |
|
70
|
73 |
|
* {@inheritdoc} |
|
71
|
73 |
|
* |
|
72
|
73 |
|
* @return \ArrayIterator<int,Route>|\ArrayIterator<int,array> |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getIterator(): \ArrayIterator |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->routes; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
83
|
|
|
{ |
|
84
|
|
|
$route = $this->matchRequest($request); |
|
85
|
|
|
|
|
86
|
|
|
if (null !== $route && !empty($routeMiddlewares = $route->get('middlewares'))) { |
|
87
|
|
|
$this->pipe(...$routeMiddlewares); |
|
88
|
73 |
|
} |
|
89
|
|
|
|
|
90
|
73 |
|
try { |
|
91
|
|
|
return $this->pipeline->process($request->withAttribute(Route::class, $route), $handler); |
|
92
|
|
|
} finally { |
|
93
|
|
|
if (null !== $this->debug) { |
|
94
|
|
|
$this->debug->leave(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|