d31f293e18c411c660c4c96989e0fef7625f66c9
[yaffs-website] / web / core / lib / Drupal / Core / Routing / RouteProvider.php
1 <?php
2
3 namespace Drupal\Core\Routing;
4
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;
20
21 /**
22  * A Route Provider front-end for all Drupal-stored routes.
23  */
24 class RouteProvider implements PreloadableRouteProviderInterface, PagedRouteProviderInterface, EventSubscriberInterface {
25
26   /**
27    * The database connection from which to read route information.
28    *
29    * @var \Drupal\Core\Database\Connection
30    */
31   protected $connection;
32
33   /**
34    * The name of the SQL table from which to read the routes.
35    *
36    * @var string
37    */
38   protected $tableName;
39
40   /**
41    * The state.
42    *
43    * @var \Drupal\Core\State\StateInterface
44    */
45   protected $state;
46
47   /**
48    * A cache of already-loaded routes, keyed by route name.
49    *
50    * @var \Symfony\Component\Routing\Route[]
51    */
52   protected $routes = [];
53
54   /**
55    * A cache of already-loaded serialized routes, keyed by route name.
56    *
57    * @var string[]
58    */
59   protected $serializedRoutes = [];
60
61   /**
62    * The current path.
63    *
64    * @var \Drupal\Core\Path\CurrentPathStack
65    */
66   protected $currentPath;
67
68   /**
69    * The cache backend.
70    *
71    * @var \Drupal\Core\Cache\CacheBackendInterface
72    */
73   protected $cache;
74
75   /**
76    * The cache tag invalidator.
77    *
78    * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
79    */
80   protected $cacheTagInvalidator;
81
82   /**
83    * A path processor manager for resolving the system path.
84    *
85    * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
86    */
87   protected $pathProcessor;
88
89   /**
90    * The language manager.
91    *
92    * @var \Drupal\Core\Language\LanguageManagerInterface
93    */
94   protected $languageManager;
95
96   /**
97    * Cache ID prefix used to load routes.
98    */
99   const ROUTE_LOAD_CID_PREFIX = 'route_provider.route_load:';
100
101   /**
102    * Constructs a new PathMatcher.
103    *
104    * @param \Drupal\Core\Database\Connection $connection
105    *   A database connection object.
106    * @param \Drupal\Core\State\StateInterface $state
107    *   The state.
108    * @param \Drupal\Core\Path\CurrentPathStack $current_path
109    *   The current path.
110    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
111    *   The 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.
120    */
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();
130   }
131
132   /**
133    * Finds routes that may potentially match the request.
134    *
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.
138    *
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.
143    *
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.
148    *
149    * @param \Symfony\Component\HttpFoundation\Request $request
150    *   A request against which to match.
151    *
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.
157    */
158   public function getRouteCollectionForRequest(Request $request) {
159     // Cache both the system path as well as route parameters and matching
160     // routes.
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'];
166     }
167     else {
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, '/'));
176       $cache_value = [
177         'path' => $path,
178         'query' => $query_parameters,
179         'routes' => $routes,
180       ];
181       $this->cache->set($cid, $cache_value, CacheBackendInterface::CACHE_PERMANENT, ['route_match']);
182       return $routes;
183     }
184   }
185
186   /**
187    * Find the route using the provided route name (and parameters).
188    *
189    * @param string $name
190    *   The route name to fetch
191    *
192    * @return \Symfony\Component\Routing\Route
193    *   The found route.
194    *
195    * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
196    *   Thrown if there is no route with that name in this repository.
197    */
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));
202     }
203
204     return reset($routes);
205   }
206
207   /**
208    * {@inheritdoc}
209    */
210   public function preLoadRoutes($names) {
211     if (empty($names)) {
212       throw new \InvalidArgumentException('You must specify the route names to load');
213     }
214
215     $routes_to_load = array_diff($names, array_keys($this->routes), array_keys($this->serializedRoutes));
216     if ($routes_to_load) {
217
218       $cid = static::ROUTE_LOAD_CID_PREFIX . hash('sha512', serialize($routes_to_load));
219       if ($cache = $this->cache->get($cid)) {
220         $routes = $cache->data;
221       }
222       else {
223         try {
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();
226
227           $this->cache->set($cid, $routes, Cache::PERMANENT, ['routes']);
228         }
229         catch (\Exception $e) {
230           $routes = [];
231         }
232       }
233
234       $this->serializedRoutes += $routes;
235     }
236   }
237
238   /**
239    * {@inheritdoc}
240    */
241   public function getRoutesByNames($names) {
242     $this->preLoadRoutes($names);
243
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]);
249       }
250     }
251
252     return array_intersect_key($this->routes, array_flip($names));
253   }
254
255   /**
256    * Returns an array of path pattern outlines that could match the path parts.
257    *
258    * @param array $parts
259    *   The parts of the path for which we want candidates.
260    *
261    * @return array
262    *   An array of outlines that could match the specified path parts.
263    */
264   protected function getCandidateOutlines(array $parts) {
265     $number_parts = count($parts);
266     $ancestors = [];
267     $length = $number_parts - 1;
268     $end = (1 << $number_parts) - 1;
269
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) {
273       $masks = [1];
274     }
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);
280     }
281     elseif ($number_parts <= 0) {
282       // No path can match, short-circuit the process.
283       $masks = [];
284     }
285     else {
286       // Get the actual patterns that exist out of state.
287       $masks = (array) $this->state->get('routing.menu_masks.' . $this->tableName, []);
288     }
289
290     // Only examine patterns that actually exist as router items (the masks).
291     foreach ($masks as $i) {
292       if ($i > $end) {
293         // Only look at masks that are not longer than the path of interest.
294         continue;
295       }
296       elseif ($i < (1 << $length)) {
297         // We have exhausted the masks of a given length, so decrease the length.
298         --$length;
299       }
300       $current = '';
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];
306         }
307         else {
308           // Bit zero means means wildcard.
309           $current .= '%';
310         }
311         // Unless we are at offset 0, add a slash.
312         if ($j) {
313           $current .= '/';
314         }
315       }
316       $ancestors[] = '/' . $current;
317     }
318     return $ancestors;
319   }
320
321   /**
322    * {@inheritdoc}
323    */
324   public function getRoutesByPattern($pattern) {
325     $path = RouteCompiler::getPatternOutline($pattern);
326
327     return $this->getRoutesByPath($path);
328   }
329
330   /**
331    * Get all routes which match a certain pattern.
332    *
333    * @param string $path
334    *   The route pattern to search for.
335    *
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.
340    */
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);
348
349     $collection = new RouteCollection();
350
351     $ancestors = $this->getCandidateOutlines($parts);
352     if (empty($ancestors)) {
353       return $collection;
354     }
355
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.
359     try {
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),
363       ])
364         ->fetchAll(\PDO::FETCH_ASSOC);
365     }
366     catch (\Exception $e) {
367       $routes = [];
368     }
369
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']);
373
374     foreach ($routes as $row) {
375       $collection->add($row['name'], unserialize($row['route']));
376     }
377
378     return $collection;
379   }
380
381   /**
382    * Comparison function for usort on routes.
383    */
384   protected function routeProviderRouteCompare(array $a, array $b) {
385     if ($a['fit'] == $b['fit']) {
386       return strcmp($a['name'], $b['name']);
387     }
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;
391   }
392
393   /**
394    * {@inheritdoc}
395    */
396   public function getAllRoutes() {
397     return new PagedRouteCollection($this);
398   }
399
400   /**
401    * {@inheritdoc}
402    */
403   public function reset() {
404     $this->routes = [];
405     $this->serializedRoutes = [];
406     $this->cacheTagInvalidator->invalidateTags(['routes']);
407   }
408
409   /**
410    * {@inheritdoc}
411    */
412   public static function getSubscribedEvents() {
413     $events[RoutingEvents::FINISHED][] = ['reset'];
414     return $events;
415   }
416
417   /**
418    * {@inheritdoc}
419    */
420   public function getRoutesPaged($offset, $length = NULL) {
421     $select = $this->connection->select($this->tableName, 'router')
422       ->fields('router', ['name', 'route']);
423
424     if (isset($length)) {
425       $select->range($offset, $length);
426     }
427
428     $routes = $select->execute()->fetchAllKeyed();
429
430     $result = [];
431     foreach ($routes as $name => $route) {
432       $result[$name] = unserialize($route);
433     }
434
435     return $result;
436   }
437
438   /**
439    * {@inheritdoc}
440    */
441   public function getRoutesCount() {
442     return $this->connection->query("SELECT COUNT(*) FROM {" . $this->connection->escapeTable($this->tableName) . "}")->fetchField();
443   }
444
445   /**
446    * Returns the cache ID for the route collection cache.
447    *
448    * @param \Symfony\Component\HttpFoundation\Request $request
449    *   The request object.
450    *
451    * @return string
452    *   The cache ID.
453    */
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();
460   }
461
462   /**
463    * Returns the language identifier for the route collection cache.
464    *
465    * @return string
466    *   The language identifier.
467    */
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();
474   }
475
476 }