vendor/symfony/dependency-injection/ParameterBag/EnvPlaceholderParameterBag.php line 162

Open in your IDE?
  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($name)
  27.     {
  28.         if (str_starts_with($name'env(') && str_ends_with($name')') && 'env()' !== $name) {
  29.             $env substr($name4, -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)) {
  44.                 $defaultValue parent::get($name);
  45.                 if (null !== $defaultValue && !is_scalar($defaultValue)) { // !is_string in 5.0
  46.                     //throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
  47.                     throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
  48.                 } elseif (is_scalar($defaultValue) && !\is_string($defaultValue)) {
  49.                     @trigger_error(sprintf('A non-string default value of an env() parameter is deprecated since 4.3, cast "%s" to string instead.'$name), \E_USER_DEPRECATED);
  50.                 }
  51.             }
  52.             $uniqueName md5($name.'_'.self::$counter++);
  53.             $placeholder sprintf('%s_%s_%s'$this->getEnvPlaceholderUniquePrefix(), str_replace(':''_'$env), $uniqueName);
  54.             $this->envPlaceholders[$env][$placeholder] = $placeholder;
  55.             return $placeholder;
  56.         }
  57.         return parent::get($name);
  58.     }
  59.     /**
  60.      * Gets the common env placeholder prefix for env vars created by this bag.
  61.      */
  62.     public function getEnvPlaceholderUniquePrefix(): string
  63.     {
  64.         if (null === $this->envPlaceholderUniquePrefix) {
  65.             $reproducibleEntropy unserialize(serialize($this->parameters));
  66.             array_walk_recursive($reproducibleEntropy, function (&$v) { $v null; });
  67.             $this->envPlaceholderUniquePrefix 'env_'.substr(md5(serialize($reproducibleEntropy)), -16);
  68.         }
  69.         return $this->envPlaceholderUniquePrefix;
  70.     }
  71.     /**
  72.      * Returns the map of env vars used in the resolved parameter values to their placeholders.
  73.      *
  74.      * @return string[][] A map of env var names to their placeholders
  75.      */
  76.     public function getEnvPlaceholders()
  77.     {
  78.         return $this->envPlaceholders;
  79.     }
  80.     public function getUnusedEnvPlaceholders(): array
  81.     {
  82.         return $this->unusedEnvPlaceholders;
  83.     }
  84.     public function clearUnusedEnvPlaceholders()
  85.     {
  86.         $this->unusedEnvPlaceholders = [];
  87.     }
  88.     /**
  89.      * Merges the env placeholders of another EnvPlaceholderParameterBag.
  90.      */
  91.     public function mergeEnvPlaceholders(self $bag)
  92.     {
  93.         if ($newPlaceholders $bag->getEnvPlaceholders()) {
  94.             $this->envPlaceholders += $newPlaceholders;
  95.             foreach ($newPlaceholders as $env => $placeholders) {
  96.                 $this->envPlaceholders[$env] += $placeholders;
  97.             }
  98.         }
  99.         if ($newUnusedPlaceholders $bag->getUnusedEnvPlaceholders()) {
  100.             $this->unusedEnvPlaceholders += $newUnusedPlaceholders;
  101.             foreach ($newUnusedPlaceholders as $env => $placeholders) {
  102.                 $this->unusedEnvPlaceholders[$env] += $placeholders;
  103.             }
  104.         }
  105.     }
  106.     /**
  107.      * Maps env prefixes to their corresponding PHP types.
  108.      */
  109.     public function setProvidedTypes(array $providedTypes)
  110.     {
  111.         $this->providedTypes $providedTypes;
  112.     }
  113.     /**
  114.      * Gets the PHP types corresponding to env() parameter prefixes.
  115.      *
  116.      * @return string[][]
  117.      */
  118.     public function getProvidedTypes()
  119.     {
  120.         return $this->providedTypes;
  121.     }
  122.     /**
  123.      * {@inheritdoc}
  124.      */
  125.     public function resolve()
  126.     {
  127.         if ($this->resolved) {
  128.             return;
  129.         }
  130.         parent::resolve();
  131.         foreach ($this->envPlaceholders as $env => $placeholders) {
  132.             if (!$this->has($name "env($env)")) {
  133.                 continue;
  134.             }
  135.             if (is_numeric($default $this->parameters[$name])) {
  136.                 if (!\is_string($default)) {
  137.                     @trigger_error(sprintf('A non-string default value of env parameter "%s" is deprecated since 4.3, cast it to string instead.'$env), \E_USER_DEPRECATED);
  138.                 }
  139.                 $this->parameters[$name] = (string) $default;
  140.             } elseif (null !== $default && !is_scalar($default)) { // !is_string in 5.0
  141.                 //throw new RuntimeException(sprintf('The default value of env parameter "%s" must be a string or null, "%s" given.', $env, \gettype($default)));
  142.                 throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, "%s" given.'$env, \gettype($default)));
  143.             } elseif (is_scalar($default) && !\is_string($default)) {
  144.                 @trigger_error(sprintf('A non-string default value of env parameter "%s" is deprecated since 4.3, cast it to string instead.'$env), \E_USER_DEPRECATED);
  145.             }
  146.         }
  147.     }
  148. }