1 | <?php |
||
21 | class RouteCollection extends RouteCollector implements |
||
22 | MiddlewareAwareInterface, |
||
23 | RouteCollectionInterface, |
||
24 | StrategyAwareInterface |
||
25 | { |
||
26 | use MiddlewareAwareTrait; |
||
27 | use RouteCollectionMapTrait; |
||
28 | use StrategyAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * @var \Interop\Container\ContainerInterface |
||
32 | */ |
||
33 | protected $container; |
||
34 | |||
35 | /** |
||
36 | * @var \League\Route\Route[] |
||
37 | */ |
||
38 | protected $routes = []; |
||
39 | |||
40 | /** |
||
41 | * @var \League\Route\Route[] |
||
42 | */ |
||
43 | protected $namedRoutes = []; |
||
44 | |||
45 | /** |
||
46 | * @var \League\Route\RouteGroup[] |
||
47 | */ |
||
48 | protected $groups = []; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $patternMatchers = [ |
||
54 | '/{(.+?):number}/' => '{$1:[0-9]+}', |
||
55 | '/{(.+?):word}/' => '{$1:[a-zA-Z]+}', |
||
56 | '/{(.+?):alphanum_dash}/' => '{$1:[a-zA-Z0-9-_]+}', |
||
57 | '/{(.+?):slug}/' => '{$1:[a-z0-9-]+}', |
||
58 | '/{(.+?):uuid}/' => '{$1:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+}' |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param \Interop\Container\ContainerInterface $container |
||
65 | * @param \FastRoute\RouteParser $parser |
||
66 | * @param \FastRoute\DataGenerator $generator |
||
67 | */ |
||
68 | 36 | public function __construct( |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 21 | 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 | 6 | public function group($prefix, callable $group) |
|
109 | |||
110 | /** |
||
111 | * Dispatch the route based on the request. |
||
112 | * |
||
113 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
114 | * @param \Psr\Http\Message\ResponseInterface $response |
||
115 | * |
||
116 | * @return \Psr\Http\Message\ResponseInterface |
||
117 | */ |
||
118 | 15 | public function dispatch(ServerRequestInterface $request, ResponseInterface $response) |
|
129 | |||
130 | /** |
||
131 | * Return a fully configured dispatcher. |
||
132 | * |
||
133 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
134 | * |
||
135 | * @return \League\Route\Dispatcher |
||
136 | */ |
||
137 | 18 | public function getDispatcher(ServerRequestInterface $request) |
|
147 | |||
148 | /** |
||
149 | * Prepare all routes, build name index and filter out none matching |
||
150 | * routes before being passed off to the parser. |
||
151 | * |
||
152 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 18 | protected function prepRoutes(ServerRequestInterface $request) |
|
186 | |||
187 | /** |
||
188 | * Build an index of named routes. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | 27 | protected function buildNameIndex() |
|
203 | |||
204 | /** |
||
205 | * Process all groups. |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | 27 | protected function processGroups() |
|
216 | |||
217 | /** |
||
218 | * Get named route. |
||
219 | * |
||
220 | * @param string $name |
||
221 | * |
||
222 | * @return \League\Route\Route |
||
223 | */ |
||
224 | 9 | public function getNamedRoute($name) |
|
234 | |||
235 | /** |
||
236 | * Add a convenient pattern matcher to the internal array for use with all routes. |
||
237 | * |
||
238 | * @param string $alias |
||
239 | * @param string $regex |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | 3 | public function addPatternMatcher($alias, $regex) |
|
250 | |||
251 | /** |
||
252 | * Convenience method to convert pre-defined key words in to regex strings. |
||
253 | * |
||
254 | * @param string $path |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | 12 | protected function parseRoutePath($path) |
|
262 | } |
||
263 |
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: