Skip to content

Commit 5b52dd4

Browse files
bug #29542 [Routing] fix dumping same-path routes with placeholders (nicolas-grekas)
This PR was merged into the 4.1 branch. Discussion ---------- [Routing] fix dumping same-path routes with placeholders | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Only 4.1 is affected. Commits ------- 94a143011a [Routing] fix dumping same-path routes with placeholders
2 parents 7c808e6 + 7ec6a3b commit 5b52dd4

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,10 @@ private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \st
526526
);
527527
} else {
528528
$prevRegex = $compiledRoute->getRegex();
529-
$combine = ' $matches = array(';
530-
foreach ($vars as $j => $m) {
531-
$combine .= sprintf('%s => $matches[%d] ?? null, ', self::export($m), 1 + $j);
532-
}
533-
$combine = $vars ? substr_replace($combine, ");\n\n", -2) : '';
534529

535530
$state->switch .= <<<EOF
536531
case {$state->mark}:
537-
{$combine}{$this->compileRoute($route, $name, false, $hasTrailingSlash)}
532+
{$this->compileRoute($route, $name, false, $hasTrailingSlash, $vars)}
538533
break;
539534
540535
EOF;
@@ -621,7 +616,7 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
621616
*
622617
* @throws \LogicException
623618
*/
624-
private function compileRoute(Route $route, string $name, bool $checkHost, bool $hasTrailingSlash): string
619+
private function compileRoute(Route $route, string $name, bool $checkHost, bool $hasTrailingSlash, array $vars = null): string
625620
{
626621
$compiledRoute = $route->compile();
627622
$conditions = array();
@@ -669,6 +664,14 @@ private function compileRoute(Route $route, string $name, bool $checkHost, bool
669664
);
670665
}
671666

667+
if ($vars) {
668+
$code .= ' $matches = array(';
669+
foreach ($vars as $j => $m) {
670+
$code .= sprintf('%s => $matches[%d] ?? null, ', self::export($m), 1 + $j);
671+
}
672+
$code = substr_replace($code, ");\n\n", -2);
673+
}
674+
672675
if ($route->getCondition()) {
673676
$expression = $this->getExpressionLanguage()->compile($route->getCondition(), array('context', 'request'));
674677

Tests/Fixtures/dumper/url_matcher1.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ public function match($rawPathinfo)
139139
while (preg_match($regex, $matchedPathinfo, $matches)) {
140140
switch ($m = (int) $matches['MARK']) {
141141
case 115:
142-
$matches = array('foo' => $matches[1] ?? null);
143-
144142
// baz4
145143
if ('/' !== $pathinfo[-1]) {
146144
goto not_baz4;
@@ -149,6 +147,8 @@ public function match($rawPathinfo)
149147
$matches = $n;
150148
}
151149

150+
$matches = array('foo' => $matches[1] ?? null);
151+
152152
return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
153153
not_baz4:
154154

@@ -188,13 +188,13 @@ public function match($rawPathinfo)
188188

189189
break;
190190
case 160:
191-
$matches = array('foo' => $matches[1] ?? null);
192-
193191
// foo1
194192
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
195193
goto not_foo1;
196194
}
197195

196+
$matches = array('foo' => $matches[1] ?? null);
197+
198198
$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
199199
if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
200200
$allow += $a;
@@ -206,25 +206,25 @@ public function match($rawPathinfo)
206206

207207
break;
208208
case 204:
209-
$matches = array('foo1' => $matches[1] ?? null);
210-
211209
// foo2
212210
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
213211
goto not_foo2;
214212
}
215213

214+
$matches = array('foo1' => $matches[1] ?? null);
215+
216216
return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
217217
not_foo2:
218218

219219
break;
220220
case 279:
221-
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
222-
223221
// foo3
224222
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
225223
goto not_foo3;
226224
}
227225

226+
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
227+
228228
return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
229229
not_foo3:
230230

Tests/Fixtures/dumper/url_matcher13.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public function match($rawPathinfo)
4242
while (preg_match($regex, $matchedPathinfo, $matches)) {
4343
switch ($m = (int) $matches['MARK']) {
4444
case 56:
45-
$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
46-
4745
// r1
4846
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
4947
goto not_r1;
5048
}
5149

50+
$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
51+
5252
return $this->mergeDefaults(array('_route' => 'r1') + $matches, array());
5353
not_r1:
5454

Tests/Fixtures/dumper/url_matcher2.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
179179
while (preg_match($regex, $matchedPathinfo, $matches)) {
180180
switch ($m = (int) $matches['MARK']) {
181181
case 115:
182-
$matches = array('foo' => $matches[1] ?? null);
183-
184182
// baz4
185183
if ('/' !== $pathinfo[-1]) {
186184
if ('GET' === $canonicalMethod) {
@@ -192,6 +190,8 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
192190
$matches = $n;
193191
}
194192

193+
$matches = array('foo' => $matches[1] ?? null);
194+
195195
return $this->mergeDefaults(array('_route' => 'baz4') + $matches, array());
196196
not_baz4:
197197

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

232232
break;
233233
case 160:
234-
$matches = array('foo' => $matches[1] ?? null);
235-
236234
// foo1
237235
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
238236
goto not_foo1;
239237
}
240238

239+
$matches = array('foo' => $matches[1] ?? null);
240+
241241
$ret = $this->mergeDefaults(array('_route' => 'foo1') + $matches, array());
242242
if (!isset(($a = array('PUT' => 0))[$requestMethod])) {
243243
$allow += $a;
@@ -249,8 +249,6 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
249249

250250
break;
251251
case 204:
252-
$matches = array('foo1' => $matches[1] ?? null);
253-
254252
// foo2
255253
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
256254
if ('GET' === $canonicalMethod) {
@@ -259,13 +257,13 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
259257
goto not_foo2;
260258
}
261259

260+
$matches = array('foo1' => $matches[1] ?? null);
261+
262262
return $this->mergeDefaults(array('_route' => 'foo2') + $matches, array());
263263
not_foo2:
264264

265265
break;
266266
case 279:
267-
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
268-
269267
// foo3
270268
if ('/' !== $pathinfo && '/' === $pathinfo[-1] && preg_match($regex, substr($pathinfo, 0, -1), $n) && $m === (int) $n['MARK']) {
271269
if ('GET' === $canonicalMethod) {
@@ -274,6 +272,8 @@ private function doMatch(string $rawPathinfo, array &$allow = array(), array &$a
274272
goto not_foo3;
275273
}
276274

275+
$matches = array('_locale' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
276+
277277
return $this->mergeDefaults(array('_route' => 'foo3') + $matches, array());
278278
not_foo3:
279279

0 commit comments

Comments
 (0)