Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag::get PHP Method

get() public method

public get ( $name )
    public function get($name)
    {
        if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
            $env = substr($name, 4, -1);
            if (isset($this->envPlaceholders[$env])) {
                foreach ($this->envPlaceholders[$env] as $placeholder) {
                    return $placeholder;
                    // return first result
                }
            }
            if (preg_match('/\\W/', $env)) {
                throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
            }
            if ($this->has($name)) {
                $defaultValue = parent::get($name);
                if (null !== $defaultValue && !is_scalar($defaultValue)) {
                    throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', gettype($defaultValue), $name));
                }
            }
            $uniqueName = md5($name . uniqid(mt_rand(), true));
            $placeholder = sprintf('env_%s_%s', $env, $uniqueName);
            $this->envPlaceholders[$env][$placeholder] = $placeholder;
            return $placeholder;
        }
        return parent::get($name);
    }

Usage Example

 public function testMergeWithDifferentIdentifiersForPlaceholders()
 {
     $envName = 'DB_USER';
     $paramName = sprintf('env(%s)', $envName);
     $firstBag = new EnvPlaceholderParameterBag();
     $secondBag = new EnvPlaceholderParameterBag();
     // initialize placeholders
     $firstPlaceholder = $firstBag->get($paramName);
     $secondPlaceholder = $secondBag->get($paramName);
     $firstBag->mergeEnvPlaceholders($secondBag);
     $merged = $firstBag->getEnvPlaceholders();
     $this->assertNotEquals($firstPlaceholder, $secondPlaceholder);
     $this->assertCount(2, $merged[$envName]);
 }
All Usage Examples Of Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag::get