Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] Fixed handling of optional parameters with text suffix in the route (/foo-{bar}-{baz}.html) #5129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ protected function mergeDefaults($params, $defaults)
{
$parameters = $defaults;
foreach ($params as $key => $value) {
if (!is_int($key)) {
// empty value in matches ($params) means not-matched optional parameter
if (!is_int($key) && !empty($value)) {
$parameters[$key] = rawurldecode($value);
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Routing/RouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public function compile(Route $route)
$token = $tokens[$i];
if ('variable' === $token[0] && $route->hasDefault($token[3])) {
$firstOptional = $i;
} elseif ('text' === $token[0]) {
continue;
} else {
// the case for variable w/o default value
break;
}
}
Expand Down Expand Up @@ -110,15 +113,10 @@ private function computeRegexp(array $tokens, $index, $firstOptional)
// When the only token is an optional variable token, the separator is required
return sprintf('%s(?P<%s>%s)?', preg_quote($token[1], '#'), $token[3], $token[2]);
} else {
$nbTokens = count($tokens);
$regexp = sprintf('%s(?P<%s>%s)', preg_quote($token[1], '#'), $token[3], $token[2]);
if ($index >= $firstOptional) {
// Enclose each optional tokens in a subpattern to make it optional
$regexp = "(?:$regexp";
if ($nbTokens - 1 == $index) {
// Close the optional subpatterns
$regexp .= str_repeat(")?", $nbTokens - $firstOptional);
}
$regexp = "(?:$regexp)?";
}

return $regexp;
Expand Down
24 changes: 24 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ public function testMatch()
$matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));

// Route with 1 optional variable and text suffix at the end
$collection = new RouteCollection();
$collection->add('bar', new Route('/bar-{foo}.html', array('foo' => 'foo'), array('foo' => 'foo|bar')));
$matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo'), $matcher->match('/bar.html'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo'), $matcher->match('/bar-foo.html'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'bar'), $matcher->match('/bar-bar.html'));

// Route with 2 optional variables and text suffix at the end
$collection = new RouteCollection();
$collection->add(
'bar',
new Route(
'/bar-{foo}-{baz}.html',
array('foo' => 'foo', 'baz' => '1'),
array('foo' => 'foo|bar', 'baz' => '\d')
)
);
$matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'baz' => 1), $matcher->match('/bar.html'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'baz' => 1), $matcher->match('/bar-foo.html'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'baz' => 2), $matcher->match('/bar-foo-2.html'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'baz' => 2), $matcher->match('/bar-2.html'));
}

public function testMatchWithPrefixes()
Expand Down
19 changes: 18 additions & 1 deletion tests/Symfony/Tests/Component/Routing/RouteCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function provideCompileData()
array(
'Route with several variables that have default values',
array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
'/foo', '#^/foo(?:/(?P<bar>[^/]+?)(?:/(?P<foobar>[^/]+?))?)?$#s', array('bar', 'foobar'), array(
'/foo', '#^/foo(?:/(?P<bar>[^/]+?))?(?:/(?P<foobar>[^/]+?))?$#s', array('bar', 'foobar'), array(
array('variable', '/', '[^/]+?', 'foobar'),
array('variable', '/', '[^/]+?', 'bar'),
array('text', '/foo'),
Expand Down Expand Up @@ -96,6 +96,23 @@ public function provideCompileData()
'', '#^/(?P<bar>(foo|bar))?$#s', array('bar'), array(
array('variable', '/', '(foo|bar)', 'bar'),
)),
array(
'Route with optional variable and suffix',
array('/foo-{bar}.html', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
'/foo', '#^/foo(?:\-(?P<bar>(foo|bar)))?\.html$#s', array('bar'), array(
array('text', '.html'),
array('variable', '-', '(foo|bar)', 'bar'),
array('text', '/foo'),
)),
array(
'Route with several optional variables and suffix',
array('/foo-{bar}-{baz}.html', array('bar' => 'bar', 'baz' => 'baz'), array('bar' => '(foo|bar)', 'baz' => '(baz|zab)')),
'/foo', '#^/foo(?:\-(?P<bar>(foo|bar)))?(?:\-(?P<baz>(baz|zab)))?\.html$#s', array('bar', 'baz'), array(
array('text', '.html'),
array('variable', '-', '(baz|zab)', 'baz'),
array('variable', '-', '(foo|bar)', 'bar'),
array('text', '/foo'),
)),
);
}

Expand Down