EnvPlaceholderParameterBag.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\ParameterBag;
  11. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class EnvPlaceholderParameterBag extends ParameterBag
  17. {
  18. private $envPlaceholders = array();
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function get($name)
  23. {
  24. if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
  25. $env = substr($name, 4, -1);
  26. if (isset($this->envPlaceholders[$env])) {
  27. foreach ($this->envPlaceholders[$env] as $placeholder) {
  28. return $placeholder; // return first result
  29. }
  30. }
  31. if (preg_match('/\W/', $env)) {
  32. throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
  33. }
  34. if ($this->has($name)) {
  35. $defaultValue = parent::get($name);
  36. if (null !== $defaultValue && !is_scalar($defaultValue)) {
  37. throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', gettype($defaultValue), $name));
  38. }
  39. }
  40. $uniqueName = md5($name.uniqid(mt_rand(), true));
  41. $placeholder = sprintf('env_%s_%s', $env, $uniqueName);
  42. $this->envPlaceholders[$env][$placeholder] = $placeholder;
  43. return $placeholder;
  44. }
  45. return parent::get($name);
  46. }
  47. /**
  48. * Returns the map of env vars used in the resolved parameter values to their placeholders.
  49. *
  50. * @return string[][] A map of env var names to their placeholders
  51. */
  52. public function getEnvPlaceholders()
  53. {
  54. return $this->envPlaceholders;
  55. }
  56. /**
  57. * Merges the env placeholders of another EnvPlaceholderParameterBag.
  58. */
  59. public function mergeEnvPlaceholders(self $bag)
  60. {
  61. if ($newPlaceholders = $bag->getEnvPlaceholders()) {
  62. $this->envPlaceholders += $newPlaceholders;
  63. foreach ($newPlaceholders as $env => $placeholders) {
  64. $this->envPlaceholders[$env] += $placeholders;
  65. }
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function resolve()
  72. {
  73. if ($this->resolved) {
  74. return;
  75. }
  76. parent::resolve();
  77. foreach ($this->envPlaceholders as $env => $placeholders) {
  78. if (!isset($this->parameters[$name = strtolower("env($env)")])) {
  79. continue;
  80. }
  81. if (is_numeric($default = $this->parameters[$name])) {
  82. $this->parameters[$name] = (string) $default;
  83. } elseif (null !== $default && !is_scalar($default)) {
  84. throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, gettype($default)));
  85. }
  86. }
  87. }
  88. }