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 | public function __construct( |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function map($method, $path, $handler) |
||
92 | |||
93 | /** |
||
94 | * Add a group of routes to the collection. |
||
95 | * |
||
96 | * @param string $prefix |
||
97 | * @param callable $group |
||
98 | * |
||
99 | * @return \League\Route\RouteGroup |
||
100 | */ |
||
101 | public function group($prefix, callable $group) |
||
108 | |||
109 | /** |
||
110 | * Dispatch the route based on the request. |
||
111 | * |
||
112 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
113 | * @param \Psr\Http\Message\ResponseInterface $response |
||
114 | * |
||
115 | * @return \Psr\Http\Message\ResponseInterface |
||
116 | */ |
||
117 | public function dispatch(ServerRequestInterface $request, ResponseInterface $response) |
||
128 | |||
129 | /** |
||
130 | * Return a fully configured dispatcher. |
||
131 | * |
||
132 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
133 | * |
||
134 | * @return \League\Route\Dispatcher |
||
135 | */ |
||
136 | public function getDispatcher(ServerRequestInterface $request) |
||
146 | |||
147 | /** |
||
148 | * Prepare all routes, build name index and filter out none matching |
||
149 | * routes before being passed off to the parser. |
||
150 | * |
||
151 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | protected function prepRoutes(ServerRequestInterface $request) |
||
185 | |||
186 | /** |
||
187 | * Build an index of named routes. |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | protected function buildNameIndex() |
||
202 | |||
203 | /** |
||
204 | * Process all groups. |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | protected function processGroups() |
||
215 | |||
216 | /** |
||
217 | * Get named route. |
||
218 | * |
||
219 | * @param string $name |
||
220 | * |
||
221 | * @return \League\Route\Route |
||
222 | */ |
||
223 | public function getNamedRoute($name) |
||
233 | |||
234 | /** |
||
235 | * Add a convenient pattern matcher to the internal array for use with all routes. |
||
236 | * |
||
237 | * @param string $alias |
||
238 | * @param string $regex |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | public function addPatternMatcher($alias, $regex) |
||
249 | |||
250 | /** |
||
251 | * Convenience method to convert pre-defined key words in to regex strings. |
||
252 | * |
||
253 | * @param string $path |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | protected function parseRoutePath($path) |
||
261 | } |
||
262 |
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: