3 namespace Drupal\Core\Routing;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Path\CurrentPathStack;
11 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
12 use Drupal\Core\State\StateInterface;
13 use Symfony\Cmf\Component\Routing\PagedRouteCollection;
14 use Symfony\Cmf\Component\Routing\PagedRouteProviderInterface;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\Routing\Exception\RouteNotFoundException;
18 use Symfony\Component\Routing\RouteCollection;
19 use Drupal\Core\Database\Connection;
22 * A Route Provider front-end for all Drupal-stored routes.
24 class RouteProvider implements PreloadableRouteProviderInterface, PagedRouteProviderInterface, EventSubscriberInterface {
27 * The database connection from which to read route information.
29 * @var \Drupal\Core\Database\Connection
31 protected $connection;
34 * The name of the SQL table from which to read the routes.
43 * @var \Drupal\Core\State\StateInterface
48 * A cache of already-loaded routes, keyed by route name.
50 * @var \Symfony\Component\Routing\Route[]
52 protected $routes = [];
55 * A cache of already-loaded serialized routes, keyed by route name.
59 protected $serializedRoutes = [];
64 * @var \Drupal\Core\Path\CurrentPathStack
66 protected $currentPath;
71 * @var \Drupal\Core\Cache\CacheBackendInterface
76 * The cache tag invalidator.
78 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
80 protected $cacheTagInvalidator;
83 * A path processor manager for resolving the system path.
85 * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
87 protected $pathProcessor;
90 * The language manager.
92 * @var \Drupal\Core\Language\LanguageManagerInterface
94 protected $languageManager;
97 * Cache ID prefix used to load routes.
99 const ROUTE_LOAD_CID_PREFIX = 'route_provider.route_load:';
102 * Constructs a new PathMatcher.
104 * @param \Drupal\Core\Database\Connection $connection
105 * A database connection object.
106 * @param \Drupal\Core\State\StateInterface $state
108 * @param \Drupal\Core\Path\CurrentPathStack $current_path
110 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
112 * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $path_processor
113 * The path processor.
114 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tag_invalidator
115 * The cache tag invalidator.
116 * @param string $table
117 * (Optional) The table in the database to use for matching. Defaults to 'router'
118 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
119 * (Optional) The language manager.
121 public function __construct(Connection $connection, StateInterface $state, CurrentPathStack $current_path, CacheBackendInterface $cache_backend, InboundPathProcessorInterface $path_processor, CacheTagsInvalidatorInterface $cache_tag_invalidator, $table = 'router', LanguageManagerInterface $language_manager = NULL) {
122 $this->connection = $connection;
123 $this->state = $state;
124 $this->currentPath = $current_path;
125 $this->cache = $cache_backend;
126 $this->cacheTagInvalidator = $cache_tag_invalidator;
127 $this->pathProcessor = $path_processor;
128 $this->tableName = $table;
129 $this->languageManager = $language_manager ?: \Drupal::languageManager();
133 * Finds routes that may potentially match the request.
135 * This may return a mixed list of class instances, but all routes returned
136 * must extend the core symfony route. The classes may also implement
137 * RouteObjectInterface to link to a content document.
139 * This method may not throw an exception based on implementation specific
140 * restrictions on the url. That case is considered a not found - returning
141 * an empty array. Exceptions are only used to abort the whole request in
142 * case something is seriously broken, like the storage backend being down.
144 * Note that implementations may not implement an optimal matching
145 * algorithm, simply a reasonable first pass. That allows for potentially
146 * very large route sets to be filtered down to likely candidates, which
147 * may then be filtered in memory more completely.
149 * @param \Symfony\Component\HttpFoundation\Request $request
150 * A request against which to match.
152 * @return \Symfony\Component\Routing\RouteCollection
153 * RouteCollection with all urls that could potentially match $request.
154 * Empty collection if nothing can match. The collection will be sorted from
155 * highest to lowest fit (match of path parts) and then in ascending order
156 * by route name for routes with the same fit.
158 public function getRouteCollectionForRequest(Request $request) {
159 // Cache both the system path as well as route parameters and matching
161 $cid = $this->getRouteCollectionCacheId($request);
162 if ($cached = $this->cache->get($cid)) {
163 $this->currentPath->setPath($cached->data['path'], $request);
164 $request->query->replace($cached->data['query']);
165 return $cached->data['routes'];
168 // Just trim on the right side.
169 $path = $request->getPathInfo();
170 $path = $path === '/' ? $path : rtrim($request->getPathInfo(), '/');
171 $path = $this->pathProcessor->processInbound($path, $request);
172 $this->currentPath->setPath($path, $request);
173 // Incoming path processors may also set query parameters.
174 $query_parameters = $request->query->all();
175 $routes = $this->getRoutesByPath(rtrim($path, '/'));
178 'query' => $query_parameters,
181 $this->cache->set($cid, $cache_value, CacheBackendInterface::CACHE_PERMANENT, ['route_match']);
187 * Find the route using the provided route name (and parameters).
189 * @param string $name
190 * The route name to fetch
192 * @return \Symfony\Component\Routing\Route
195 * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
196 * Thrown if there is no route with that name in this repository.
198 public function getRouteByName($name) {
199 $routes = $this->getRoutesByNames([$name]);
200 if (empty($routes)) {
201 throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
204 return reset($routes);
210 public function preLoadRoutes($names) {
212 throw new \InvalidArgumentException('You must specify the route names to load');
215 $routes_to_load = array_diff($names, array_keys($this->routes), array_keys($this->serializedRoutes));
216 if ($routes_to_load) {
218 $cid = static::ROUTE_LOAD_CID_PREFIX . hash('sha512', serialize($routes_to_load));
219 if ($cache = $this->cache->get($cid)) {
220 $routes = $cache->data;
224 $result = $this->connection->query('SELECT name, route FROM {' . $this->connection->escapeTable($this->tableName) . '} WHERE name IN ( :names[] )', [':names[]' => $routes_to_load]);
225 $routes = $result->fetchAllKeyed();
227 $this->cache->set($cid, $routes, Cache::PERMANENT, ['routes']);
229 catch (\Exception $e) {
234 $this->serializedRoutes += $routes;
241 public function getRoutesByNames($names) {
242 $this->preLoadRoutes($names);
244 foreach ($names as $name) {
245 // The specified route name might not exist or might be serialized.
246 if (!isset($this->routes[$name]) && isset($this->serializedRoutes[$name])) {
247 $this->routes[$name] = unserialize($this->serializedRoutes[$name]);
248 unset($this->serializedRoutes[$name]);
252 return array_intersect_key($this->routes, array_flip($names));
256 * Returns an array of path pattern outlines that could match the path parts.
258 * @param array $parts
259 * The parts of the path for which we want candidates.
262 * An array of outlines that could match the specified path parts.
264 protected function getCandidateOutlines(array $parts) {
265 $number_parts = count($parts);
267 $length = $number_parts - 1;
268 $end = (1 << $number_parts) - 1;
270 // The highest possible mask is a 1 bit for every part of the path. We will
271 // check every value down from there to generate a possible outline.
272 if ($number_parts == 1) {
275 elseif ($number_parts <= 3 && $number_parts > 0) {
276 // Optimization - don't query the state system for short paths. This also
277 // insulates against the state entry for masks going missing for common
278 // user-facing paths since we generate all values without checking state.
279 $masks = range($end, 1);
281 elseif ($number_parts <= 0) {
282 // No path can match, short-circuit the process.
286 // Get the actual patterns that exist out of state.
287 $masks = (array) $this->state->get('routing.menu_masks.' . $this->tableName, []);
290 // Only examine patterns that actually exist as router items (the masks).
291 foreach ($masks as $i) {
293 // Only look at masks that are not longer than the path of interest.
296 elseif ($i < (1 << $length)) {
297 // We have exhausted the masks of a given length, so decrease the length.
301 for ($j = $length; $j >= 0; $j--) {
302 // Check the bit on the $j offset.
303 if ($i & (1 << $j)) {
304 // Bit one means the original value.
305 $current .= $parts[$length - $j];
308 // Bit zero means means wildcard.
311 // Unless we are at offset 0, add a slash.
316 $ancestors[] = '/' . $current;
324 public function getRoutesByPattern($pattern) {
325 $path = RouteCompiler::getPatternOutline($pattern);
327 return $this->getRoutesByPath($path);
331 * Get all routes which match a certain pattern.
333 * @param string $path
334 * The route pattern to search for.
336 * @return \Symfony\Component\Routing\RouteCollection
337 * Returns a route collection of matching routes. The collection may be
338 * empty and will be sorted from highest to lowest fit (match of path parts)
339 * and then in ascending order by route name for routes with the same fit.
341 protected function getRoutesByPath($path) {
342 // Split the path up on the slashes, ignoring multiple slashes in a row
343 // or leading or trailing slashes. Convert to lower case here so we can
344 // have a case-insensitive match from the incoming path to the lower case
345 // pattern outlines from \Drupal\Core\Routing\RouteCompiler::compile().
346 // @see \Drupal\Core\Routing\CompiledRoute::__construct()
347 $parts = preg_split('@/+@', mb_strtolower($path), NULL, PREG_SPLIT_NO_EMPTY);
349 $collection = new RouteCollection();
351 $ancestors = $this->getCandidateOutlines($parts);
352 if (empty($ancestors)) {
356 // The >= check on number_parts allows us to match routes with optional
357 // trailing wildcard parts as long as the pattern matches, since we
358 // dump the route pattern without those optional parts.
360 $routes = $this->connection->query("SELECT name, route, fit FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN ( :patterns[] ) AND number_parts >= :count_parts", [
361 ':patterns[]' => $ancestors,
362 ':count_parts' => count($parts),
364 ->fetchAll(\PDO::FETCH_ASSOC);
366 catch (\Exception $e) {
370 // We sort by fit and name in PHP to avoid a SQL filesort and avoid any
371 // difference in the sorting behavior of SQL back-ends.
372 usort($routes, [$this, 'routeProviderRouteCompare']);
374 foreach ($routes as $row) {
375 $collection->add($row['name'], unserialize($row['route']));
382 * Comparison function for usort on routes.
384 protected function routeProviderRouteCompare(array $a, array $b) {
385 if ($a['fit'] == $b['fit']) {
386 return strcmp($a['name'], $b['name']);
388 // Reverse sort from highest to lowest fit. PHP should cast to int, but
389 // the explicit cast makes this sort more robust against unexpected input.
390 return (int) $a['fit'] < (int) $b['fit'] ? 1 : -1;
396 public function getAllRoutes() {
397 return new PagedRouteCollection($this);
403 public function reset() {
405 $this->serializedRoutes = [];
406 $this->cacheTagInvalidator->invalidateTags(['routes']);
412 public static function getSubscribedEvents() {
413 $events[RoutingEvents::FINISHED][] = ['reset'];
420 public function getRoutesPaged($offset, $length = NULL) {
421 $select = $this->connection->select($this->tableName, 'router')
422 ->fields('router', ['name', 'route']);
424 if (isset($length)) {
425 $select->range($offset, $length);
428 $routes = $select->execute()->fetchAllKeyed();
431 foreach ($routes as $name => $route) {
432 $result[$name] = unserialize($route);
441 public function getRoutesCount() {
442 return $this->connection->query("SELECT COUNT(*) FROM {" . $this->connection->escapeTable($this->tableName) . "}")->fetchField();
446 * Returns the cache ID for the route collection cache.
448 * @param \Symfony\Component\HttpFoundation\Request $request
449 * The request object.
454 protected function getRouteCollectionCacheId(Request $request) {
455 // Include the current language code in the cache identifier as
456 // the language information can be elsewhere than in the path, for example
457 // based on the domain.
458 $language_part = $this->getCurrentLanguageCacheIdPart();
459 return 'route:' . $language_part . ':' . $request->getPathInfo() . ':' . $request->getQueryString();
463 * Returns the language identifier for the route collection cache.
466 * The language identifier.
468 protected function getCurrentLanguageCacheIdPart() {
469 // This must be in sync with the language logic in
470 // \Drupal\Core\PathProcessor\PathProcessorAlias::processInbound() and
471 // \Drupal\Core\Path\AliasManager::getPathByAlias().
472 // @todo Update this if necessary in https://www.drupal.org/node/1125428.
473 return $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId();