RouteProvider.php 16 KB

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