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 25, 2018
1 parent 7f310b4 commit 9aa09e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -140,14 +140,18 @@ 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]]) {
$variablePattern = $token[2];
if ($this->hasLookAround($token[2])) {
$variablePattern = $this->removeLookAround($token[2]);
}
// check requirement
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]])) {
if (null !== $this->strictRequirements && !preg_match('#^'.$variablePattern.'$#'.(empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]])) {
if ($this->strictRequirements) {
throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]])));
throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $variablePattern, '{given}' => $mergedParams[$token[3]])));
}

if ($this->logger) {
$this->logger->error($message, array('parameter' => $token[3], 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$token[3]]));
$this->logger->error($message, array('parameter' => $token[3], 'route' => $name, 'expected' => $variablePattern, 'given' => $mergedParams[$token[3]]));
}

return;
Expand Down Expand Up @@ -318,4 +322,18 @@ public static function getRelativePath($basePath, $targetPath)
|| false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
? "./$path" : $path;
}

private function hasLookAround($path)
{
if (false === $i = strpos($path, '(?')) {
return false;
}

return false !== strpos($path, '(?=', $i) || false !== strpos($path, '(?<=', $i) || false !== strpos($path, '(?!', $i) || false !== strpos($path, '(?<!', $i);
}

private function removeLookAround($path)
{
return preg_replace('/\(\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\((?1)\))*)\)/', '', $path);
}
}
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

0 comments on commit 9aa09e1

Please sign in to comment.