1 | <?php |
||
21 | class RouteCollection |
||
22 | extends RouteCollector |
||
23 | implements MiddlewareAwareInterface, RouteCollectionInterface, StrategyAwareInterface |
||
24 | { |
||
25 | use MiddlewareAwareTrait; |
||
26 | use RouteCollectionMapTrait; |
||
27 | use StrategyAwareTrait; |
||
28 | |||
29 | /** |
||
30 | * @var \Interop\Container\ContainerInterface |
||
31 | */ |
||
32 | protected $container; |
||
33 | |||
34 | /** |
||
35 | * @var \League\Route\Route[] |
||
36 | */ |
||
37 | protected $routes = []; |
||
38 | |||
39 | /** |
||
40 | * @var \League\Route\Route[] |
||
41 | */ |
||
42 | protected $namedRoutes = []; |
||
43 | |||
44 | /** |
||
45 | * @var \League\Route\RouteGroup[] |
||
46 | */ |
||
47 | protected $groups = []; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $patternMatchers = [ |
||
53 | '/{(.+?):number}/' => '{$1:[0-9]+}', |
||
54 | '/{(.+?):word}/' => '{$1:[a-zA-Z]+}', |
||
55 | '/{(.+?):alphanum_dash}/' => '{$1:[a-zA-Z0-9-_]+}', |
||
56 | '/{(.+?):slug}/' => '{$1:[a-z0-9-]+}', |
||
57 | '/{(.+?):uuid}/' => '{$1:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+}' |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Constructor. |
||
62 | * |
||
63 | * @param \Interop\Container\ContainerInterface $container |
||
64 | * @param \FastRoute\RouteParser $parser |
||
65 | * @param \FastRoute\DataGenerator $generator |
||
66 | */ |
||
67 | 33 | public function __construct( |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 18 | public function map($method, $path, $handler) |
|
93 | |||
94 | /** |
||
95 | * Add a group of routes to the collection. |
||
96 | * |
||
97 | * @param string $prefix |
||
98 | * @param callable $group |
||
99 | * |
||
100 | * @return \League\Route\RouteGroup |
||
101 | */ |
||
102 | 3 | public function group($prefix, callable $group) |
|
111 | |||
112 | /** |
||
113 | * Dispatch the route based on the request. |
||
114 | * |
||
115 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
116 | * @param \Psr\Http\Message\ResponseInterface $response |
||
117 | * |
||
118 | * @return \Psr\Http\Message\ResponseInterface |
||
119 | */ |
||
120 | 15 | public function dispatch(ServerRequestInterface $request, ResponseInterface $response) |
|
126 | |||
127 | /** |
||
128 | * Return a fully configured dispatcher. |
||
129 | * |
||
130 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
131 | * |
||
132 | * @return \League\Route\Dispatcher |
||
133 | */ |
||
134 | 18 | public function getDispatcher(ServerRequestInterface $request) |
|
144 | |||
145 | /** |
||
146 | * Prepare all routes, build name index and filter out none matching |
||
147 | * routes before being passed off to the parser. |
||
148 | * |
||
149 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | 18 | protected function prepRoutes(ServerRequestInterface $request) |
|
183 | |||
184 | /** |
||
185 | * Build an index of named routes. |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | 27 | protected function buildNameIndex() |
|
200 | |||
201 | /** |
||
202 | * Process all groups. |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | 27 | protected function processGroups() |
|
213 | |||
214 | /** |
||
215 | * Get named route. |
||
216 | * |
||
217 | * @param string $name |
||
218 | * |
||
219 | * @return \League\Route\Route |
||
220 | */ |
||
221 | 9 | public function getNamedRoute($name) |
|
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 void |
||
239 | */ |
||
240 | 3 | public function addPatternMatcher($alias, $regex) |
|
247 | |||
248 | /** |
||
249 | * Convenience method to convert pre-defined key words in to regex strings. |
||
250 | * |
||
251 | * @param string $path |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 12 | protected function parseRoutePath($path) |
|
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | 3 | public function before(callable $middleware) |
|
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | 3 | public function after(callable $middleware) |
|
279 | } |
||
280 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: