diff core/lib/Drupal/Core/Routing/Router.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
line wrap: on
line diff
--- a/core/lib/Drupal/Core/Routing/Router.php	Mon Apr 23 09:33:26 2018 +0100
+++ b/core/lib/Drupal/Core/Routing/Router.php	Mon Apr 23 09:46:53 2018 +0100
@@ -3,9 +3,9 @@
 namespace Drupal\Core\Routing;
 
 use Drupal\Core\Path\CurrentPathStack;
-use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface as BaseRouteEnhancerInterface;
+use Drupal\Core\Routing\Enhancer\RouteEnhancerInterface;
 use Symfony\Cmf\Component\Routing\LazyRouteCollection;
-use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface as BaseRouteFilterInterface;
+use Symfony\Cmf\Component\Routing\RouteObjectInterface;
 use Symfony\Cmf\Component\Routing\RouteProviderInterface as BaseRouteProviderInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
@@ -53,32 +53,18 @@
   /**
    * The list of available enhancers.
    *
-   * @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
+   * @var \Drupal\Core\Routing\EnhancerInterface[]
    */
   protected $enhancers = [];
 
   /**
-   * Cached sorted list of enhancers.
-   *
-   * @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
-   */
-  protected $sortedEnhancers;
-
-  /**
    * The list of available route filters.
    *
-   * @var \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
+   * @var \Drupal\Core\Routing\FilterInterface[]
    */
   protected $filters = [];
 
   /**
-   * Cached sorted list route filters.
-   *
-   * @var \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
-   */
-  protected $sortedFilters;
-
-  /**
    * The URL generator.
    *
    * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface
@@ -102,36 +88,23 @@
   }
 
   /**
-   * Adds a route enhancer to the list of used route enhancers.
+   * Adds a route filter.
    *
-   * @param \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface $route_enhancer
-   *   A route enhancer.
-   * @param int $priority
-   *   (optional) The priority of the enhancer. Higher number enhancers will be
-   *   used first.
-   *
-   * @return $this
+   * @param \Drupal\Core\Routing\FilterInterface $route_filter
+   *   The route filter.
    */
-  public function addRouteEnhancer(BaseRouteEnhancerInterface $route_enhancer, $priority = 0) {
-    $this->enhancers[$priority][] = $route_enhancer;
-    return $this;
+  public function addRouteFilter(FilterInterface $route_filter) {
+    $this->filters[] = $route_filter;
   }
 
   /**
-   * Adds a route filter to the list of used route filters.
+   * Adds a route enhancer.
    *
-   * @param \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface $route_filter
-   *   A route filter.
-   * @param int $priority
-   *   (optional) The priority of the filter. Higher number filters will be used
-   *   first.
-   *
-   * @return $this
+   * @param \Drupal\Core\Routing\EnhancerInterface $route_enhancer
+   *   The route enhancer.
    */
-  public function addRouteFilter(BaseRouteFilterInterface $route_filter, $priority = 0) {
-    $this->filters[$priority][] = $route_filter;
-
-    return $this;
+  public function addRouteEnhancer(EnhancerInterface $route_enhancer) {
+    $this->enhancers[] = $route_enhancer;
   }
 
   /**
@@ -148,6 +121,9 @@
    */
   public function matchRequest(Request $request) {
     $collection = $this->getInitialRouteCollection($request);
+    if ($collection->count() === 0) {
+      throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $this->currentPath->getPath()));
+    }
     $collection = $this->applyRouteFilters($collection, $request);
 
     if ($ret = $this->matchCollection(rawurldecode($this->currentPath->getPath($request)), $collection)) {
@@ -276,7 +252,10 @@
    *   from route enhancers.
    */
   protected function applyRouteEnhancers($defaults, Request $request) {
-    foreach ($this->getRouteEnhancers() as $enhancer) {
+    foreach ($this->enhancers as $enhancer) {
+      if ($enhancer instanceof RouteEnhancerInterface && !$enhancer->applies($defaults[RouteObjectInterface::ROUTE_OBJECT])) {
+        continue;
+      }
       $defaults = $enhancer->enhance($defaults, $request);
     }
 
@@ -284,39 +263,6 @@
   }
 
   /**
-   * Sorts the enhancers and flattens them.
-   *
-   * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
-   *   The enhancers ordered by priority.
-   */
-  public function getRouteEnhancers() {
-    if (!isset($this->sortedEnhancers)) {
-      $this->sortedEnhancers = $this->sortRouteEnhancers();
-    }
-
-    return $this->sortedEnhancers;
-  }
-
-  /**
-   * Sort enhancers by priority.
-   *
-   * The highest priority number is the highest priority (reverse sorting).
-   *
-   * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
-   *   The sorted enhancers.
-   */
-  protected function sortRouteEnhancers() {
-    $sortedEnhancers = [];
-    krsort($this->enhancers);
-
-    foreach ($this->enhancers as $enhancers) {
-      $sortedEnhancers = array_merge($sortedEnhancers, $enhancers);
-    }
-
-    return $sortedEnhancers;
-  }
-
-  /**
    * Applies all route filters to a given route collection.
    *
    * This method reduces the sets of routes further down, for example by
@@ -333,7 +279,7 @@
   protected function applyRouteFilters(RouteCollection $collection, Request $request) {
     // Route filters are expected to throw an exception themselves if they
     // end up filtering the list down to 0.
-    foreach ($this->getRouteFilters() as $filter) {
+    foreach ($this->filters as $filter) {
       $collection = $filter->filter($collection, $request);
     }
 
@@ -341,39 +287,6 @@
   }
 
   /**
-   * Sorts the filters and flattens them.
-   *
-   * @return \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
-   *   The filters ordered by priority
-   */
-  public function getRouteFilters() {
-    if (!isset($this->sortedFilters)) {
-      $this->sortedFilters = $this->sortFilters();
-    }
-
-    return $this->sortedFilters;
-  }
-
-  /**
-   * Sort filters by priority.
-   *
-   * The highest priority number is the highest priority (reverse sorting).
-   *
-   * @return \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
-   *   The sorted filters.
-   */
-  protected function sortFilters() {
-    $sortedFilters = [];
-    krsort($this->filters);
-
-    foreach ($this->filters as $filters) {
-      $sortedFilters = array_merge($sortedFilters, $filters);
-    }
-
-    return $sortedFilters;
-  }
-
-  /**
    * {@inheritdoc}
    */
   public function getRouteCollection() {