Skip to content

[DependencyInjection] Make Container::getEnv public #46819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ private function getContainerEnvVars(ContainerBuilder $container): array
};
$getDefaultParameter = $getDefaultParameter->bindTo($bag, \get_class($bag));

$getEnvReflection = new \ReflectionMethod($container, 'getEnv');

$envs = [];

foreach ($envVars as $env) {
Expand All @@ -343,7 +341,7 @@ private function getContainerEnvVars(ContainerBuilder $container): array
if (false === ($runtimeValue = $_ENV[$name] ?? $_SERVER[$name] ?? getenv($name))) {
$runtimeValue = null;
}
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $container->getEnv($env) : null;
$envs["$name$processor"] = [
'name' => $name,
'processor' => $processor,
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected function load(string $file)
*
* @throws EnvNotFoundException When the environment variable is not found and has no default value
*/
protected function getEnv(string $name): mixed
public function getEnv(string $name): mixed
{
if (isset($this->resolving[$envName = "env($name)"])) {
throw new ParameterCircularReferenceException(array_keys($this->resolving));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ public static function hash(mixed $value): string
/**
* {@inheritdoc}
*/
protected function getEnv(string $name): mixed
public function getEnv(string $name): mixed
{
$value = parent::getEnv($name);
$bag = $this->getParameterBag();
Expand Down Expand Up @@ -1612,7 +1612,7 @@ private function getExpressionLanguage(): ExpressionLanguage
if (!class_exists(Expression::class)) {
throw new LogicException('Expressions cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".');
}
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders, null, $this->getEnv(...));
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders, null);
}

return $this->expressionLanguage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
/**
* ContainerInterface is the interface implemented by service container classes.
*
* @method mixed getEnv Fetches a variable from the environment.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class ExpressionLanguage extends BaseExpressionLanguage
/**
* {@inheritdoc}
*/
public function __construct(CacheItemPoolInterface $cache = null, array $providers = [], callable $serviceCompiler = null, \Closure $getEnv = null)
public function __construct(CacheItemPoolInterface $cache = null, array $providers = [], callable $serviceCompiler = null)
{
// prepend the default provider to let users override it easily
array_unshift($providers, new ExpressionLanguageProvider($serviceCompiler, $getEnv));
array_unshift($providers, new ExpressionLanguageProvider($serviceCompiler));

parent::__construct($cache, $providers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
{
private ?\Closure $serviceCompiler;

private ?\Closure $getEnv;

public function __construct(callable $serviceCompiler = null, \Closure $getEnv = null)
public function __construct(callable $serviceCompiler = null)
{
$this->serviceCompiler = null === $serviceCompiler ? null : $serviceCompiler(...);
$this->getEnv = $getEnv;
}

public function getFunctions(): array
Expand All @@ -54,11 +51,7 @@ public function getFunctions(): array
new ExpressionFunction('env', function ($arg) {
return sprintf('$this->getEnv(%s)', $arg);
}, function (array $variables, $value) {
if (!$this->getEnv) {
throw new LogicException('You need to pass a getEnv closure to the expression langage provider to use the "env" function.');
}

return ($this->getEnv)($value);
return $variables['container']->getEnv($value);
}),

new ExpressionFunction('arg', function ($arg) {
Expand Down