1 | <?php |
||
23 | class RouteCollection extends RouteCollector implements |
||
24 | MiddlewareAwareInterface, |
||
25 | RouteCollectionInterface, |
||
26 | StrategyAwareInterface |
||
27 | { |
||
28 | use MiddlewareAwareTrait; |
||
29 | use RouteCollectionMapTrait; |
||
30 | use StrategyAwareTrait; |
||
31 | |||
32 | /** |
||
33 | * @var \Psr\Container\ContainerInterface |
||
34 | */ |
||
35 | protected $container; |
||
36 | |||
37 | /** |
||
38 | * @var \League\Route\Route[] |
||
39 | */ |
||
40 | protected $routes = []; |
||
41 | |||
42 | /** |
||
43 | * @var \League\Route\Route[] |
||
44 | */ |
||
45 | protected $namedRoutes = []; |
||
46 | |||
47 | /** |
||
48 | * @var \League\Route\RouteGroup[] |
||
49 | */ |
||
50 | protected $groups = []; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $patternMatchers = [ |
||
56 | '/{(.+?):number}/' => '{$1:[0-9]+}', |
||
57 | '/{(.+?):word}/' => '{$1:[a-zA-Z]+}', |
||
58 | '/{(.+?):alphanum_dash}/' => '{$1:[a-zA-Z0-9-_]+}', |
||
59 | '/{(.+?):slug}/' => '{$1:[a-z0-9-]+}', |
||
60 | '/{(.+?):uuid}/' => '{$1:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}+}' |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * Constructor. |
||
65 | * |
||
66 | * @param \Psr\Container\ContainerInterface $container |
||
67 | * @param \FastRoute\RouteParser $parser |
||
68 | * @param \FastRoute\DataGenerator $generator |
||
69 | */ |
||
70 | 45 | public function __construct( |
|
71 | ContainerInterface $container = null, |
||
72 | RouteParser $parser = null, |
||
73 | DataGenerator $generator = null |
||
74 | ) { |
||
75 | 45 | $this->container = ($container instanceof ContainerInterface) ? $container : new Container; |
|
76 | |||
77 | // build parent route collector |
||
78 | 45 | $parser = ($parser instanceof RouteParser) ? $parser : new StdRouteParser; |
|
79 | 45 | $generator = ($generator instanceof DataGenerator) ? $generator : new GroupCountBasedDataGenerator; |
|
80 | 45 | parent::__construct($parser, $generator); |
|
81 | 45 | } |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 30 | public function map($method, $path, $handler) |
|
95 | |||
96 | /** |
||
97 | * Add a group of routes to the collection. |
||
98 | * |
||
99 | * @param string $prefix |
||
100 | * @param callable $group |
||
101 | * |
||
102 | * @return \League\Route\RouteGroup |
||
103 | */ |
||
104 | 6 | 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 | 24 | public function dispatch(ServerRequestInterface $request, ResponseInterface $response) |
|
121 | { |
||
122 | 24 | $dispatcher = $this->getDispatcher($request); |
|
123 | 24 | $execChain = $dispatcher->handle($request); |
|
124 | |||
125 | 18 | foreach ($this->getMiddlewareStack() as $middleware) { |
|
126 | $execChain->middleware($middleware); |
||
127 | } |
||
128 | |||
129 | try { |
||
130 | 18 | return $execChain->execute($request, $response); |
|
131 | 9 | } catch (Exception $exception) { |
|
132 | 9 | $middleware = $this->getStrategy()->getExceptionDecorator($exception); |
|
133 | 6 | return (new ExecutionChain)->middleware($middleware)->execute($request, $response); |
|
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Return a fully configured dispatcher. |
||
139 | * |
||
140 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
141 | * |
||
142 | * @return \League\Route\Dispatcher |
||
143 | */ |
||
144 | 27 | public function getDispatcher(ServerRequestInterface $request) |
|
145 | { |
||
146 | 27 | if (is_null($this->getStrategy())) { |
|
147 | 15 | $this->setStrategy(new ApplicationStrategy); |
|
148 | } |
||
149 | |||
150 | 27 | $this->prepRoutes($request); |
|
151 | |||
152 | 27 | return (new Dispatcher($this->getData()))->setStrategy($this->getStrategy()); |
|
|
|||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Prepare all routes, build name index and filter out none matching |
||
157 | * routes before being passed off to the parser. |
||
158 | * |
||
159 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | 27 | protected function prepRoutes(ServerRequestInterface $request) |
|
164 | { |
||
165 | 27 | $this->buildNameIndex(); |
|
166 | |||
167 | 27 | $routes = array_merge(array_values($this->routes), array_values($this->namedRoutes)); |
|
168 | |||
169 | 27 | foreach ($routes as $key => $route) { |
|
170 | // check for scheme condition |
||
171 | 21 | if (! is_null($route->getScheme()) && $route->getScheme() !== $request->getUri()->getScheme()) { |
|
172 | 3 | continue; |
|
173 | } |
||
174 | |||
175 | // check for domain condition |
||
176 | 21 | if (! is_null($route->getHost()) && $route->getHost() !== $request->getUri()->getHost()) { |
|
177 | 3 | continue; |
|
178 | } |
||
179 | |||
180 | 21 | $route->setContainer($this->container); |
|
181 | |||
182 | 21 | if (is_null($route->getStrategy())) { |
|
183 | 21 | $route->setStrategy($this->getStrategy()); |
|
184 | } |
||
185 | |||
186 | 21 | $this->addRoute( |
|
187 | 21 | $route->getMethods(), |
|
188 | 21 | $this->parseRoutePath($route->getPath()), |
|
189 | 21 | [$route, 'getExecutionChain'] |
|
190 | ); |
||
191 | } |
||
192 | 27 | } |
|
193 | |||
194 | /** |
||
195 | * Build an index of named routes. |
||
196 | * |
||
197 | * @return void |
||
198 | */ |
||
199 | 36 | protected function buildNameIndex() |
|
200 | { |
||
201 | 36 | $this->processGroups(); |
|
202 | |||
203 | 36 | foreach ($this->routes as $key => $route) { |
|
204 | 27 | if (! is_null($route->getName())) { |
|
205 | 6 | unset($this->routes[$key]); |
|
206 | 13 | $this->namedRoutes[$route->getName()] = $route; |
|
207 | } |
||
208 | } |
||
209 | 36 | } |
|
210 | |||
211 | /** |
||
212 | * Process all groups. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | 36 | protected function processGroups() |
|
217 | { |
||
218 | 36 | foreach ($this->groups as $key => $group) { |
|
219 | 3 | unset($this->groups[$key]); |
|
220 | 3 | $group(); |
|
221 | } |
||
222 | 36 | } |
|
223 | |||
224 | /** |
||
225 | * Get named route. |
||
226 | * |
||
227 | * @param string $name |
||
228 | * |
||
229 | * @return \League\Route\Route |
||
230 | */ |
||
231 | 9 | public function getNamedRoute($name) |
|
241 | |||
242 | /** |
||
243 | * Add a convenient pattern matcher to the internal array for use with all routes. |
||
244 | * |
||
245 | * @param string $alias |
||
246 | * @param string $regex |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | 3 | public function addPatternMatcher($alias, $regex) |
|
257 | |||
258 | /** |
||
259 | * Convenience method to convert pre-defined key words in to regex strings. |
||
260 | * |
||
261 | * @param string $path |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | 21 | protected function parseRoutePath($path) |
|
269 | } |
||
270 |
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: