PageRenderTime 78ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/core/lib/Drupal/Core/Routing/RouteProvider.php

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