Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. EnvPlaceholderParameterBag.php

class EnvPlaceholderParameterBag

@author Nicolas Grekas <p@tchwork.com>

Hierarchy

  • class \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag implements \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface
    • class \Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag extends \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag

Expanded class hierarchy of EnvPlaceholderParameterBag

6 files declare their use of EnvPlaceholderParameterBag
CheckTypeDeclarationsPass.php in vendor/symfony/dependency-injection/Compiler/CheckTypeDeclarationsPass.php
Container.php in vendor/symfony/dependency-injection/Container.php
ContainerBuilder.php in vendor/symfony/dependency-injection/ContainerBuilder.php
MergeExtensionConfigurationPass.php in vendor/symfony/dependency-injection/Compiler/MergeExtensionConfigurationPass.php
RegisterEnvVarProcessorsPass.php in vendor/symfony/dependency-injection/Compiler/RegisterEnvVarProcessorsPass.php

... See full list

File

vendor/symfony/dependency-injection/ParameterBag/EnvPlaceholderParameterBag.php, line 20

Namespace

Symfony\Component\DependencyInjection\ParameterBag
View source
class EnvPlaceholderParameterBag extends ParameterBag {
    private string $envPlaceholderUniquePrefix;
    private array $envPlaceholders = [];
    private array $unusedEnvPlaceholders = [];
    private array $providedTypes = [];
    private static int $counter = 0;
    public function get(string $name) : array|bool|string|int|float|\UnitEnum|null {
        if (str_starts_with($name, 'env(') && str_ends_with($name, ')') && 'env()' !== $name) {
            $env = substr($name, 4, -1);
            if (isset($this->envPlaceholders[$env])) {
                foreach ($this->envPlaceholders[$env] as $placeholder) {
                    return $placeholder;
                    // return first result
                }
            }
            if (isset($this->unusedEnvPlaceholders[$env])) {
                foreach ($this->unusedEnvPlaceholders[$env] as $placeholder) {
                    return $placeholder;
                    // return first result
                }
            }
            if (!preg_match('/^(?:[-.\\w\\\\]*+:)*+\\w*+$/', $env)) {
                throw new InvalidArgumentException(\sprintf('The given env var name "%s" contains invalid characters (allowed characters: letters, digits, hyphens, backslashes and colons).', $name));
            }
            if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) {
                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));
            }
            $uniqueName = hash('xxh128', $name . '_' . self::$counter++);
            $placeholder = \sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), strtr($env, ':-.\\', '____'), $uniqueName);
            $this->envPlaceholders[$env][$placeholder] = $placeholder;
            return $placeholder;
        }
        return parent::get($name);
    }
    
    /**
     * Gets the common env placeholder prefix for env vars created by this bag.
     */
    public function getEnvPlaceholderUniquePrefix() : string {
        if (!isset($this->envPlaceholderUniquePrefix)) {
            $reproducibleEntropy = unserialize(serialize($this->parameters));
            array_walk_recursive($reproducibleEntropy, function (&$v) {
                $v = null;
            });
            $this->envPlaceholderUniquePrefix = 'env_' . substr(hash('xxh128', serialize($reproducibleEntropy)), -16);
        }
        return $this->envPlaceholderUniquePrefix;
    }
    
    /**
     * Returns the map of env vars used in the resolved parameter values to their placeholders.
     *
     * @return string[][] A map of env var names to their placeholders
     */
    public function getEnvPlaceholders() : array {
        return $this->envPlaceholders;
    }
    public function getUnusedEnvPlaceholders() : array {
        return $this->unusedEnvPlaceholders;
    }
    public function clearUnusedEnvPlaceholders() : void {
        $this->unusedEnvPlaceholders = [];
    }
    
    /**
     * Merges the env placeholders of another EnvPlaceholderParameterBag.
     */
    public function mergeEnvPlaceholders(self $bag) : void {
        if ($newPlaceholders = $bag->getEnvPlaceholders()) {
            $this->envPlaceholders += $newPlaceholders;
            foreach ($newPlaceholders as $env => $placeholders) {
                $this->envPlaceholders[$env] += $placeholders;
            }
        }
        if ($newUnusedPlaceholders = $bag->getUnusedEnvPlaceholders()) {
            $this->unusedEnvPlaceholders += $newUnusedPlaceholders;
            foreach ($newUnusedPlaceholders as $env => $placeholders) {
                $this->unusedEnvPlaceholders[$env] += $placeholders;
            }
        }
    }
    
    /**
     * Maps env prefixes to their corresponding PHP types.
     */
    public function setProvidedTypes(array $providedTypes) : void {
        $this->providedTypes = $providedTypes;
    }
    
    /**
     * Gets the PHP types corresponding to env() parameter prefixes.
     *
     * @return string[][]
     */
    public function getProvidedTypes() : array {
        return $this->providedTypes;
    }
    public function resolve() : void {
        if ($this->resolved) {
            return;
        }
        parent::resolve();
        foreach ($this->envPlaceholders as $env => $placeholders) {
            if ($this->has($name = "env({$env})") && null !== ($default = $this->parameters[$name]) && !\is_string($default)) {
                throw new RuntimeException(\sprintf('The default value of env parameter "%s" must be a string or null, "%s" given.', $env, get_debug_type($default)));
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
EnvPlaceholderParameterBag::$counter private static property
EnvPlaceholderParameterBag::$envPlaceholders private property
EnvPlaceholderParameterBag::$envPlaceholderUniquePrefix private property
EnvPlaceholderParameterBag::$providedTypes private property
EnvPlaceholderParameterBag::$unusedEnvPlaceholders private property
EnvPlaceholderParameterBag::clearUnusedEnvPlaceholders public function
EnvPlaceholderParameterBag::get public function Gets a service container parameter. Overrides ParameterBag::get
EnvPlaceholderParameterBag::getEnvPlaceholders public function Returns the map of env vars used in the resolved parameter values to their placeholders. 1
EnvPlaceholderParameterBag::getEnvPlaceholderUniquePrefix public function Gets the common env placeholder prefix for env vars created by this bag.
EnvPlaceholderParameterBag::getProvidedTypes public function Gets the PHP types corresponding to env() parameter prefixes.
EnvPlaceholderParameterBag::getUnusedEnvPlaceholders public function 1
EnvPlaceholderParameterBag::mergeEnvPlaceholders public function Merges the env placeholders of another EnvPlaceholderParameterBag.
EnvPlaceholderParameterBag::resolve public function Replaces parameter placeholders (%name%) by their values for all parameters. Overrides ParameterBag::resolve
EnvPlaceholderParameterBag::setProvidedTypes public function Maps env prefixes to their corresponding PHP types.
ParameterBag::$deprecatedParameters protected property
ParameterBag::$nonEmptyParameters protected property
ParameterBag::$parameters protected property
ParameterBag::$resolved protected property
ParameterBag::add public function Adds parameters to the service container parameters. Overrides ParameterBagInterface::add 1
ParameterBag::all public function Gets the service container parameters. Overrides ParameterBagInterface::all 1
ParameterBag::allDeprecated public function
ParameterBag::allNonEmpty public function
ParameterBag::cannotBeEmpty public function 1
ParameterBag::clear public function Clears all parameters. Overrides ParameterBagInterface::clear 1
ParameterBag::deprecate public function Deprecates a service container parameter. 1
ParameterBag::escapeValue public function Escape parameter placeholders %. Overrides ParameterBagInterface::escapeValue
ParameterBag::has public function Returns true if a parameter name is defined. Overrides ParameterBagInterface::has 1
ParameterBag::isResolved public function
ParameterBag::remove public function Removes a parameter. Overrides ParameterBagInterface::remove 1
ParameterBag::resolveString public function Resolves parameters inside a string.
ParameterBag::resolveValue public function Replaces parameter placeholders (%name%) by their values. Overrides ParameterBagInterface::resolveValue
ParameterBag::set public function Sets a service container parameter. Overrides ParameterBagInterface::set 1
ParameterBag::unescapeValue public function Unescape parameter placeholders %. Overrides ParameterBagInterface::unescapeValue
ParameterBag::__construct public function 2
RSS feed
Powered by Drupal