[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/ParameterBag/ -> EnvPlaceholderParameterBag.php (source)

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


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1