diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 0dc0a178a8542..c5f3739cf363a 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -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 .= <<mark}: -{$combine}{$this->compileRoute($route, $name, false, $hasTrailingSlash)} +{$this->compileRoute($route, $name, false, $hasTrailingSlash, $vars)} break; EOF; @@ -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(); @@ -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')); diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php index c5074514e86e4..ccbe5941f2aa5 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -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; @@ -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: @@ -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; @@ -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: diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher13.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher13.php index 23c9327857175..60cc69fa7bfee 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher13.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher13.php @@ -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: diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php index d8d0e3bc372ab..3a61a2bdf78ee 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -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) { @@ -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: @@ -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; @@ -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) { @@ -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) { @@ -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: