1 | <?php declare(strict_types=1); |
||
12 | class Router extends RouteCollector implements |
||
13 | MiddlewareAwareInterface, |
||
14 | RouteCollectionInterface, |
||
15 | StrategyAwareInterface, |
||
16 | RequestHandlerInterface |
||
17 | { |
||
18 | use MiddlewareAwareTrait; |
||
19 | use RouteCollectionTrait; |
||
20 | use StrategyAwareTrait; |
||
21 | |||
22 | /** |
||
23 | * @var Route[] |
||
24 | */ |
||
25 | protected $routes = []; |
||
26 | |||
27 | /** |
||
28 | * @var Route[] |
||
29 | */ |
||
30 | protected $namedRoutes = []; |
||
31 | |||
32 | /** |
||
33 | * @var RouteGroup[] |
||
34 | */ |
||
35 | protected $groups = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $patternMatchers = [ |
||
41 | '/{(.+?):number}/' => '{$1:[0-9]+}', |
||
42 | '/{(.+?):word}/' => '{$1:[a-zA-Z]+}', |
||
43 | '/{(.+?):alphanum_dash}/' => '{$1:[a-zA-Z0-9-_]+}', |
||
44 | '/{(.+?):slug}/' => '{$1:[a-z0-9-]+}', |
||
45 | '/{(.+?):uuid}/' => '{$1:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+}' |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Constructor |
||
50 | * |
||
51 | * @param RouteParser $parser |
||
52 | * @param DataGenerator $generator |
||
53 | */ |
||
54 | 63 | public function __construct(?RouteParser $parser = null, ?DataGenerator $generator = null) |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 45 | public function map(string $method, string $path, $handler): Route |
|
74 | |||
75 | /** |
||
76 | * Add a group of routes to the collection |
||
77 | * |
||
78 | * @param string $prefix |
||
79 | * @param callable $group |
||
80 | * |
||
81 | * @return RouteGroup |
||
82 | */ |
||
83 | 12 | public function group(string $prefix, callable $group): RouteGroup |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 48 | public function dispatch(ServerRequestInterface $request): ResponseInterface |
|
116 | |||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
125 | |||
126 | /** |
||
127 | * Prepare all routes, build name index and filter out none matching |
||
128 | * routes before being passed off to the parser. |
||
129 | * |
||
130 | * @param ServerRequestInterface $request |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | 48 | protected function prepRoutes(ServerRequestInterface $request): void |
|
168 | |||
169 | /** |
||
170 | * Build an index of named routes. |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | 54 | protected function buildNameIndex(): void |
|
183 | |||
184 | /** |
||
185 | * Process all groups |
||
186 | * |
||
187 | * Adds all of the group routes to the collection and determines if the group |
||
188 | * strategy should be be used. |
||
189 | * |
||
190 | * @param ServerRequestInterface $request |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 48 | protected function processGroups(ServerRequestInterface $request): void |
|
211 | |||
212 | /** |
||
213 | * Get a named route |
||
214 | * |
||
215 | * @param string $name |
||
216 | * |
||
217 | * @return Route |
||
218 | * |
||
219 | * @throws InvalidArgumentException when no route of the provided name exists |
||
220 | */ |
||
221 | 6 | public function getNamedRoute(string $name): Route |
|
231 | |||
232 | /** |
||
233 | * Add a convenient pattern matcher to the internal array for use with all routes |
||
234 | * |
||
235 | * @param string $alias |
||
236 | * @param string $regex |
||
237 | * |
||
238 | * @return self |
||
239 | */ |
||
240 | 3 | public function addPatternMatcher(string $alias, string $regex): self |
|
249 | |||
250 | /** |
||
251 | * Replace word patterns with regex in route path |
||
252 | * |
||
253 | * @param string $path |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 30 | protected function parseRoutePath(string $path): string |
|
261 | } |
||
262 |