Skip to content

Commit

Permalink
[Routing] Prevent some double match of hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Feb 22, 2018
1 parent 7b8934b commit 8e207cd
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Expand Up @@ -239,7 +239,7 @@ private function compileStaticRoutes(array $staticRoutes, bool $supportsRedirect

$code .= sprintf(" case %s:\n", self::export($url));
foreach ($routes as $name => list($hasTrailingSlash, $route)) {
$code .= $this->compileRoute($route, $name, $supportsRedirections, $hasTrailingSlash);
$code .= $this->compileRoute($route, $name, $supportsRedirections, $hasTrailingSlash, true);
}
$code .= " break;\n";
}
Expand Down Expand Up @@ -451,7 +451,7 @@ private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \st
$hasTrailingSlash = $state->supportsRedirections && '' !== $regex && '/' === $regex[-1];

if ($compiledRoute->getRegex() === $prevRegex) {
$state->switch = substr_replace($state->switch, $this->compileRoute($route, $name, $state->supportsRedirections, $hasTrailingSlash)."\n", -19, 0);
$state->switch = substr_replace($state->switch, $this->compileRoute($route, $name, $state->supportsRedirections, $hasTrailingSlash, false)."\n", -19, 0);
continue;
}

Expand Down Expand Up @@ -485,7 +485,7 @@ private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \st

$state->switch .= <<<EOF
case {$state->mark}:
{$combine}{$this->compileRoute($route, $name, $state->supportsRedirections, $hasTrailingSlash)}
{$combine}{$this->compileRoute($route, $name, $state->supportsRedirections, $hasTrailingSlash, false)}
break;
EOF;
Expand Down Expand Up @@ -572,16 +572,9 @@ private function compileSwitchDefault(bool $hasVars, string $routesKey, bool $ma
/**
* Compiles a single Route to PHP code used to match it against the path info.
*
* @param Route $route A Route instance
* @param string $name The name of the Route
* @param bool $supportsRedirections Whether redirections are supported by the base class
* @param bool $hasTrailingSlash Whether the path has a trailing slash
*
* @return string PHP code
*
* @throws \LogicException
*/
private function compileRoute(Route $route, string $name, bool $supportsRedirections, bool $hasTrailingSlash): string
private function compileRoute(Route $route, string $name, bool $supportsRedirections, bool $hasTrailingSlash, bool $checkHost): string
{
$code = '';
$compiledRoute = $route->compile();
Expand All @@ -605,7 +598,7 @@ private function compileRoute(Route $route, string $name, bool $supportsRedirect
$conditions[] = $expression;
}

if (!$compiledRoute->getHostRegex()) {
if (!$checkHost || !$compiledRoute->getHostRegex()) {
// no-op
} elseif ($hostMatches) {
$conditions[] = sprintf('preg_match(%s, $host, $hostMatches)', self::export($compiledRoute->getHostRegex()));
Expand Down

0 comments on commit 8e207cd

Please sign in to comment.