private function PhpMatcherDumper::compileRoutes

Generates PHP code recursively to match a RouteCollection with all child routes and child collections.

Parameters

RouteCollection $routes A RouteCollection instance:

Boolean $supportsRedirections Whether redirections are supported by the base class:

string|null $parentPrefix The prefix of the parent collection used to optimize the code:

Return value

string PHP code

1 call to PhpMatcherDumper::compileRoutes()
PhpMatcherDumper::generateMatchMethod in drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Generates the code for the match method implementing UrlMatcherInterface.

File

drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php, line 129

Class

PhpMatcherDumper
PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.

Namespace

Symfony\Component\Routing\Matcher\Dumper

Code

private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null) {
  $code = '';
  $prefix = $routes
    ->getPrefix();
  $countDirectChildRoutes = $this
    ->countDirectChildRoutes($routes);
  $countAllChildRoutes = count($routes
    ->all());

  // Can the matching be optimized by wrapping it with the prefix condition
  // - no need to optimize if current prefix is the same as the parent prefix
  // - if $countDirectChildRoutes === 0, the sub-collections can do their own optimizations (in case there are any)
  // - it's not worth wrapping a single child route
  // - prefixes with variables cannot be optimized because routes within the collection might have different requirements for the same variable
  $optimizable = '' !== $prefix && $prefix !== $parentPrefix && $countDirectChildRoutes > 0 && $countAllChildRoutes > 1 && false === strpos($prefix, '{');
  if ($optimizable) {
    $code .= sprintf("    if (0 === strpos(\$pathinfo, %s)) {\n", var_export($prefix, true));
  }
  foreach ($routes as $name => $route) {
    if ($route instanceof Route) {

      // a single route in a sub-collection is not wrapped so it should do its own optimization in ->compileRoute with $parentPrefix = null
      $code .= $this
        ->compileRoute($route, $name, $supportsRedirections, 1 === $countAllChildRoutes ? null : $prefix) . "\n";
    }
    elseif ($countAllChildRoutes - $countDirectChildRoutes > 0) {

      // we can stop iterating recursively if we already know there are no more routes
      $code .= $this
        ->compileRoutes($route, $supportsRedirections, $prefix);
    }
  }
  if ($optimizable) {
    $code .= "    }\n\n";

    // apply extra indention at each line (except empty ones)
    $code = preg_replace('/^.{2,}$/m', '    $0', $code);
  }
  return $code;
}