annotate core/lib/Drupal/Core/Routing/RouteProvider.php @ 19:fa3358dc1485 tip

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