annotate core/lib/Drupal/Core/Routing/RouteProvider.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 7a779792577d
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Routing;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Unicode;
Chris@0 6 use Drupal\Core\Cache\Cache;
Chris@0 7 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 8 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
Chris@14 9 use Drupal\Core\Language\LanguageInterface;
Chris@14 10 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 11 use Drupal\Core\Path\CurrentPathStack;
Chris@0 12 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
Chris@0 13 use Drupal\Core\State\StateInterface;
Chris@0 14 use Symfony\Cmf\Component\Routing\PagedRouteCollection;
Chris@0 15 use Symfony\Cmf\Component\Routing\PagedRouteProviderInterface;
Chris@0 16 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 17 use Symfony\Component\HttpFoundation\Request;
Chris@0 18 use Symfony\Component\Routing\Exception\RouteNotFoundException;
Chris@0 19 use Symfony\Component\Routing\RouteCollection;
Chris@0 20 use Drupal\Core\Database\Connection;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * A Route Provider front-end for all Drupal-stored routes.
Chris@0 24 */
Chris@0 25 class RouteProvider implements PreloadableRouteProviderInterface, PagedRouteProviderInterface, EventSubscriberInterface {
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The database connection from which to read route information.
Chris@0 29 *
Chris@0 30 * @var \Drupal\Core\Database\Connection
Chris@0 31 */
Chris@0 32 protected $connection;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * The name of the SQL table from which to read the routes.
Chris@0 36 *
Chris@0 37 * @var string
Chris@0 38 */
Chris@0 39 protected $tableName;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The state.
Chris@0 43 *
Chris@0 44 * @var \Drupal\Core\State\StateInterface
Chris@0 45 */
Chris@0 46 protected $state;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * A cache of already-loaded routes, keyed by route name.
Chris@0 50 *
Chris@0 51 * @var \Symfony\Component\Routing\Route[]
Chris@0 52 */
Chris@0 53 protected $routes = [];
Chris@0 54
Chris@0 55 /**
Chris@0 56 * A cache of already-loaded serialized routes, keyed by route name.
Chris@0 57 *
Chris@0 58 * @var string[]
Chris@0 59 */
Chris@0 60 protected $serializedRoutes = [];
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The current path.
Chris@0 64 *
Chris@0 65 * @var \Drupal\Core\Path\CurrentPathStack
Chris@0 66 */
Chris@0 67 protected $currentPath;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * The cache backend.
Chris@0 71 *
Chris@0 72 * @var \Drupal\Core\Cache\CacheBackendInterface
Chris@0 73 */
Chris@0 74 protected $cache;
Chris@0 75
Chris@0 76 /**
Chris@0 77 * The cache tag invalidator.
Chris@0 78 *
Chris@0 79 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
Chris@0 80 */
Chris@0 81 protected $cacheTagInvalidator;
Chris@0 82
Chris@0 83 /**
Chris@0 84 * A path processor manager for resolving the system path.
Chris@0 85 *
Chris@0 86 * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
Chris@0 87 */
Chris@0 88 protected $pathProcessor;
Chris@0 89
Chris@0 90 /**
Chris@14 91 * The language manager.
Chris@14 92 *
Chris@14 93 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@14 94 */
Chris@14 95 protected $languageManager;
Chris@14 96
Chris@14 97 /**
Chris@0 98 * Cache ID prefix used to load routes.
Chris@0 99 */
Chris@0 100 const ROUTE_LOAD_CID_PREFIX = 'route_provider.route_load:';
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Constructs a new PathMatcher.
Chris@0 104 *
Chris@0 105 * @param \Drupal\Core\Database\Connection $connection
Chris@0 106 * A database connection object.
Chris@0 107 * @param \Drupal\Core\State\StateInterface $state
Chris@0 108 * The state.
Chris@0 109 * @param \Drupal\Core\Path\CurrentPathStack $current_path
Chris@0 110 * The current path.
Chris@0 111 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
Chris@0 112 * The cache backend.
Chris@0 113 * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $path_processor
Chris@0 114 * The path processor.
Chris@0 115 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tag_invalidator
Chris@0 116 * The cache tag invalidator.
Chris@0 117 * @param string $table
Chris@0 118 * (Optional) The table in the database to use for matching. Defaults to 'router'
Chris@14 119 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@14 120 * (Optional) The language manager.
Chris@0 121 */
Chris@14 122 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) {
Chris@0 123 $this->connection = $connection;
Chris@0 124 $this->state = $state;
Chris@0 125 $this->currentPath = $current_path;
Chris@0 126 $this->cache = $cache_backend;
Chris@0 127 $this->cacheTagInvalidator = $cache_tag_invalidator;
Chris@0 128 $this->pathProcessor = $path_processor;
Chris@0 129 $this->tableName = $table;
Chris@14 130 $this->languageManager = $language_manager ?: \Drupal::languageManager();
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * Finds routes that may potentially match the request.
Chris@0 135 *
Chris@0 136 * This may return a mixed list of class instances, but all routes returned
Chris@0 137 * must extend the core symfony route. The classes may also implement
Chris@0 138 * RouteObjectInterface to link to a content document.
Chris@0 139 *
Chris@0 140 * This method may not throw an exception based on implementation specific
Chris@0 141 * restrictions on the url. That case is considered a not found - returning
Chris@0 142 * an empty array. Exceptions are only used to abort the whole request in
Chris@0 143 * case something is seriously broken, like the storage backend being down.
Chris@0 144 *
Chris@0 145 * Note that implementations may not implement an optimal matching
Chris@0 146 * algorithm, simply a reasonable first pass. That allows for potentially
Chris@0 147 * very large route sets to be filtered down to likely candidates, which
Chris@0 148 * may then be filtered in memory more completely.
Chris@0 149 *
Chris@12 150 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 151 * A request against which to match.
Chris@0 152 *
Chris@0 153 * @return \Symfony\Component\Routing\RouteCollection
Chris@0 154 * RouteCollection with all urls that could potentially match $request.
Chris@0 155 * Empty collection if nothing can match. The collection will be sorted from
Chris@0 156 * highest to lowest fit (match of path parts) and then in ascending order
Chris@0 157 * by route name for routes with the same fit.
Chris@0 158 */
Chris@0 159 public function getRouteCollectionForRequest(Request $request) {
Chris@0 160 // Cache both the system path as well as route parameters and matching
Chris@0 161 // routes.
Chris@14 162 $cid = $this->getRouteCollectionCacheId($request);
Chris@0 163 if ($cached = $this->cache->get($cid)) {
Chris@0 164 $this->currentPath->setPath($cached->data['path'], $request);
Chris@0 165 $request->query->replace($cached->data['query']);
Chris@0 166 return $cached->data['routes'];
Chris@0 167 }
Chris@0 168 else {
Chris@0 169 // Just trim on the right side.
Chris@0 170 $path = $request->getPathInfo();
Chris@0 171 $path = $path === '/' ? $path : rtrim($request->getPathInfo(), '/');
Chris@0 172 $path = $this->pathProcessor->processInbound($path, $request);
Chris@0 173 $this->currentPath->setPath($path, $request);
Chris@0 174 // Incoming path processors may also set query parameters.
Chris@0 175 $query_parameters = $request->query->all();
Chris@0 176 $routes = $this->getRoutesByPath(rtrim($path, '/'));
Chris@0 177 $cache_value = [
Chris@0 178 'path' => $path,
Chris@0 179 'query' => $query_parameters,
Chris@0 180 'routes' => $routes,
Chris@0 181 ];
Chris@0 182 $this->cache->set($cid, $cache_value, CacheBackendInterface::CACHE_PERMANENT, ['route_match']);
Chris@0 183 return $routes;
Chris@0 184 }
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * Find the route using the provided route name (and parameters).
Chris@0 189 *
Chris@0 190 * @param string $name
Chris@0 191 * The route name to fetch
Chris@0 192 *
Chris@0 193 * @return \Symfony\Component\Routing\Route
Chris@0 194 * The found route.
Chris@0 195 *
Chris@0 196 * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
Chris@0 197 * Thrown if there is no route with that name in this repository.
Chris@0 198 */
Chris@0 199 public function getRouteByName($name) {
Chris@0 200 $routes = $this->getRoutesByNames([$name]);
Chris@0 201 if (empty($routes)) {
Chris@0 202 throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
Chris@0 203 }
Chris@0 204
Chris@0 205 return reset($routes);
Chris@0 206 }
Chris@0 207
Chris@0 208 /**
Chris@0 209 * {@inheritdoc}
Chris@0 210 */
Chris@0 211 public function preLoadRoutes($names) {
Chris@0 212 if (empty($names)) {
Chris@0 213 throw new \InvalidArgumentException('You must specify the route names to load');
Chris@0 214 }
Chris@0 215
Chris@0 216 $routes_to_load = array_diff($names, array_keys($this->routes), array_keys($this->serializedRoutes));
Chris@0 217 if ($routes_to_load) {
Chris@0 218
Chris@0 219 $cid = static::ROUTE_LOAD_CID_PREFIX . hash('sha512', serialize($routes_to_load));
Chris@0 220 if ($cache = $this->cache->get($cid)) {
Chris@0 221 $routes = $cache->data;
Chris@0 222 }
Chris@0 223 else {
Chris@0 224 try {
Chris@0 225 $result = $this->connection->query('SELECT name, route FROM {' . $this->connection->escapeTable($this->tableName) . '} WHERE name IN ( :names[] )', [':names[]' => $routes_to_load]);
Chris@0 226 $routes = $result->fetchAllKeyed();
Chris@0 227
Chris@0 228 $this->cache->set($cid, $routes, Cache::PERMANENT, ['routes']);
Chris@0 229 }
Chris@0 230 catch (\Exception $e) {
Chris@0 231 $routes = [];
Chris@0 232 }
Chris@0 233 }
Chris@0 234
Chris@0 235 $this->serializedRoutes += $routes;
Chris@0 236 }
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * {@inheritdoc}
Chris@0 241 */
Chris@0 242 public function getRoutesByNames($names) {
Chris@0 243 $this->preLoadRoutes($names);
Chris@0 244
Chris@0 245 foreach ($names as $name) {
Chris@0 246 // The specified route name might not exist or might be serialized.
Chris@0 247 if (!isset($this->routes[$name]) && isset($this->serializedRoutes[$name])) {
Chris@0 248 $this->routes[$name] = unserialize($this->serializedRoutes[$name]);
Chris@0 249 unset($this->serializedRoutes[$name]);
Chris@0 250 }
Chris@0 251 }
Chris@0 252
Chris@0 253 return array_intersect_key($this->routes, array_flip($names));
Chris@0 254 }
Chris@0 255
Chris@0 256 /**
Chris@0 257 * Returns an array of path pattern outlines that could match the path parts.
Chris@0 258 *
Chris@0 259 * @param array $parts
Chris@0 260 * The parts of the path for which we want candidates.
Chris@0 261 *
Chris@0 262 * @return array
Chris@0 263 * An array of outlines that could match the specified path parts.
Chris@0 264 */
Chris@0 265 protected function getCandidateOutlines(array $parts) {
Chris@0 266 $number_parts = count($parts);
Chris@0 267 $ancestors = [];
Chris@0 268 $length = $number_parts - 1;
Chris@0 269 $end = (1 << $number_parts) - 1;
Chris@0 270
Chris@0 271 // The highest possible mask is a 1 bit for every part of the path. We will
Chris@0 272 // check every value down from there to generate a possible outline.
Chris@0 273 if ($number_parts == 1) {
Chris@0 274 $masks = [1];
Chris@0 275 }
Chris@0 276 elseif ($number_parts <= 3 && $number_parts > 0) {
Chris@0 277 // Optimization - don't query the state system for short paths. This also
Chris@0 278 // insulates against the state entry for masks going missing for common
Chris@0 279 // user-facing paths since we generate all values without checking state.
Chris@0 280 $masks = range($end, 1);
Chris@0 281 }
Chris@0 282 elseif ($number_parts <= 0) {
Chris@0 283 // No path can match, short-circuit the process.
Chris@0 284 $masks = [];
Chris@0 285 }
Chris@0 286 else {
Chris@0 287 // Get the actual patterns that exist out of state.
Chris@0 288 $masks = (array) $this->state->get('routing.menu_masks.' . $this->tableName, []);
Chris@0 289 }
Chris@0 290
Chris@0 291 // Only examine patterns that actually exist as router items (the masks).
Chris@0 292 foreach ($masks as $i) {
Chris@0 293 if ($i > $end) {
Chris@0 294 // Only look at masks that are not longer than the path of interest.
Chris@0 295 continue;
Chris@0 296 }
Chris@0 297 elseif ($i < (1 << $length)) {
Chris@0 298 // We have exhausted the masks of a given length, so decrease the length.
Chris@0 299 --$length;
Chris@0 300 }
Chris@0 301 $current = '';
Chris@0 302 for ($j = $length; $j >= 0; $j--) {
Chris@0 303 // Check the bit on the $j offset.
Chris@0 304 if ($i & (1 << $j)) {
Chris@0 305 // Bit one means the original value.
Chris@0 306 $current .= $parts[$length - $j];
Chris@0 307 }
Chris@0 308 else {
Chris@0 309 // Bit zero means means wildcard.
Chris@0 310 $current .= '%';
Chris@0 311 }
Chris@0 312 // Unless we are at offset 0, add a slash.
Chris@0 313 if ($j) {
Chris@0 314 $current .= '/';
Chris@0 315 }
Chris@0 316 }
Chris@0 317 $ancestors[] = '/' . $current;
Chris@0 318 }
Chris@0 319 return $ancestors;
Chris@0 320 }
Chris@0 321
Chris@0 322 /**
Chris@0 323 * {@inheritdoc}
Chris@0 324 */
Chris@0 325 public function getRoutesByPattern($pattern) {
Chris@0 326 $path = RouteCompiler::getPatternOutline($pattern);
Chris@0 327
Chris@0 328 return $this->getRoutesByPath($path);
Chris@0 329 }
Chris@0 330
Chris@0 331 /**
Chris@0 332 * Get all routes which match a certain pattern.
Chris@0 333 *
Chris@0 334 * @param string $path
Chris@0 335 * The route pattern to search for.
Chris@0 336 *
Chris@0 337 * @return \Symfony\Component\Routing\RouteCollection
Chris@0 338 * Returns a route collection of matching routes. The collection may be
Chris@0 339 * empty and will be sorted from highest to lowest fit (match of path parts)
Chris@0 340 * and then in ascending order by route name for routes with the same fit.
Chris@0 341 */
Chris@0 342 protected function getRoutesByPath($path) {
Chris@0 343 // Split the path up on the slashes, ignoring multiple slashes in a row
Chris@0 344 // or leading or trailing slashes. Convert to lower case here so we can
Chris@0 345 // have a case-insensitive match from the incoming path to the lower case
Chris@0 346 // pattern outlines from \Drupal\Core\Routing\RouteCompiler::compile().
Chris@0 347 // @see \Drupal\Core\Routing\CompiledRoute::__construct()
Chris@0 348 $parts = preg_split('@/+@', Unicode::strtolower($path), NULL, PREG_SPLIT_NO_EMPTY);
Chris@0 349
Chris@0 350 $collection = new RouteCollection();
Chris@0 351
Chris@0 352 $ancestors = $this->getCandidateOutlines($parts);
Chris@0 353 if (empty($ancestors)) {
Chris@0 354 return $collection;
Chris@0 355 }
Chris@0 356
Chris@0 357 // The >= check on number_parts allows us to match routes with optional
Chris@0 358 // trailing wildcard parts as long as the pattern matches, since we
Chris@0 359 // dump the route pattern without those optional parts.
Chris@0 360 try {
Chris@0 361 $routes = $this->connection->query("SELECT name, route, fit FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN ( :patterns[] ) AND number_parts >= :count_parts", [
Chris@0 362 ':patterns[]' => $ancestors,
Chris@0 363 ':count_parts' => count($parts),
Chris@0 364 ])
Chris@0 365 ->fetchAll(\PDO::FETCH_ASSOC);
Chris@0 366 }
Chris@0 367 catch (\Exception $e) {
Chris@0 368 $routes = [];
Chris@0 369 }
Chris@0 370
Chris@0 371 // We sort by fit and name in PHP to avoid a SQL filesort and avoid any
Chris@0 372 // difference in the sorting behavior of SQL back-ends.
Chris@0 373 usort($routes, [$this, 'routeProviderRouteCompare']);
Chris@0 374
Chris@0 375 foreach ($routes as $row) {
Chris@0 376 $collection->add($row['name'], unserialize($row['route']));
Chris@0 377 }
Chris@0 378
Chris@0 379 return $collection;
Chris@0 380 }
Chris@0 381
Chris@0 382 /**
Chris@0 383 * Comparison function for usort on routes.
Chris@0 384 */
Chris@0 385 protected function routeProviderRouteCompare(array $a, array $b) {
Chris@0 386 if ($a['fit'] == $b['fit']) {
Chris@0 387 return strcmp($a['name'], $b['name']);
Chris@0 388 }
Chris@0 389 // Reverse sort from highest to lowest fit. PHP should cast to int, but
Chris@0 390 // the explicit cast makes this sort more robust against unexpected input.
Chris@0 391 return (int) $a['fit'] < (int) $b['fit'] ? 1 : -1;
Chris@0 392 }
Chris@0 393
Chris@0 394 /**
Chris@0 395 * {@inheritdoc}
Chris@0 396 */
Chris@0 397 public function getAllRoutes() {
Chris@0 398 return new PagedRouteCollection($this);
Chris@0 399 }
Chris@0 400
Chris@0 401 /**
Chris@0 402 * {@inheritdoc}
Chris@0 403 */
Chris@0 404 public function reset() {
Chris@0 405 $this->routes = [];
Chris@0 406 $this->serializedRoutes = [];
Chris@0 407 $this->cacheTagInvalidator->invalidateTags(['routes']);
Chris@0 408 }
Chris@0 409
Chris@0 410 /**
Chris@0 411 * {@inheritdoc}
Chris@0 412 */
Chris@0 413 public static function getSubscribedEvents() {
Chris@0 414 $events[RoutingEvents::FINISHED][] = ['reset'];
Chris@0 415 return $events;
Chris@0 416 }
Chris@0 417
Chris@0 418 /**
Chris@0 419 * {@inheritdoc}
Chris@0 420 */
Chris@0 421 public function getRoutesPaged($offset, $length = NULL) {
Chris@0 422 $select = $this->connection->select($this->tableName, 'router')
Chris@0 423 ->fields('router', ['name', 'route']);
Chris@0 424
Chris@0 425 if (isset($length)) {
Chris@0 426 $select->range($offset, $length);
Chris@0 427 }
Chris@0 428
Chris@0 429 $routes = $select->execute()->fetchAllKeyed();
Chris@0 430
Chris@0 431 $result = [];
Chris@0 432 foreach ($routes as $name => $route) {
Chris@0 433 $result[$name] = unserialize($route);
Chris@0 434 }
Chris@0 435
Chris@0 436 return $result;
Chris@0 437 }
Chris@0 438
Chris@0 439 /**
Chris@0 440 * {@inheritdoc}
Chris@0 441 */
Chris@0 442 public function getRoutesCount() {
Chris@0 443 return $this->connection->query("SELECT COUNT(*) FROM {" . $this->connection->escapeTable($this->tableName) . "}")->fetchField();
Chris@0 444 }
Chris@0 445
Chris@14 446 /**
Chris@14 447 * Returns the cache ID for the route collection cache.
Chris@14 448 *
Chris@14 449 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@14 450 * The request object.
Chris@14 451 *
Chris@14 452 * @return string
Chris@14 453 * The cache ID.
Chris@14 454 */
Chris@14 455 protected function getRouteCollectionCacheId(Request $request) {
Chris@14 456 // Include the current language code in the cache identifier as
Chris@14 457 // the language information can be elsewhere than in the path, for example
Chris@14 458 // based on the domain.
Chris@14 459 $language_part = $this->getCurrentLanguageCacheIdPart();
Chris@14 460 return 'route:' . $language_part . ':' . $request->getPathInfo() . ':' . $request->getQueryString();
Chris@14 461 }
Chris@14 462
Chris@14 463 /**
Chris@14 464 * Returns the language identifier for the route collection cache.
Chris@14 465 *
Chris@14 466 * @return string
Chris@14 467 * The language identifier.
Chris@14 468 */
Chris@14 469 protected function getCurrentLanguageCacheIdPart() {
Chris@14 470 // This must be in sync with the language logic in
Chris@14 471 // \Drupal\Core\PathProcessor\PathProcessorAlias::processInbound() and
Chris@14 472 // \Drupal\Core\Path\AliasManager::getPathByAlias().
Chris@14 473 // @todo Update this if necessary in https://www.drupal.org/node/1125428.
Chris@14 474 return $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId();
Chris@14 475 }
Chris@14 476
Chris@0 477 }