[DependencyInjection] Fix duplication of placeholders
This commit is contained in:
parent
a5d134b136
commit
124f30d4c6
@ -30,7 +30,9 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
|||||||
$env = substr($name, 4, -1);
|
$env = substr($name, 4, -1);
|
||||||
|
|
||||||
if (isset($this->envPlaceholders[$env])) {
|
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)) {
|
if (preg_match('/\W/', $env)) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
|
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);
|
return parent::get($name);
|
||||||
|
@ -23,4 +23,19 @@ class EnvPlaceholderParameterBagTest extends \PHPUnit_Framework_TestCase
|
|||||||
$bag = new EnvPlaceholderParameterBag();
|
$bag = new EnvPlaceholderParameterBag();
|
||||||
$bag->get('env(%foo%)');
|
$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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user