You are here

class RouteCompiler in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/routing/RouteCompiler.php \Symfony\Component\Routing\RouteCompiler
  2. 8 core/lib/Drupal/Core/Routing/RouteCompiler.php \Drupal\Core\Routing\RouteCompiler
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Routing/RouteCompiler.php \Drupal\Core\Routing\RouteCompiler

Compiler to generate derived information from a Route necessary for matching.

Hierarchy

Expanded class hierarchy of RouteCompiler

2 files declare their use of RouteCompiler
PathPluginBase.php in core/modules/views/src/Plugin/views/display/PathPluginBase.php
Contains \Drupal\views\Plugin\views\display\PathPluginBase.
RouteCompilerTest.php in core/tests/Drupal/Tests/Core/Routing/RouteCompilerTest.php
Contains \Drupal\Tests\Core\Routing\RouteCompilerTest.
11 string references to 'RouteCompiler'
legacy_validpattern.yml in vendor/symfony/routing/Tests/Fixtures/legacy_validpattern.yml
vendor/symfony/routing/Tests/Fixtures/legacy_validpattern.yml
PhpFileLoaderTest::testLoadWithImport in vendor/symfony/routing/Tests/Loader/PhpFileLoaderTest.php
PhpFileLoaderTest::testLoadWithRoute in vendor/symfony/routing/Tests/Loader/PhpFileLoaderTest.php
RouteTest::getValidParameters in vendor/symfony/routing/Tests/Annotation/RouteTest.php
validpattern.php in vendor/symfony/routing/Tests/Fixtures/validpattern.php

... See full list

File

core/lib/Drupal/Core/Routing/RouteCompiler.php, line 17
Contains \Drupal\Core\Routing\RouteCompiler.

Namespace

Drupal\Core\Routing
View source
class RouteCompiler extends SymfonyRouteCompiler implements RouteCompilerInterface {

  /**
   * Utility constant to use for regular expressions against the path.
   */
  const REGEX_DELIMITER = '#';

  /**
   * Compiles the current route instance.
   *
   * Because so much of the parent class is private, we need to call the parent
   * class's compile() method and then dissect its return value to build our
   * new compiled object.  If upstream gets refactored so we can subclass more
   * easily then this may not be necessary.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   A Route instance.
   *
   * @return \Drupal\Core\Routing\CompiledRoute
   *   A CompiledRoute instance.
   */
  public static function compile(Route $route) {
    $symfony_compiled = parent::compile($route);

    // The Drupal-specific compiled information.
    $stripped_path = static::getPathWithoutDefaults($route);
    $fit = static::getFit($stripped_path);
    $pattern_outline = static::getPatternOutline($stripped_path);

    // We count the number of parts including any optional trailing parts. This
    // allows the RouteProvider to filter candidate routes more efficiently.
    $num_parts = count(explode('/', trim($route
      ->getPath(), '/')));
    return new CompiledRoute($fit, $pattern_outline, $num_parts, $symfony_compiled
      ->getStaticPrefix(), $symfony_compiled
      ->getRegex(), $symfony_compiled
      ->getTokens(), $symfony_compiled
      ->getPathVariables(), $symfony_compiled
      ->getHostRegex(), $symfony_compiled
      ->getHostTokens(), $symfony_compiled
      ->getHostVariables(), $symfony_compiled
      ->getVariables());
  }

  /**
   * Returns the pattern outline.
   *
   * The pattern outline is the path pattern but normalized so that all
   * placeholders are equal strings and default values are removed.
   *
   * @param string $path
   *   The path for which we want the normalized outline.
   *
   * @return string
   *   The path pattern outline.
   */
  public static function getPatternOutline($path) {
    return preg_replace('#\\{\\w+\\}#', '%', $path);
  }

  /**
   * Determines the fitness of the provided path.
   *
   * @param string $path
   *   The path whose fitness we want.
   *
   * @return int
   *   The fitness of the path, as an integer.
   */
  public static function getFit($path) {
    $parts = explode('/', trim($path, '/'));
    $number_parts = count($parts);

    // We store the highest index of parts here to save some work in the fit
    // calculation loop.
    $slashes = $number_parts - 1;

    // The fit value is a binary number which has 1 at every fixed path
    // position and 0 where there is a wildcard. We keep track of all such
    // patterns that exist so that we can minimize the number of path
    // patterns we need to check in the RouteProvider.
    $fit = 0;
    foreach ($parts as $k => $part) {
      if (strpos($part, '{') === FALSE) {
        $fit |= 1 << $slashes - $k;
      }
    }
    return $fit;
  }

  /**
   * Returns the path of the route, without placeholders with a default value.
   *
   * When computing the path outline and fit, we want to skip default-value
   * placeholders.  If we didn't, the path would never match.  Note that this
   * only works for placeholders at the end of the path. Infix placeholders
   * with default values don't make sense anyway, so that should not be a
   * problem.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to have the placeholders removed from.
   *
   * @return string
   *   The path string, stripped of placeholders that have default values.
   */
  public static function getPathWithoutDefaults(Route $route) {
    $path = $route
      ->getPath();
    $defaults = $route
      ->getDefaults();

    // Remove placeholders with default values from the outline, so that they
    // will still match.
    $remove = array_map(function ($a) {
      return '/{' . $a . '}';
    }, array_keys($defaults));
    $path = str_replace($remove, '', $path);
    return $path;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteCompiler::compile public static function Compiles the current route instance. Overrides RouteCompiler::compile
RouteCompiler::compilePattern private static function
RouteCompiler::computeRegexp private static function Computes the regexp used to match a specific token. It can be static text or a subpattern.
RouteCompiler::findNextSeparator private static function Returns the next static character in the Route pattern that will serve as a separator.
RouteCompiler::getFit public static function Determines the fitness of the provided path.
RouteCompiler::getPathWithoutDefaults public static function Returns the path of the route, without placeholders with a default value.
RouteCompiler::getPatternOutline public static function Returns the pattern outline.
RouteCompiler::REGEX_DELIMITER constant Utility constant to use for regular expressions against the path. Overrides RouteCompiler::REGEX_DELIMITER
RouteCompiler::SEPARATORS constant This string defines the characters that are automatically considered separators in front of optional placeholders (with default and no static text following). Such a single separator can be left out together with the optional placeholder from matching…