1 | <?php declare(strict_types=1); |
||
11 | class Router extends RouteCollector implements |
||
12 | MiddlewareAwareInterface, |
||
13 | RouteCollectionInterface, |
||
14 | StrategyAwareInterface |
||
15 | { |
||
16 | use MiddlewareAwareTrait; |
||
17 | use RouteCollectionTrait; |
||
18 | use StrategyAwareTrait; |
||
19 | |||
20 | /** |
||
21 | * @var \League\Route\Route[] |
||
22 | */ |
||
23 | protected $routes = []; |
||
24 | |||
25 | /** |
||
26 | * @var \League\Route\Route[] |
||
27 | */ |
||
28 | protected $namedRoutes = []; |
||
29 | |||
30 | /** |
||
31 | * @var \League\Route\RouteGroup[] |
||
32 | */ |
||
33 | protected $groups = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $patternMatchers = [ |
||
39 | '/{(.+?):number}/' => '{$1:[0-9]+}', |
||
40 | '/{(.+?):word}/' => '{$1:[a-zA-Z]+}', |
||
41 | '/{(.+?):alphanum_dash}/' => '{$1:[a-zA-Z0-9-_]+}', |
||
42 | '/{(.+?):slug}/' => '{$1:[a-z0-9-]+}', |
||
43 | '/{(.+?):uuid}/' => '{$1:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+}' |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | * |
||
49 | * @param \FastRoute\RouteParser $parser |
||
50 | * @param \FastRoute\DataGenerator $generator |
||
51 | */ |
||
52 | 40 | public function __construct(?RouteParser $parser = null, ?DataGenerator $generator = null) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 30 | public function map(string $method, string $path, $handler) : Route |
|
72 | |||
73 | /** |
||
74 | * Add a group of routes to the collection |
||
75 | * |
||
76 | * @param string $prefix |
||
77 | * @param callable $group |
||
78 | * |
||
79 | * @return \League\Route\RouteGroup |
||
80 | */ |
||
81 | 8 | public function group(string $prefix, callable $group) : RouteGroup |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 30 | public function dispatch(ServerRequestInterface $request) : ResponseInterface |
|
106 | |||
107 | /** |
||
108 | * Prepare all routes, build name index and filter out none matching |
||
109 | * routes before being passed off to the parser. |
||
110 | * |
||
111 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | 30 | protected function prepRoutes(ServerRequestInterface $request) : void |
|
116 | { |
||
117 | 30 | $this->processGroups($request); |
|
118 | 30 | $this->buildNameIndex(); |
|
119 | |||
120 | 30 | $routes = array_merge(array_values($this->routes), array_values($this->namedRoutes)); |
|
121 | |||
122 | 30 | foreach ($routes as $key => $route) { |
|
123 | // check for scheme condition |
||
124 | 26 | if (! is_null($route->getScheme()) && $route->getScheme() !== $request->getUri()->getScheme()) { |
|
125 | 2 | continue; |
|
126 | } |
||
127 | |||
128 | // check for domain condition |
||
129 | 24 | if (! is_null($route->getHost()) && $route->getHost() !== $request->getUri()->getHost()) { |
|
130 | 2 | continue; |
|
131 | } |
||
132 | |||
133 | // check for port condition |
||
134 | 22 | if (! is_null($route->getPort()) && $route->getPort() !== $request->getUri()->getPort()) { |
|
135 | 2 | continue; |
|
136 | } |
||
137 | |||
138 | 20 | if (is_null($route->getStrategy())) { |
|
139 | 14 | $route->setStrategy($this->getStrategy()); |
|
140 | } |
||
141 | |||
142 | 20 | $this->addRoute($route->getMethod(), $this->parseRoutePath($route->getPath()), $route); |
|
143 | } |
||
144 | 30 | } |
|
145 | |||
146 | /** |
||
147 | * Build an index of named routes. |
||
148 | * |
||
149 | * @return void |
||
150 | */ |
||
151 | 34 | protected function buildNameIndex() : void |
|
160 | |||
161 | /** |
||
162 | * Process all groups |
||
163 | * |
||
164 | * Adds all of the group routes to the collection and determines if the group |
||
165 | * strategy should be be used. |
||
166 | * |
||
167 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 30 | protected function processGroups(ServerRequestInterface $request) : void |
|
188 | |||
189 | /** |
||
190 | * Get a named route |
||
191 | * |
||
192 | * @param string $name |
||
193 | * |
||
194 | * @throws \InvalidArgumentException when no route of the provided name exists. |
||
195 | * |
||
196 | * @return \League\Route\Route |
||
197 | */ |
||
198 | 4 | public function getNamedRoute(string $name) : Route |
|
208 | |||
209 | /** |
||
210 | * Add a convenient pattern matcher to the internal array for use with all routes |
||
211 | * |
||
212 | * @param string $alias |
||
213 | * @param string $regex |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | 2 | public function addPatternMatcher(string $alias, string $regex) : self |
|
226 | |||
227 | /** |
||
228 | * Replace word patterns with regex in route path |
||
229 | * |
||
230 | * @param string $path |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | 20 | protected function parseRoutePath(string $path) : string |
|
238 | } |
||
239 |