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 | 63 | * @param DataGenerator $generator |
|
53 | */ |
||
54 | public function __construct(?RouteParser $parser = null, ?DataGenerator $generator = null) |
||
61 | |||
62 | /** |
||
63 | 45 | * {@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 | 12 | * @return RouteGroup |
|
82 | */ |
||
83 | 12 | public function group(string $prefix, callable $group): RouteGroup |
|
90 | |||
91 | /** |
||
92 | 48 | * {@inheritdoc} |
|
93 | * @deprecated use handle() method |
||
94 | 48 | */ |
|
95 | 30 | public function dispatch(ServerRequestInterface $request): ResponseInterface |
|
99 | |||
100 | |||
101 | 48 | /** |
|
102 | * @param ServerRequestInterface $request |
||
103 | 48 | * @return ResponseInterface |
|
104 | 3 | */ |
|
105 | 3 | public function handle(ServerRequestInterface $request): ResponseInterface |
|
127 | |||
128 | 48 | /** |
|
129 | * Prepare all routes, build name index and filter out none matching |
||
130 | * routes before being passed off to the parser. |
||
131 | 48 | * |
|
132 | * @param ServerRequestInterface $request |
||
133 | 39 | * |
|
134 | 39 | * @return void |
|
135 | 3 | */ |
|
136 | protected function prepRoutes(ServerRequestInterface $request): void |
||
170 | |||
171 | 54 | /** |
|
172 | * Build an index of named routes. |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | protected function buildNameIndex(): void |
||
185 | 48 | ||
186 | /** |
||
187 | 48 | * Process all groups |
|
188 | * |
||
189 | * Adds all of the group routes to the collection and determines if the group |
||
190 | 9 | * strategy should be be used. |
|
191 | 9 | * |
|
192 | * @param ServerRequestInterface $request |
||
193 | 6 | * |
|
194 | * @return void |
||
195 | */ |
||
196 | 9 | protected function processGroups(ServerRequestInterface $request): void |
|
213 | |||
214 | 6 | /** |
|
215 | 3 | * Get a named route |
|
216 | * |
||
217 | * @param string $name |
||
218 | 3 | * |
|
219 | * @return Route |
||
220 | * |
||
221 | * @throws InvalidArgumentException when no route of the provided name exists |
||
222 | */ |
||
223 | public function getNamedRoute(string $name): Route |
||
233 | |||
234 | 3 | /** |
|
235 | * Add a convenient pattern matcher to the internal array for use with all routes |
||
236 | 3 | * |
|
237 | * @param string $alias |
||
238 | * @param string $regex |
||
239 | * |
||
240 | * @return self |
||
241 | */ |
||
242 | public function addPatternMatcher(string $alias, string $regex): self |
||
251 | |||
252 | /** |
||
253 | * Replace word patterns with regex in route path |
||
254 | * |
||
255 | * @param string $path |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function parseRoutePath(string $path): string |
||
263 | } |
||
264 |