Version 1
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / SpecialAttributesRouteSubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Drupal\Core\Routing\RouteBuildEvent;
6 use Drupal\Core\Routing\RouteSubscriberBase;
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8 use Symfony\Component\Routing\RouteCollection;
9
10 /**
11  * Provides a route subscriber which checks for invalid pattern variables.
12  */
13 class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function alterRoutes(RouteCollection $collection) {
19     $special_variables = [
20       'system_path',
21       '_legacy',
22       '_raw_variables',
23       RouteObjectInterface::ROUTE_OBJECT,
24       RouteObjectInterface::ROUTE_NAME,
25       '_content',
26       '_controller',
27       '_form',
28     ];
29     foreach ($collection->all() as $name => $route) {
30       if ($not_allowed_variables = array_intersect($route->compile()->getVariables(), $special_variables)) {
31         $reserved = implode(', ', $not_allowed_variables);
32         trigger_error(sprintf('Route %s uses reserved variable names: %s', $name, $reserved), E_USER_WARNING);
33       }
34     }
35   }
36
37   /**
38    * Delegates the route altering to self::alterRoutes().
39    *
40    * @param \Drupal\Core\Routing\RouteBuildEvent $event
41    *   The route build event.
42    */
43   public function onAlterRoutes(RouteBuildEvent $event) {
44     $collection = $event->getRouteCollection();
45     $this->alterRoutes($collection);
46   }
47
48 }