SpecialAttributesRouteSubscriber.php 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber.
 */

namespace Drupal\Core\EventSubscriber;

use Drupal\Component\Utility\String;
use Drupal\Core\Routing\RouteBuildEvent;
12
use Drupal\Core\Routing\RouteSubscriberBase;
13
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
14
use Symfony\Component\Routing\RouteCollection;
15 16 17 18

/**
 * Provides a route subscriber which checks for invalid pattern variables.
 */
19
class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
20 21

  /**
22
   * {@inheritdoc}
23
   */
24
  protected function alterRoutes(RouteCollection $collection) {
25 26 27 28 29 30 31 32 33 34 35
    $special_variables = array(
      'system_path',
      '_maintenance',
      '_legacy',
      '_authentication_provider',
      '_raw_variables',
      RouteObjectInterface::ROUTE_OBJECT,
      RouteObjectInterface::ROUTE_NAME,
      '_content',
      '_form',
    );
36
    foreach ($collection->all() as $name => $route) {
37
      if ($not_allowed_variables = array_intersect($route->compile()->getVariables(), $special_variables)) {
38 39
        $reserved = implode(', ', $not_allowed_variables);
        trigger_error(sprintf('Route %s uses reserved variable names: %s', $name, $reserved), E_USER_WARNING);
40 41 42 43 44
      }
    }
  }

  /**
45 46 47 48 49 50
   * Delegates the route altering to self::alterRoutes().
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route build event.
   *
   * @return bool
51 52
   *   Returns TRUE if the variables were successfully replaced, otherwise
   *   FALSE.
53
   */
54 55
  public function onAlterRoutes(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
56
    return $this->alterRoutes($collection);
57 58 59
  }

}