function ContainerBuilder::resolveEnvPlaceholders
Resolves env parameter placeholders in a string or an array.
Parameters
string|true|null $format A sprintf() format returning the replacement for each env var name or: null to resolve back to the original "%env(VAR)%" format or true to resolve to the actual values of the referenced env vars
array &$usedEnvs Env vars found while resolving are added to this array:
Return value
mixed The value with env parameters resolved if a string or an array is passed
5 calls to ContainerBuilder::resolveEnvPlaceholders()
- ContainerBuilder::compile in vendor/
symfony/ dependency-injection/ ContainerBuilder.php - Compiles the container.
- ContainerBuilder::getEnv in vendor/
symfony/ dependency-injection/ ContainerBuilder.php - Fetches a variable from the environment.
- ContainerBuilder::log in vendor/
symfony/ dependency-injection/ ContainerBuilder.php - @final
- MergeExtensionConfigurationContainerBuilder::resolveEnvPlaceholders in vendor/
symfony/ dependency-injection/ Compiler/ MergeExtensionConfigurationPass.php - Resolves env parameter placeholders in a string or an array.
- MergeExtensionConfigurationContainerBuilder::resolveEnvPlaceholders in vendor/
symfony/ dependency-injection/ Compiler/ MergeExtensionConfigurationPass.php - Resolves env parameter placeholders in a string or an array.
1 method overrides ContainerBuilder::resolveEnvPlaceholders()
- MergeExtensionConfigurationContainerBuilder::resolveEnvPlaceholders in vendor/
symfony/ dependency-injection/ Compiler/ MergeExtensionConfigurationPass.php - Resolves env parameter placeholders in a string or an array.
File
-
vendor/
symfony/ dependency-injection/ ContainerBuilder.php, line 1477
Class
- ContainerBuilder
- ContainerBuilder is a DI container that provides an API to easily describe services.
Namespace
Symfony\Component\DependencyInjectionCode
public function resolveEnvPlaceholders(mixed $value, string|bool|null $format = null, ?array &$usedEnvs = null) : mixed {
$bag = $this->getParameterBag();
if (true === ($format ??= '%%env(%s)%%')) {
$value = $bag->resolveValue($value);
}
if ($value instanceof Definition) {
$value = (array) $value;
}
if (\is_array($value)) {
$result = [];
foreach ($value as $k => $v) {
$result[\is_string($k) ? $this->resolveEnvPlaceholders($k, $format, $usedEnvs) : $k] = $this->resolveEnvPlaceholders($v, $format, $usedEnvs);
}
return $result;
}
if (!\is_string($value) || 38 > \strlen($value) || false === stripos($value, 'env_')) {
return $value;
}
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
$completed = false;
preg_match_all('/env_[a-f0-9]{16}_\\w+_[a-f0-9]{32}/Ui', $value, $matches);
$usedPlaceholders = array_flip($matches[0]);
foreach ($envPlaceholders as $env => $placeholders) {
foreach ($placeholders as $placeholder) {
if (isset($usedPlaceholders[$placeholder])) {
if (true === $format) {
$resolved = $bag->escapeValue($this->getEnv($env));
}
else {
$resolved = \sprintf($format, $env);
}
if ($placeholder === $value) {
$value = $resolved;
$completed = true;
}
else {
if (!\is_string($resolved) && !is_numeric($resolved)) {
throw new RuntimeException(\sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type "%s" inside string value "%s".', $env, get_debug_type($resolved), $this->resolveEnvPlaceholders($value)));
}
$value = str_ireplace($placeholder, $resolved, $value);
}
$usedEnvs[$env] = $env;
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
if ($completed) {
break 2;
}
}
}
}
return $value;
}