Skip to content

Commit

Permalink
[Routing] fix URL generation with look-around requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
nasimnabavi authored and nicolas-grekas committed Nov 26, 2018
1 parent 7f310b4 commit 578eb1b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -140,8 +140,8 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
foreach ($tokens as $token) {
if ('variable' === $token[0]) {
if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
// check requirement
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]])) {
// check requirement (while ignoring look-around patterns)
if (null !== $this->strictRequirements && !preg_match('#^'.preg_replace('/\(\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\((?1)\))*)\)/', '', $token[2]).'$#'.($token[4] ?? ''), $mergedParams[$token[3]])) {
if ($this->strictRequirements) {
throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]])));
}
Expand Down Expand Up @@ -195,7 +195,8 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$routeHost = '';
foreach ($hostTokens as $token) {
if ('variable' === $token[0]) {
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]])) {
// check requirement (while ignoring look-around patterns)
if (null !== $this->strictRequirements && !preg_match('#^'.preg_replace('/\(\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\((?1)\))*)\)/', '', $token[2]).'$#i'.($token[4] ?? ''), $mergedParams[$token[3]])) {
if ($this->strictRequirements) {
throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]])));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/RouteCompiler.php
Expand Up @@ -217,7 +217,7 @@ private static function compilePattern(Route $route, $pattern, $isHost)
$regexp .= 'u';
for ($i = 0, $nbToken = \count($tokens); $i < $nbToken; ++$i) {
if ('variable' === $tokens[$i][0]) {
$tokens[$i][] = true;
$tokens[$i][] = 'u';
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
Expand Up @@ -703,6 +703,30 @@ public function testFragmentsCanBeDefinedAsDefaults()
$this->assertEquals('/app.php/testing#fragment', $url);
}

public function testLookAheadPositiveInRequirements()
{
$routes = $this->getRoutes('test', new Route('/{foo}/b(ar/{baz}', array(), array('foo' => '.+(?=/b\\(ar/)', 'baz' => '.+?')));
$this->assertSame('/app.php/a/b/b%28ar/c/d/e', $this->getGenerator($routes)->generate('test', array('foo' => 'a/b', 'baz' => 'c/d/e')));
}

public function testLookAheadNegativeInRequirementss()
{
$routes = $this->getRoutes('test', new Route('/{foo}/bar/{baz}', array(), array('foo' => '.+(?!$)', 'baz' => '.+?')));
$this->assertSame('/app.php/a/b/bar/c/d/e', $this->getGenerator($routes)->generate('test', array('foo' => 'a/b', 'baz' => 'c/d/e')));
}

public function testLookBehindPositiveInRequirements()
{
$routes = $this->getRoutes('test', new Route('/bar/{foo}/bam/{baz}', array(), array('foo' => '(?<=/bar/).+', 'baz' => '.+?')));
$this->assertSame('/app.php/bar/a/b/bam/c/d/e', $this->getGenerator($routes)->generate('test', array('foo' => 'a/b', 'baz' => 'c/d/e')));
}

public function testLookBehindNegativeInRequirements()
{
$routes = $this->getRoutes('test', new Route('/bar/{foo}/bam/{baz}', array(), array('foo' => '(?<!^).+', 'baz' => '.+?')));
$this->assertSame('/app.php/bar/a/b/bam/c/d/e', $this->getGenerator($routes)->generate('test', array('foo' => 'a/b', 'baz' => 'c/d/e')));
}

protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
{
$context = new RequestContext('/app.php');
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Routing/Tests/RouteCompilerTest.php
Expand Up @@ -177,7 +177,7 @@ public function provideCompileData()
'Route with an explicit UTF-8 requirement',
array('/{bar}', array('bar' => null), array('bar' => '.'), array('utf8' => true)),
'', '#^/(?P<bar>.)?$#sDu', array('bar'), array(
array('variable', '/', '.', 'bar', true),
array('variable', '/', '.', 'bar', 'u'),
),
),
);
Expand Down Expand Up @@ -216,7 +216,7 @@ public function provideCompileImplicitUtf8Data()
'Route with an implicit UTF-8 requirement',
array('/{bar}', array('bar' => null), array('bar' => 'é')),
'', '#^/(?P<bar>é)?$#sDu', array('bar'), array(
array('variable', '/', 'é', 'bar', true),
array('variable', '/', 'é', 'bar', 'u'),
),
'requirements',
),
Expand All @@ -225,7 +225,7 @@ public function provideCompileImplicitUtf8Data()
'Route with a UTF-8 class requirement',
array('/{bar}', array('bar' => null), array('bar' => '\pM')),
'', '#^/(?P<bar>\pM)?$#sDu', array('bar'), array(
array('variable', '/', '\pM', 'bar', true),
array('variable', '/', '\pM', 'bar', 'u'),
),
'requirements',
),
Expand All @@ -234,8 +234,8 @@ public function provideCompileImplicitUtf8Data()
'Route with a UTF-8 separator',
array('/foo/{bar}§{_format}', array(), array(), array('compiler_class' => Utf8RouteCompiler::class)),
'/foo', '#^/foo/(?P<bar>[^/§]++)§(?P<_format>[^/]++)$#sDu', array('bar', '_format'), array(
array('variable', '§', '[^/]++', '_format', true),
array('variable', '/', '[^/§]++', 'bar', true),
array('variable', '§', '[^/]++', '_format', 'u'),
array('variable', '/', '[^/§]++', 'bar', 'u'),
array('text', '/foo'),
),
'patterns',
Expand Down

0 comments on commit 578eb1b

Please sign in to comment.