Skip to content

[Routing] fix dumping same-path routes with placeholders #29542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,10 @@ private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \st
);
} else {
$prevRegex = $compiledRoute->getRegex();
$combine = ' $matches = array(';
foreach ($vars as $j => $m) {
$combine .= sprintf('%s => $matches[%d] ?? null, ', self::export($m), 1 + $j);
}
$combine = $vars ? substr_replace($combine, ");\n\n", -2) : '';

$state->switch .= <<<EOF
case {$state->mark}:
{$combine}{$this->compileRoute($route, $name, false, $hasTrailingSlash)}
{$this->compileRoute($route, $name, false, $hasTrailingSlash, $vars)}
break;

EOF;
Expand Down Expand Up @@ -621,7 +616,7 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
*
* @throws \LogicException
*/
private function compileRoute(Route $route, string $name, bool $checkHost, bool $hasTrailingSlash): string
private function compileRoute(Route $route, string $name, bool $checkHost, bool $hasTrailingSlash, array $vars = null): string
{
$compiledRoute = $route->compile();
$conditions = array();
Expand Down Expand Up @@ -669,6 +664,14 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
);
}

if ($vars) {
$code .= ' $matches = array(';
foreach ($vars as $j => $m) {
$code .= sprintf('%s => $matches[%d] ?? null, ', self::export($m), 1 + $j);
}
$code = substr_replace($code, ");\n\n", -2);
}

if ($route->getCondition()) {
$expression = $this->getExpressionLanguage()->compile($route->getCondition(), array('context', 'request'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ public function match($rawPathinfo)
while (preg_match($regex, $matchedPathinfo, $matches)) {
switch ($m = (int) $matches['MARK']) {
case 115:
$matches = array('foo' => $matches[1] ?? null);

// baz4
if ('/' !== $pathinfo[-1]) {
goto not_baz4;
Expand All @@ -149,6 +147,8 @@ public function match($rawPathinfo)
$matches = $n;
}

$matches = array('foo' => $matches[1] ?? null);

return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
not_baz4:

Expand Down Expand Up @@ -188,13 +188,13 @@ public function match($rawPathinfo)

break;
case 160:
$matches = array('foo' => $matches[1] ?? null);

// foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
goto not_foo1;
}

$matches = array('foo' => $matches[1] ?? null);

$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a;
Expand All @@ -206,25 +206,25 @@ public function match($rawPathinfo)

break;
case 204:
$matches = array('foo1' => $matches[1] ?? null);

// foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
goto not_foo2;
}

$matches = array('foo1' => $matches[1] ?? null);

return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
not_foo2:

break;
case 279:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
goto not_foo3;
}

$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
not_foo3:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public function match($rawPathinfo)
while (preg_match($regex, $matchedPathinfo, $matches)) {
switch ($m = (int) $matches['MARK']) {
case 56:
$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// r1
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
goto not_r1;
}

$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

return $this->mergeDefaults(array('_route' => 'r1') + $matches, array());
not_r1:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
while (preg_match($regex, $matchedPathinfo, $matches)) {
switch ($m = (int) $matches['MARK']) {
case 115:
$matches = array('foo' => $matches[1] ?? null);

// baz4
if ('/' !== $pathinfo[-1]) {
if ('GET' === $canonicalMethod) {
Expand All @@ -192,6 +190,8 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
$matches = $n;
}

$matches = array('foo' => $matches[1] ?? null);

return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
not_baz4:

Expand Down Expand Up @@ -231,13 +231,13 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

break;
case 160:
$matches = array('foo' => $matches[1] ?? null);

// foo1
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
goto not_foo1;
}

$matches = array('foo' => $matches[1] ?? null);

$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
$allow += $a;
Expand All @@ -249,8 +249,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a

break;
case 204:
$matches = array('foo1' => $matches[1] ?? null);

// foo2
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
if ('GET' === $canonicalMethod) {
Expand All @@ -259,13 +257,13 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
goto not_foo2;
}

$matches = array('foo1' => $matches[1] ?? null);

return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
not_foo2:

break;
case 279:
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

// foo3
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
if ('GET' === $canonicalMethod) {
Expand All @@ -274,6 +272,8 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
goto not_foo3;
}

$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);

return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
not_foo3:

Expand Down