Skip to content

Commit 1a859d2

Browse files
Merge branch '4.2'
* 4.2: [HttpFoundation] fix tests [Routing] fix trailing slash matching with empty-matching trailing vars [Routing] fix matching trailing vars with defaults [Validator] fix LegacyTranslatorProxy call method with Translator component only bumped Symfony version to 4.2.8 updated VERSION for 4.2.7 updated CHANGELOG for 4.2.7 bumped Symfony version to 3.4.27 updated VERSION for 3.4.26 updated CHANGELOG for 3.4.26
2 parents e7a45bd + c898cc9 commit 1a859d2

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Matcher/Dumper/CompiledUrlMatcherTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
137137

138138
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
139139

140-
if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[\count($vars)], -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
140+
if ($hasTrailingVar && ($hasTrailingSlash || (null === $n = $matches[\count($vars)] ?? null) || '/' !== ($n[-1] ?? '/')) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
141141
if ($hasTrailingSlash) {
142142
$matches = $n;
143143
} else {

Matcher/UrlMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ 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)) {
161+
if ($hasTrailingVar && ($hasTrailingSlash || (null === $m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
162162
if ($hasTrailingSlash) {
163163
$matches = $m;
164164
} else {

Tests/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ public function testNonGreedyTrailingRequirement()
198198
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
199199
}
200200

201+
public function testTrailingRequirementWithDefault_A()
202+
{
203+
$coll = new RouteCollection();
204+
$coll->add('a', new Route('/fr-fr/{a}', ['a' => 'aaa'], ['a' => '.+']));
205+
206+
$matcher = $this->getUrlMatcher($coll);
207+
$matcher->expects($this->once())->method('redirect')->with('/fr-fr')->willReturn([]);
208+
209+
$this->assertEquals(['_route' => 'a', 'a' => 'aaa'], $matcher->match('/fr-fr/'));
210+
}
211+
201212
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
202213
{
203214
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', [$routes, $context ?: new RequestContext()]);

Tests/Matcher/UrlMatcherTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,41 @@ public function testGreedyTrailingRequirement()
777777
$this->assertEquals(['_route' => 'a', 'a' => 'foo/'], $matcher->match('/foo/'));
778778
}
779779

780+
public function testTrailingRequirementWithDefault()
781+
{
782+
$coll = new RouteCollection();
783+
$coll->add('a', new Route('/fr-fr/{a}', ['a' => 'aaa'], ['a' => '.+']));
784+
$coll->add('b', new Route('/en-en/{b}', ['b' => 'bbb'], ['b' => '.*']));
785+
786+
$matcher = $this->getUrlMatcher($coll);
787+
788+
$this->assertEquals(['_route' => 'a', 'a' => 'aaa'], $matcher->match('/fr-fr'));
789+
$this->assertEquals(['_route' => 'a', 'a' => 'AAA'], $matcher->match('/fr-fr/AAA'));
790+
$this->assertEquals(['_route' => 'b', 'b' => 'bbb'], $matcher->match('/en-en'));
791+
$this->assertEquals(['_route' => 'b', 'b' => 'BBB'], $matcher->match('/en-en/BBB'));
792+
}
793+
794+
public function testTrailingRequirementWithDefault_A()
795+
{
796+
$coll = new RouteCollection();
797+
$coll->add('a', new Route('/fr-fr/{a}', ['a' => 'aaa'], ['a' => '.+']));
798+
799+
$matcher = $this->getUrlMatcher($coll);
800+
801+
$this->expectException(ResourceNotFoundException::class);
802+
$matcher->match('/fr-fr/');
803+
}
804+
805+
public function testTrailingRequirementWithDefault_B()
806+
{
807+
$coll = new RouteCollection();
808+
$coll->add('b', new Route('/en-en/{b}', ['b' => 'bbb'], ['b' => '.*']));
809+
810+
$matcher = $this->getUrlMatcher($coll);
811+
812+
$this->assertEquals(['_route' => 'b', 'b' => ''], $matcher->match('/en-en/'));
813+
}
814+
780815
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
781816
{
782817
return new UrlMatcher($routes, $context ?: new RequestContext());

0 commit comments

Comments
 (0)