Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ac4c2a

Browse files
committed
bug#27601 [Routing] fix URL generation with look-around requirements (nasimnabavi)
This PR was merged into the 3.4 branch.Discussion----------[Routing] fix URL generation with look-around requirements| Q | A| ------------- | ---| Branch? | 2.8 up to 4.1 for bug fixes| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#10548| License | MIT| Doc PR |If you have a non-catchable pattern in requirements like f.e. a positive lookahead (.+(?=/foo/)), the generator will not accept the parameter as the parameter itself cannot fulfil the requirement, but only matches in the context of the entire path.This fix looks for lookAround in the path and ignores checking the requirements if any lookAround exists.Commits-------c474451 [Routing] fix URL generation with look-around requirements
2 parents4b56617 +c474451 commit5ac4c2a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

‎src/Symfony/Component/Routing/Generator/UrlGenerator.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
140140
foreach ($tokensas$token) {
141141
if ('variable' ===$token[0]) {
142142
if (!$optional || !array_key_exists($token[3],$defaults) ||null !==$mergedParams[$token[3]] && (string)$mergedParams[$token[3]] !== (string)$defaults[$token[3]]) {
143-
// check requirement
144-
if (null !==$this->strictRequirements && !preg_match('#^'.$token[2].'$#'.(empty($token[4]) ?'' :'u'),$mergedParams[$token[3]])) {
143+
// check requirement (while ignoring look-around patterns)
144+
if (null !==$this->strictRequirements && !preg_match('#^'.preg_replace('/\(\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\((?1)\))*)\)/','',$token[2]).'$#'.(empty($token[4]) ?'' :'u'),$mergedParams[$token[3]])) {
145145
if ($this->strictRequirements) {
146146
thrownewInvalidParameterException(strtr($message, ['{parameter}' =>$token[3],'{route}' =>$name,'{expected}' =>$token[2],'{given}' =>$mergedParams[$token[3]]]));
147147
}
@@ -195,7 +195,8 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
195195
$routeHost ='';
196196
foreach ($hostTokensas$token) {
197197
if ('variable' ===$token[0]) {
198-
if (null !==$this->strictRequirements && !preg_match('#^'.$token[2].'$#i'.(empty($token[4]) ?'' :'u'),$mergedParams[$token[3]])) {
198+
// check requirement (while ignoring look-around patterns)
199+
if (null !==$this->strictRequirements && !preg_match('#^'.preg_replace('/\(\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\((?1)\))*)\)/','',$token[2]).'$#i'.(empty($token[4]) ?'' :'u'),$mergedParams[$token[3]])) {
199200
if ($this->strictRequirements) {
200201
thrownewInvalidParameterException(strtr($message, ['{parameter}' =>$token[3],'{route}' =>$name,'{expected}' =>$token[2],'{given}' =>$mergedParams[$token[3]]]));
201202
}

‎src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,23 @@ public function testFragmentsCanBeDefinedAsDefaults()
703703
$this->assertEquals('/app.php/testing#fragment',$url);
704704
}
705705

706+
/**
707+
* @dataProvider provideLookAroundRequirementsInPath
708+
*/
709+
publicfunctiontestLookRoundRequirementsInPath($expected,$path,$requirement)
710+
{
711+
$routes =$this->getRoutes('test',newRoute($path,array(),array('foo' =>$requirement,'baz' =>'.+?')));
712+
$this->assertSame($expected,$this->getGenerator($routes)->generate('test',array('foo' =>'a/b','baz' =>'c/d/e')));
713+
}
714+
715+
publicfunctionprovideLookAroundRequirementsInPath()
716+
{
717+
yieldarray('/app.php/a/b/b%28ar/c/d/e','/{foo}/b(ar/{baz}','.+(?=/b\\(ar/)');
718+
yieldarray('/app.php/a/b/bar/c/d/e','/{foo}/bar/{baz}','.+(?!$)');
719+
yieldarray('/app.php/bar/a/b/bam/c/d/e','/bar/{foo}/bam/{baz}','(?<=/bar/).+');
720+
yieldarray('/app.php/bar/a/b/bam/c/d/e','/bar/{foo}/bam/{baz}','(?<!^).+');
721+
}
722+
706723
protectedfunctiongetGenerator(RouteCollection$routes,array$parameters = [],$logger =null)
707724
{
708725
$context =newRequestContext('/app.php');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp