Skip to content
Permalink
Browse files

bug #31167 [Routing] fix matching trailing vars with defaults (nicola…

…s-grekas)

This PR was merged into the 4.2 branch.

Discussion
----------

[Routing] fix matching trailing vars with defaults

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31158
| License       | MIT
| Doc PR        | -

Commits
-------

177dfbc [Routing] fix matching trailing vars with defaults
  • Loading branch information
nicolas-grekas committed Apr 18, 2019
2 parents 5379e73 + 177dfbc commit 243b257ca7538d4e464d8675060f510bf93a7fac
@@ -132,7 +132,7 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche

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

if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[\count($vars)], -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
if ($hasTrailingVar && ($hasTrailingSlash || !($n = $matches[\count($vars)] ?? '') || '/' !== substr($n, -1)) && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
if ($hasTrailingSlash) {
$matches = $n;
} else {
@@ -158,7 +158,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)

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

if ($hasTrailingVar && ($hasTrailingSlash || '/' !== substr($matches[(\count($matches) - 1) >> 1], -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingVar && ($hasTrailingSlash || !($m = $matches[\count($compiledRoute->getPathVariables())] ?? '') || '/' !== substr($m, -1)) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingSlash) {
$matches = $m;
} else {
@@ -198,6 +198,17 @@ public function testNonGreedyTrailingRequirement()
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
}

public function testTrailingRequirementWithDefault()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/foo/{a}', ['a' => 'bar'], ['a' => '.+']));

$matcher = $this->getUrlMatcher($coll);
$matcher->expects($this->once())->method('redirect')->with('/foo')->willReturn([]);

$this->assertEquals(['_route' => 'a', 'a' => 'bar'], $matcher->match('/foo/'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', [$routes, $context ?: new RequestContext()]);

0 comments on commit 243b257

Please sign in to comment.
You can’t perform that action at this time.