8000 [Routing] fix trailing slash redirection with non-greedy trailing vars · symfony/symfony@d0c5649 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0c5649

Browse files
[Routing] fix trailing slash redirection with non-greedy trailing vars
1 parent dc2edaf commit d0c5649

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherTrait.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,22 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
131131
}
132132

133133
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
134+
135+
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[\count($vars)], -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $t 8000 rimmedPathinfo, $n) && $m === (int) $n['MARK']) {
136+
if ($hasTrailingSlash) {
137+
$matches = $n;
138+
} else {
139+
$hasTrailingVar = false;
140+
}
141+
}
142+
134143
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
135144
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
136145
return $allow = $allowSchemes = [];
137146
}
138147
continue;
139148
}
140149

141-
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
142-
$matches = $n;
143-
}
144-
145150
foreach ($vars as $i => $v) {
146151
if (isset($matches[1 + $i])) {
147152
$ret[$v] = $matches[1 + $i];

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
158158

159159
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
160160

161+
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[(\count($matches) - 1) >> 1], -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
162+
if ($hasTrailingSlash) {
163+
$matches = $m;
164+
} else {
165+
$hasTrailingVar = false;
166+
}
167+
}
168+
161169
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
162170
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
163171
return $this->allow = $this->allowSchemes = [];
@@ -166,10 +174,6 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
166174
continue;
167175
}
168176

169-
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $trimmedPathinfo, $m)) {
170-
$matches = $m;
171-
}
172-
173177
$hostMatches = [];
174178
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
175179
continue;

src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ public function testSlashAndVerbPrecedenceWithRedirection()
187187
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
188188
}
189189

190+
public function testNonGreedyTrailingRequirement()
191+
{
192+
$coll = new RouteCollection();
193+
$coll->add('a', new Route('/{a}', [], ['a' => '\d+']));
194+
195+
$matcher = $this->getUrlMatcher($coll);
196+
$matcher->expects($this->once())->method('redirect')->with('/123')->willReturn([]);
197+
198+
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
199+
}
200+
190201
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
191202
{
192203
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', [$routes, $context ?: new RequestContext()]);

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterfac
258258
*/
259259
public function setTranslator(LegacyTranslatorInterface $translator)
260260
{
261-
$this->translator = $translator instanceof LegacyTranslatorProxy ? $translator->getTranslator() : $translator;
261+
$this->translator = $translator;
262+
263+
while ($this->translator instanceof LegacyTranslatorProxy) {
264+
$this->translator = $this->translator->getTranslator();
265+
}
262266

263267
return $this;
264268
}

0 commit comments

Comments
 (0)
0