You are here

public function ApacheMatcherDumper::dump in Zircon Profile 8.0

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

Dumps a set of Apache mod_rewrite rules.

Available options:

  • script_name: The script name (app.php by default)
  • base_uri: The base URI ("" by default)

Parameters

array $options An array of options:

Return value

string A string to be used as Apache rewrite rules

Throws

\LogicException When the route regex is invalid

Overrides MatcherDumperInterface::dump

File

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

Class

ApacheMatcherDumper
Dumps a set of Apache mod_rewrite rules.

Namespace

Symfony\Component\Routing\Matcher\Dumper

Code

public function dump(array $options = array()) {
  $options = array_merge(array(
    'script_name' => 'app.php',
    'base_uri' => '',
  ), $options);
  $options['script_name'] = self::escape($options['script_name'], ' ', '\\');
  $rules = array(
    "# skip \"real\" requests\nRewriteCond %{REQUEST_FILENAME} -f\nRewriteRule .* - [QSA,L]",
  );
  $methodVars = array();
  $hostRegexUnique = 0;
  $prevHostRegex = '';
  foreach ($this
    ->getRoutes()
    ->all() as $name => $route) {
    if ($route
      ->getCondition()) {
      throw new \LogicException(sprintf('Unable to dump the routes for Apache as route "%s" has a condition.', $name));
    }
    $compiledRoute = $route
      ->compile();
    $hostRegex = $compiledRoute
      ->getHostRegex();
    if (null !== $hostRegex && $prevHostRegex !== $hostRegex) {
      $prevHostRegex = $hostRegex;
      ++$hostRegexUnique;
      $rule = array();
      $regex = $this
        ->regexToApacheRegex($hostRegex);
      $regex = self::escape($regex, ' ', '\\');
      $rule[] = sprintf('RewriteCond %%{HTTP:Host} %s', $regex);
      $variables = array();
      $variables[] = sprintf('E=__ROUTING_host_%s:1', $hostRegexUnique);
      foreach ($compiledRoute
        ->getHostVariables() as $i => $variable) {
        $variables[] = sprintf('E=__ROUTING_host_%s_%s:%%%d', $hostRegexUnique, $variable, $i + 1);
      }
      $variables = implode(',', $variables);
      $rule[] = sprintf('RewriteRule .? - [%s]', $variables);
      $rules[] = implode("\n", $rule);
    }
    $rules[] = $this
      ->dumpRoute($name, $route, $options, $hostRegexUnique);
    $methodVars = array_merge($methodVars, $route
      ->getMethods());
  }
  if (0 < count($methodVars)) {
    $rule = array(
      '# 405 Method Not Allowed',
    );
    $methodVars = array_values(array_unique($methodVars));
    if (in_array('GET', $methodVars) && !in_array('HEAD', $methodVars)) {
      $methodVars[] = 'HEAD';
    }
    foreach ($methodVars as $i => $methodVar) {
      $rule[] = sprintf('RewriteCond %%{ENV:_ROUTING__allow_%s} =1%s', $methodVar, isset($methodVars[$i + 1]) ? ' [OR]' : '');
    }
    $rule[] = sprintf('RewriteRule .* %s [QSA,L]', $options['script_name']);
    $rules[] = implode("\n", $rule);
  }
  return implode("\n\n", $rules) . "\n";
}