8000 [DependencyInjection] Fix duplication of placeholders · symfony/symfony@124f30d · GitHub
[go: up one dir, main page]

Skip to content

Commit 124f30d

Browse files
mickadoofabpot
authored andcommitted
[DependencyInjection] Fix duplication of placeholders
1 parent a5d134b commit 124f30d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public function get($name)
3030
$env = substr($name, 4, -1);
3131

3232
if (isset($this->envPlaceholders[$env])) {
33-
return $this->envPlaceholders[$env][0];
33+
foreach ($this->envPlaceholders[$env] as $placeholder) {
34+
return $placeholder; // return first result
35+
}
3436
}
3537
if (preg_match('/\W/', $env)) {
3638
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
@@ -44,7 +46,11 @@ public function get($name)
4446
}
4547
}
4648

47-
return $this->envPlaceholders[$env][] = sprintf('env_%s_%s', $env, md5($name.uniqid(mt_rand(), true)));
49+
$uniqueName = md5($name.uniqid(mt_rand(), true));
50+
$placeholder = sprintf('env_%s_%s', $env, $uniqueName);
51+
$this->envPlaceholders[$env][$placeholder] = $placeholder;
52+
53+
return $placeholder;
4854
}
4955

5056
return parent::get($name);

src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@ public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCha
2323
$bag = new EnvPlaceholderParameterBag();
2424
$bag->get('env(%foo%)');
2525
}
26+
27+
public function testMergeWillNotDuplicateIdenticalParameters()
28+
{
29+
$originalPlaceholders = array('database_host' => array('localhost'));
30+
$firstBag = new EnvPlaceholderParameterBag($originalPlaceholders);
31+
32+
// initialize placeholders
33+
$firstBag->get('env(database_host)');
34+
$secondBag = clone $firstBag;
35+
36+
$firstBag->mergeEnvPlaceholders($secondBag);
37+
$mergedPlaceholders = $firstBag->getEnvPlaceholders();
38+
39+
$this->assertCount(1, $mergedPlaceholders['database_host']);
40+
}
2641
}

0 commit comments

Comments
 (0)
0