[DependencyInjection] Fix duplication of placeholders

This commit is contained in:
Michael Devery 2016-10-11 04:25:54 +01:00 committed by Fabien Potencier
parent a5d134b136
commit 124f30d4c6
2 changed files with 23 additions and 2 deletions

View File

@ -30,7 +30,9 @@ class EnvPlaceholderParameterBag extends ParameterBag
$env = substr($name, 4, -1);
if (isset($this->envPlaceholders[$env])) {
return $this->envPlaceholders[$env][0];
foreach ($this->envPlaceholders[$env] as $placeholder) {
return $placeholder; // return first result
}
}
if (preg_match('/\W/', $env)) {
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
@ -44,7 +46,11 @@ class EnvPlaceholderParameterBag extends ParameterBag
}
}
return $this->envPlaceholders[$env][] = sprintf('env_%s_%s', $env, md5($name.uniqid(mt_rand(), true)));
$uniqueName = md5($name.uniqid(mt_rand(), true));
$placeholder = sprintf('env_%s_%s', $env, $uniqueName);
$this->envPlaceholders[$env][$placeholder] = $placeholder;
return $placeholder;
}
return parent::get($name);

View File

@ -23,4 +23,19 @@ class EnvPlaceholderParameterBagTest extends \PHPUnit_Framework_TestCase
$bag = new EnvPlaceholderParameterBag();
$bag->get('env(%foo%)');
}
public function testMergeWillNotDuplicateIdenticalParameters()
{
$originalPlaceholders = array('database_host' => array('localhost'));
$firstBag = new EnvPlaceholderParameterBag($originalPlaceholders);
// initialize placeholders
$firstBag->get('env(database_host)');
$secondBag = clone $firstBag;
$firstBag->mergeEnvPlaceholders($secondBag);
$mergedPlaceholders = $firstBag->getEnvPlaceholders();
$this->assertCount(1, $mergedPlaceholders['database_host']);
}
}