EnvPlaceholderParameterBag.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\InvalidArgumentException;
  12. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class EnvPlaceholderParameterBag extends ParameterBag
  17. {
  18. private $envPlaceholderUniquePrefix;
  19. private $envPlaceholders = [];
  20. private $unusedEnvPlaceholders = [];
  21. private $providedTypes = [];
  22. private static $counter = 0;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function get(string $name)
  27. {
  28. if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
  29. $env = substr($name, 4, -1);
  30. if (isset($this->envPlaceholders[$env])) {
  31. foreach ($this->envPlaceholders[$env] as $placeholder) {
  32. return $placeholder; // return first result
  33. }
  34. }
  35. if (isset($this->unusedEnvPlaceholders[$env])) {
  36. foreach ($this->unusedEnvPlaceholders[$env] as $placeholder) {
  37. return $placeholder; // return first result
  38. }
  39. }
  40. if (!preg_match('/^(?:[-.\w]*+:)*+\w++$/', $env)) {
  41. throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
  42. }
  43. if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) {
  44. throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', get_debug_type($defaultValue), $name));
  45. }
  46. $uniqueName = md5($name.'_'.self::$counter++);
  47. $placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), strtr($env, ':-.', '___'), $uniqueName);
  48. $this->envPlaceholders[$env][$placeholder] = $placeholder;
  49. return $placeholder;
  50. }
  51. return parent::get($name);
  52. }
  53. /**
  54. * Gets the common env placeholder prefix for env vars created by this bag.
  55. */
  56. public function getEnvPlaceholderUniquePrefix(): string
  57. {
  58. if (null === $this->envPlaceholderUniquePrefix) {
  59. $reproducibleEntropy = unserialize(serialize($this->parameters));
  60. array_walk_recursive($reproducibleEntropy, function (&$v) { $v = null; });
  61. $this->envPlaceholderUniquePrefix = 'env_'.substr(md5(serialize($reproducibleEntropy)), -16);
  62. }
  63. return $this->envPlaceholderUniquePrefix;
  64. }
  65. /**
  66. * Returns the map of env vars used in the resolved parameter values to their placeholders.
  67. *
  68. * @return string[][] A map of env var names to their placeholders
  69. */
  70. public function getEnvPlaceholders()
  71. {
  72. return $this->envPlaceholders;
  73. }
  74. public function getUnusedEnvPlaceholders(): array
  75. {
  76. return $this->unusedEnvPlaceholders;
  77. }
  78. public function clearUnusedEnvPlaceholders()
  79. {
  80. $this->unusedEnvPlaceholders = [];
  81. }
  82. /**
  83. * Merges the env placeholders of another EnvPlaceholderParameterBag.
  84. */
  85. public function mergeEnvPlaceholders(self $bag)
  86. {
  87. if ($newPlaceholders = $bag->getEnvPlaceholders()) {
  88. $this->envPlaceholders += $newPlaceholders;
  89. foreach ($newPlaceholders as $env => $placeholders) {
  90. $this->envPlaceholders[$env] += $placeholders;
  91. }
  92. }
  93. if ($newUnusedPlaceholders = $bag->getUnusedEnvPlaceholders()) {
  94. $this->unusedEnvPlaceholders += $newUnusedPlaceholders;
  95. foreach ($newUnusedPlaceholders as $env => $placeholders) {
  96. $this->unusedEnvPlaceholders[$env] += $placeholders;
  97. }
  98. }
  99. }
  100. /**
  101. * Maps env prefixes to their corresponding PHP types.
  102. */
  103. public function setProvidedTypes(array $providedTypes)
  104. {
  105. $this->providedTypes = $providedTypes;
  106. }
  107. /**
  108. * Gets the PHP types corresponding to env() parameter prefixes.
  109. *
  110. * @return string[][]
  111. */
  112. public function getProvidedTypes()
  113. {
  114. return $this->providedTypes;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function resolve()
  120. {
  121. if ($this->resolved) {
  122. return;
  123. }
  124. parent::resolve();
  125. foreach ($this->envPlaceholders as $env => $placeholders) {
  126. if ($this->has($name = "env($env)") && null !== ($default = $this->parameters[$name]) && !\is_string($default)) {
  127. throw new RuntimeException(sprintf('The default value of env parameter "%s" must be a string or null, "%s" given.', $env, get_debug_type($default)));
  128. }
  129. }
  130. }
  131. }