summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/Routing
diff options
context:
space:
mode:
authorNathaniel Catchpole2017-10-24 11:58:17 (GMT)
committerNathaniel Catchpole2017-10-24 11:58:17 (GMT)
commit1cbbff1503f00f6cfa00d60fd9598349140c50f6 (patch)
tree77cf6248677670f3797db3ec016665cfbfd940ab /core/lib/Drupal/Core/Routing
parent51f6344c8c7583afb9ee9d156c0ae1fa39f1c48f (diff)
Issue #2883680 by dawehner, Wim Leers, tim.plunkett, larowlan: Force all route filters and route enhancers to be non-lazy
Diffstat (limited to 'core/lib/Drupal/Core/Routing')
-rw-r--r--core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php10
-rw-r--r--core/lib/Drupal/Core/Routing/Enhancer/EntityRevisionRouteEnhancer.php16
-rw-r--r--core/lib/Drupal/Core/Routing/Enhancer/FormRouteEnhancer.php18
-rw-r--r--core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php11
-rw-r--r--core/lib/Drupal/Core/Routing/Enhancer/RouteEnhancerInterface.php12
-rw-r--r--core/lib/Drupal/Core/Routing/EnhancerInterface.php12
-rw-r--r--core/lib/Drupal/Core/Routing/FilterInterface.php12
-rw-r--r--core/lib/Drupal/Core/Routing/LazyRouteEnhancer.php101
-rw-r--r--core/lib/Drupal/Core/Routing/LazyRouteFilter.php102
-rw-r--r--core/lib/Drupal/Core/Routing/MethodFilter.php10
-rw-r--r--core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php40
-rw-r--r--core/lib/Drupal/Core/Routing/RouteFilterInterface.php9
-rw-r--r--core/lib/Drupal/Core/Routing/Router.php131
13 files changed, 117 insertions, 367 deletions
diff --git a/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php b/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
index 13e18f3..96a10c1 100644
--- a/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
+++ b/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
@@ -4,13 +4,12 @@ namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
-use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Filters routes based on the HTTP Content-type header.
*/
-class ContentTypeHeaderMatcher implements RouteFilterInterface {
+class ContentTypeHeaderMatcher implements FilterInterface {
/**
* {@inheritdoc}
@@ -50,11 +49,4 @@ class ContentTypeHeaderMatcher implements RouteFilterInterface {
}
}
- /**
- * {@inheritdoc}
- */
- public function applies(Route $route) {
- return TRUE;
- }
-
}
diff --git a/core/lib/Drupal/Core/Routing/Enhancer/EntityRevisionRouteEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/EntityRevisionRouteEnhancer.php
index 9880edd..d54338f 100644
--- a/core/lib/Drupal/Core/Routing/Enhancer/EntityRevisionRouteEnhancer.php
+++ b/core/lib/Drupal/Core/Routing/Enhancer/EntityRevisionRouteEnhancer.php
@@ -2,6 +2,7 @@
namespace Drupal\Core\Routing\Enhancer;
+use Drupal\Core\Routing\EnhancerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
@@ -9,12 +10,17 @@ use Symfony\Component\Routing\Route;
/**
* Adds _entity_revision to the request attributes, if possible.
*/
-class EntityRevisionRouteEnhancer implements RouteEnhancerInterface {
+class EntityRevisionRouteEnhancer implements EnhancerInterface {
/**
- * {@inheritdoc}
+ * Returns whether the enhancer runs on the current route.
+ *
+ * @param \Symfony\Component\Routing\Route $route
+ * The current route.
+ *
+ * @return bool
*/
- public function applies(Route $route) {
+ protected function applies(Route $route) {
// Check whether there is any entity revision parameter.
$parameters = $route->getOption('parameters') ?: [];
foreach ($parameters as $info) {
@@ -31,6 +37,10 @@ class EntityRevisionRouteEnhancer implements RouteEnhancerInterface {
public function enhance(array $defaults, Request $request) {
/** @var \Symfony\Component\Routing\Route $route */
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
+ if (!$this->applies($route)) {
+ return $defaults;
+ }
+
$options = $route->getOptions();
if (isset($options['parameters'])) {
foreach ($options['parameters'] as $name => $details) {
diff --git a/core/lib/Drupal/Core/Routing/Enhancer/FormRouteEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/FormRouteEnhancer.php
index d2bdb12..c812178 100644
--- a/core/lib/Drupal/Core/Routing/Enhancer/FormRouteEnhancer.php
+++ b/core/lib/Drupal/Core/Routing/Enhancer/FormRouteEnhancer.php
@@ -2,18 +2,25 @@
namespace Drupal\Core\Routing\Enhancer;
+use Drupal\Core\Routing\EnhancerInterface;
+use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Enhancer to add a wrapping controller for _form routes.
*/
-class FormRouteEnhancer implements RouteEnhancerInterface {
+class FormRouteEnhancer implements EnhancerInterface {
/**
- * {@inheritdoc}
+ * Returns whether the enhancer runs on the current route.
+ *
+ * @param \Drupal\Core\Routing\Enhancer\Route $route
+ * The current route.
+ *
+ * @return bool
*/
- public function applies(Route $route) {
+ protected function applies(Route $route) {
return $route->hasDefault('_form') && !$route->hasDefault('_controller');
}
@@ -21,6 +28,11 @@ class FormRouteEnhancer implements RouteEnhancerInterface {
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
+ $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
+ if (!$this->applies($route)) {
+ return $defaults;
+ }
+
$defaults['_controller'] = 'controller.form:getContentResult';
return $defaults;
}
diff --git a/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php
index 09d6148..212839d 100644
--- a/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php
+++ b/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php
@@ -4,6 +4,7 @@ namespace Drupal\Core\Routing\Enhancer;
use Drupal\Core\ParamConverter\ParamConverterManagerInterface;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
+use Drupal\Core\Routing\EnhancerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
@@ -11,12 +12,11 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
-use Symfony\Component\Routing\Route;
/**
* Provides a route enhancer that handles parameter conversion.
*/
-class ParamConversionEnhancer implements RouteEnhancerInterface, EventSubscriberInterface {
+class ParamConversionEnhancer implements EnhancerInterface, EventSubscriberInterface {
/**
* The parameter conversion manager.
@@ -89,11 +89,4 @@ class ParamConversionEnhancer implements RouteEnhancerInterface, EventSubscriber
return $events;
}
- /**
- * {@inheritdoc}
- */
- public function applies(Route $route) {
- return TRUE;
- }
-
}
diff --git a/core/lib/Drupal/Core/Routing/Enhancer/RouteEnhancerInterface.php b/core/lib/Drupal/Core/Routing/Enhancer/RouteEnhancerInterface.php
index d6be823..d376985 100644
--- a/core/lib/Drupal/Core/Routing/Enhancer/RouteEnhancerInterface.php
+++ b/core/lib/Drupal/Core/Routing/Enhancer/RouteEnhancerInterface.php
@@ -2,13 +2,21 @@
namespace Drupal\Core\Routing\Enhancer;
-use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface as BaseRouteEnhancerInterface;
+use Drupal\Core\Routing\EnhancerInterface;
use Symfony\Component\Routing\Route;
+@trigger_error('\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Routing\EnhancerInterface. See https://www.drupal.org/node/2894934', E_USER_DEPRECATED);
+
/**
* A route enhance service to determine route enhance rules.
+ *
+ * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead,
+ * you should use \Drupal\Core\Routing\EnhancerInterface.
+ * See https://www.drupal.org/node/2894934
+ * Part of the deprecation means that applies() is now called on runtime instead
+ * of compile time.
*/
-interface RouteEnhancerInterface extends BaseRouteEnhancerInterface {
+interface RouteEnhancerInterface extends EnhancerInterface {
/**
* Declares if the route enhancer applies to the given route.
diff --git a/core/lib/Drupal/Core/Routing/EnhancerInterface.php b/core/lib/Drupal/Core/Routing/EnhancerInterface.php
new file mode 100644
index 0000000..3ddee66
--- /dev/null
+++ b/core/lib/Drupal/Core/Routing/EnhancerInterface.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace Drupal\Core\Routing;
+
+use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
+
+/**
+ * A route enhance service to determine route enhance rules.
+ */
+interface EnhancerInterface extends RouteEnhancerInterface {
+
+}
diff --git a/core/lib/Drupal/Core/Routing/FilterInterface.php b/core/lib/Drupal/Core/Routing/FilterInterface.php
new file mode 100644
index 0000000..73c9f83
--- /dev/null
+++ b/core/lib/Drupal/Core/Routing/FilterInterface.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace Drupal\Core\Routing;
+
+use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface;
+
+/**
+ * A route filter service to filter down the collection of route instances.
+ */
+interface FilterInterface extends RouteFilterInterface {
+
+}
diff --git a/core/lib/Drupal/Core/Routing/LazyRouteEnhancer.php b/core/lib/Drupal/Core/Routing/LazyRouteEnhancer.php
deleted file mode 100644
index 356fe3e..0000000
--- a/core/lib/Drupal/Core/Routing/LazyRouteEnhancer.php
+++ /dev/null
@@ -1,101 +0,0 @@
-<?php
-
-namespace Drupal\Core\Routing;
-
-use Drupal\Core\Routing\Enhancer\RouteEnhancerInterface;
-use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface as BaseRouteEnhancerInterface;
-use Symfony\Cmf\Component\Routing\RouteObjectInterface;
-use Symfony\Component\DependencyInjection\ContainerAwareInterface;
-use Symfony\Component\DependencyInjection\ContainerAwareTrait;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\Routing\RouteCollection;
-
-/**
- * A route enhancer which lazily loads route enhancers, depending on the route.
- *
- * We lazy initialize route enhancers, because otherwise all dependencies of
- * all route enhancers are initialized on every request, which is slow. However,
- * with the use of lazy loading, dependencies are instantiated only when used.
- */
-class LazyRouteEnhancer implements BaseRouteEnhancerInterface, ContainerAwareInterface {
-
- use ContainerAwareTrait;
-
- /**
- * Array of enhancers service IDs.
- *
- * @var array
- */
- protected $serviceIds = [];
-
- /**
- * The initialized route enhancers.
- *
- * @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
- */
- protected $enhancers = NULL;
-
- /**
- * Constructs the LazyRouteEnhancer object.
- *
- * @param $service_ids
- * Array of enhancers service IDs.
- */
- public function __construct($service_ids) {
- $this->serviceIds = $service_ids;
- }
-
- /**
- * For each route, saves a list of applicable enhancers to the route.
- *
- * @param \Symfony\Component\Routing\RouteCollection $route_collection
- * A collection of routes to apply enhancer checks to.
- */
- public function setEnhancers(RouteCollection $route_collection) {
- /** @var \Symfony\Component\Routing\Route $route **/
- foreach ($route_collection as $route_name => $route) {
- $service_ids = [];
- foreach ($this->getEnhancers() as $service_id => $enhancer) {
- if ((!$enhancer instanceof RouteEnhancerInterface) || $enhancer->applies($route)) {
- $service_ids[] = $service_id;
- }
- }
- if ($service_ids) {
- $route->setOption('_route_enhancers', array_unique($service_ids));
- }
- }
- }
-
- /**
- * For each route, gets a list of applicable enhancer to the route.
- *
- * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
- */
- protected function getEnhancers() {
- if (!isset($this->enhancers)) {
- foreach ($this->serviceIds as $service_id) {
- $this->enhancers[$service_id] = $this->container->get($service_id);
- }
- }
- return $this->enhancers;
- }
-
- /**
- * {@inheritdoc}
- */
- public function enhance(array $defaults, Request $request) {
- /** @var \Symfony\Component\Routing\Route $route */
- $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
-
- $enhancer_ids = $route->getOption('_route_enhancers');
-
- if (isset($enhancer_ids)) {
- foreach ($enhancer_ids as $enhancer_id) {
- $defaults = $this->container->get($enhancer_id)->enhance($defaults, $request);
- }
- }
-
- return $defaults;
- }
-
-}
diff --git a/core/lib/Drupal/Core/Routing/LazyRouteFilter.php b/core/lib/Drupal/Core/Routing/LazyRouteFilter.php
deleted file mode 100644
index 10f3d51..0000000
--- a/core/lib/Drupal/Core/Routing/LazyRouteFilter.php
+++ /dev/null
@@ -1,102 +0,0 @@
-<?php
-
-namespace Drupal\Core\Routing;
-
-use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface as BaseRouteFilterInterface;
-use Symfony\Component\DependencyInjection\ContainerAwareInterface;
-use Symfony\Component\DependencyInjection\ContainerAwareTrait;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\Routing\RouteCollection;
-
-/**
- * A route filter which lazily loads route filters, depending on the route.
- *
- * We lazy initialize route filters, because otherwise all dependencies of all
- * route filters are initialized on every request, which is slow. However, with
- * the use of lazy loading, dependencies are instantiated only when used.
- */
-class LazyRouteFilter implements BaseRouteFilterInterface, ContainerAwareInterface {
-
- use ContainerAwareTrait;
-
- /**
- * Array of route filter service IDs.
- *
- * @var array
- */
- protected $serviceIds = [];
-
- /**
- * The initialized route filters.
- *
- * @var \Drupal\Core\Routing\RouteFilterInterface[]
- */
- protected $filters = NULL;
-
- /**
- * Constructs the LazyRouteEnhancer object.
- *
- * @param $service_ids
- * Array of route filter service IDs.
- */
- public function __construct($service_ids) {
- $this->serviceIds = $service_ids;
- }
-
- /**
- * For each route, filter down the route collection.
- *
- * @param \Symfony\Component\Routing\RouteCollection $route_collection
- * A collection of routes to apply filter checks to.
- */
- public function setFilters(RouteCollection $route_collection) {
- /** @var \Symfony\Component\Routing\Route $route **/
- foreach ($route_collection as $route) {
- $service_ids = [];
- foreach ($this->getFilters() as $service_id => $filter) {
- if ($filter instanceof RouteFilterInterface && $filter->applies($route)) {
- $service_ids[] = $service_id;
- }
- }
- if ($service_ids) {
- $route->setOption('_route_filters', array_unique($service_ids));
- }
- }
- }
-
- /**
- * For each route, gets a list of applicable enhancers to the route.
- *
- * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
- */
- protected function getFilters() {
- if (!isset($this->filters)) {
- foreach ($this->serviceIds as $service_id) {
- $this->filters[$service_id] = $this->container->get($service_id);
- }
- }
- return $this->filters;
- }
-
- /**
- * {@inheritdoc}
- */
- public function filter(RouteCollection $collection, Request $request) {
- $filter_ids = [];
- foreach ($collection->all() as $route) {
- $filter_ids = array_merge($filter_ids, $route->getOption('_route_filters') ?: []);
- }
- $filter_ids = array_unique($filter_ids);
-
- if (isset($filter_ids)) {
- foreach ($filter_ids as $filter_id) {
- if ($filter = $this->container->get($filter_id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
- $collection = $filter->filter($collection, $request);
- }
- }
- }
- return $collection;
- }
-
-}
diff --git a/core/lib/Drupal/Core/Routing/MethodFilter.php b/core/lib/Drupal/Core/Routing/MethodFilter.php
index e565a3e..4d24aa5 100644
--- a/core/lib/Drupal/Core/Routing/MethodFilter.php
+++ b/core/lib/Drupal/Core/Routing/MethodFilter.php
@@ -4,13 +4,12 @@ namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
-use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Filters routes based on the HTTP method.
*/
-class MethodFilter implements RouteFilterInterface {
+class MethodFilter implements FilterInterface {
/**
* {@inheritdoc}
@@ -47,11 +46,4 @@ class MethodFilter implements RouteFilterInterface {
throw new MethodNotAllowedException(array_unique($all_supported_methods));
}
- /**
- * {@inheritdoc}
- */
- public function applies(Route $route) {
- return !empty($route->getMethods());
- }
-
}
diff --git a/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php b/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php
index 547e611..dbfb801 100644
--- a/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php
+++ b/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php
@@ -4,20 +4,12 @@ namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
-use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides a route filter, which filters by the request format.
*/
-class RequestFormatRouteFilter implements RouteFilterInterface {
-
- /**
- * {@inheritdoc}
- */
- public function applies(Route $route) {
- return $route->hasRequirement('_format');
- }
+class RequestFormatRouteFilter implements FilterInterface {
/**
* {@inheritdoc}
@@ -27,22 +19,34 @@ class RequestFormatRouteFilter implements RouteFilterInterface {
$default_format = static::getDefaultFormat($collection);
$format = $request->getRequestFormat($default_format);
+ $routes_with_requirement = [];
+ $routes_without_requirement = [];
+ $result_collection = new RouteCollection();
/** @var \Symfony\Component\Routing\Route $route */
foreach ($collection as $name => $route) {
- // If the route has no _format specification, we move it to the end. If it
- // does, then no match means the route is removed entirely.
- if ($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) {
- if (!in_array($format, $supported_formats)) {
- $collection->remove($name);
- }
+ if (!$route->hasRequirement('_format')) {
+ $routes_without_requirement[$name] = $route;
+ continue;
}
else {
- $collection->add($name, $route);
+ $routes_with_requirement[$name] = $route;
}
}
- if (count($collection)) {
- return $collection;
+ foreach ($routes_with_requirement as $name => $route) {
+ // If the route has no _format specification, we move it to the end. If it
+ // does, then no match means the route is removed entirely.
+ if (($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) && in_array($format, $supported_formats, TRUE)) {
+ $result_collection->add($name, $route);
+ }
+ }
+
+ foreach ($routes_without_requirement as $name => $route) {
+ $result_collection->add($name, $route);
+ }
+
+ if (count($result_collection)) {
+ return $result_collection;
}
// We do not throw a
diff --git a/core/lib/Drupal/Core/Routing/RouteFilterInterface.php b/core/lib/Drupal/Core/Routing/RouteFilterInterface.php
index a3ccb44..efe97a2 100644
--- a/core/lib/Drupal/Core/Routing/RouteFilterInterface.php
+++ b/core/lib/Drupal/Core/Routing/RouteFilterInterface.php
@@ -2,13 +2,18 @@
namespace Drupal\Core\Routing;
-use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface as BaseRouteFilterInterface;
use Symfony\Component\Routing\Route;
+@trigger_error('\Drupal\Core\Routing\Enhancer\RouteFilterInterface is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Routing\FilterInterface. See https://www.drupal.org/node/2894934', E_USER_DEPRECATED);
+
/**
* A route filter service to filter down the collection of route instances.
+ *
+ * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead,
+ * you should use \Drupal\Core\Routing\FilterInterface.
+ * See https://www.drupal.org/node/2894934
*/
-interface RouteFilterInterface extends BaseRouteFilterInterface {
+interface RouteFilterInterface extends FilterInterface {
/**
* Determines if the route filter applies to the given route.
diff --git a/core/lib/Drupal/Core/Routing/Router.php b/core/lib/Drupal/Core/Routing/Router.php
index e949c5e..ee2dc28 100644
--- a/core/lib/Drupal/Core/Routing/Router.php
+++ b/core/lib/Drupal/Core/Routing/Router.php
@@ -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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
/**
* 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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
}
/**
- * Adds a route enhancer to the list of used route enhancers.
- *
- * @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.
+ * Adds a route filter.
*
- * @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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
*/
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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
* 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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
}
/**
- * 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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
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 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf
}
/**
- * 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() {