You are here

private function ApacheMatcherDumper::dumpRoute in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/routing/Matcher/Dumper/ApacheMatcherDumper.php \Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper::dumpRoute()

Dumps a single route.

Parameters

string $name Route name:

Route $route The route:

array $options Options:

bool $hostRegexUnique Unique identifier for the host regex:

Return value

string The compiled route

1 call to ApacheMatcherDumper::dumpRoute()
ApacheMatcherDumper::dump in vendor/symfony/routing/Matcher/Dumper/ApacheMatcherDumper.php
Dumps a set of Apache mod_rewrite rules.

File

vendor/symfony/routing/Matcher/Dumper/ApacheMatcherDumper.php, line 122

Class

ApacheMatcherDumper
Dumps a set of Apache mod_rewrite rules.

Namespace

Symfony\Component\Routing\Matcher\Dumper

Code

private function dumpRoute($name, $route, array $options, $hostRegexUnique) {
  $compiledRoute = $route
    ->compile();

  // prepare the apache regex
  $regex = $this
    ->regexToApacheRegex($compiledRoute
    ->getRegex());
  $regex = '^' . self::escape(preg_quote($options['base_uri']) . substr($regex, 1), ' ', '\\');
  $methods = $this
    ->getRouteMethods($route);
  $hasTrailingSlash = (!$methods || in_array('HEAD', $methods)) && '/$' === substr($regex, -2) && '^/$' !== $regex;
  $variables = array(
    'E=_ROUTING_route:' . $name,
  );
  foreach ($compiledRoute
    ->getHostVariables() as $variable) {
    $variables[] = sprintf('E=_ROUTING_param_%s:%%{ENV:__ROUTING_host_%s_%s}', $variable, $hostRegexUnique, $variable);
  }
  foreach ($compiledRoute
    ->getPathVariables() as $i => $variable) {
    $variables[] = 'E=_ROUTING_param_' . $variable . ':%' . ($i + 1);
  }
  foreach ($this
    ->normalizeValues($route
    ->getDefaults()) as $key => $value) {
    $variables[] = 'E=_ROUTING_default_' . $key . ':' . strtr($value, array(
      ':' => '\\:',
      '=' => '\\=',
      '\\' => '\\\\',
      ' ' => '\\ ',
    ));
  }
  $variables = implode(',', $variables);
  $rule = array(
    "# {$name}",
  );

  // method mismatch
  if (0 < count($methods)) {
    $allow = array();
    foreach ($methods as $method) {
      $allow[] = 'E=_ROUTING_allow_' . $method . ':1';
    }
    if ($compiledRoute
      ->getHostRegex()) {
      $rule[] = sprintf('RewriteCond %%{ENV:__ROUTING_host_%s} =1', $hostRegexUnique);
    }
    $rule[] = "RewriteCond %{REQUEST_URI} {$regex}";
    $rule[] = sprintf('RewriteCond %%{REQUEST_METHOD} !^(%s)$ [NC]', implode('|', $methods));
    $rule[] = sprintf('RewriteRule .* - [S=%d,%s]', $hasTrailingSlash ? 2 : 1, implode(',', $allow));
  }

  // redirect with trailing slash appended
  if ($hasTrailingSlash) {
    if ($compiledRoute
      ->getHostRegex()) {
      $rule[] = sprintf('RewriteCond %%{ENV:__ROUTING_host_%s} =1', $hostRegexUnique);
    }
    $rule[] = 'RewriteCond %{REQUEST_URI} ' . substr($regex, 0, -2) . '$';
    $rule[] = 'RewriteRule .* $0/ [QSA,L,R=301]';
  }

  // the main rule
  if ($compiledRoute
    ->getHostRegex()) {
    $rule[] = sprintf('RewriteCond %%{ENV:__ROUTING_host_%s} =1', $hostRegexUnique);
  }
  $rule[] = "RewriteCond %{REQUEST_URI} {$regex}";
  $rule[] = "RewriteRule .* {$options['script_name']} [QSA,L,{$variables}]";
  return implode("\n", $rule);
}