comparison core/lib/Drupal/Core/Routing/Router.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
123 $collection = $this->getInitialRouteCollection($request); 123 $collection = $this->getInitialRouteCollection($request);
124 if ($collection->count() === 0) { 124 if ($collection->count() === 0) {
125 throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $this->currentPath->getPath())); 125 throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $this->currentPath->getPath()));
126 } 126 }
127 $collection = $this->applyRouteFilters($collection, $request); 127 $collection = $this->applyRouteFilters($collection, $request);
128 $collection = $this->applyFitOrder($collection);
128 129
129 if ($ret = $this->matchCollection(rawurldecode($this->currentPath->getPath($request)), $collection)) { 130 if ($ret = $this->matchCollection(rawurldecode($this->currentPath->getPath($request)), $collection)) {
130 return $this->applyRouteEnhancers($ret, $request); 131 return $this->applyRouteEnhancers($ret, $request);
131 } 132 }
132 133
285 286
286 return $collection; 287 return $collection;
287 } 288 }
288 289
289 /** 290 /**
291 * Reapplies the fit order to a RouteCollection object.
292 *
293 * Route filters can reorder route collections. For example, routes with an
294 * explicit _format requirement will be preferred. This can result in a less
295 * fit route being used. For example, as a result of filtering /user/% comes
296 * before /user/login. In order to not break this fundamental property of
297 * routes, we need to reapply the fit order. We also need to ensure that order
298 * within each group of the same fit is preserved.
299 *
300 * @param \Symfony\Component\Routing\RouteCollection $collection
301 * The route collection.
302 *
303 * @return \Symfony\Component\Routing\RouteCollection
304 * The reordered route collection.
305 */
306 protected function applyFitOrder(RouteCollection $collection) {
307 $buckets = [];
308 // Sort all the routes by fit descending.
309 foreach ($collection->all() as $name => $route) {
310 $fit = $route->compile()->getFit();
311 $buckets += [$fit => []];
312 $buckets[$fit][] = [$name, $route];
313 }
314 krsort($buckets);
315
316 $flattened = array_reduce($buckets, 'array_merge', []);
317
318 // Add them back onto a new route collection.
319 $collection = new RouteCollection();
320 foreach ($flattened as $pair) {
321 $name = $pair[0];
322 $route = $pair[1];
323 $collection->add($name, $route);
324 }
325 return $collection;
326 }
327
328 /**
290 * {@inheritdoc} 329 * {@inheritdoc}
291 */ 330 */
292 public function getRouteCollection() { 331 public function getRouteCollection() {
293 return new LazyRouteCollection($this->routeProvider); 332 return new LazyRouteCollection($this->routeProvider);
294 } 333 }