Skip to content

[DependencyInjection] Add an env function to DI expression language #45450

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

Merged
merged 1 commit into from
Feb 18, 2022
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `$exclude` to `TaggedIterator` and `TaggedLocator` attributes
* Add `$exclude` to `tagged_iterator` and `tagged_locator` configurator
* Add an `env` function to the expression language provider

6.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ private function getExpressionLanguage(): ExpressionLanguage
if (!class_exists(\Symfony\Component\ExpressionLanguage\ExpressionLanguage::class)) {
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders, null, \Closure::fromCallable([$this, 'getEnv']));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only way I found to allow the expression language to fetch an env variable outside of a dumped container because of the getEnv visibility but maybe we could make the method public instead.

}

return $this->expressionLanguage;
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)
public function __construct(CacheItemPoolInterface $cache = null, array $providers = [], callable $serviceCompiler = null, \Closure $getEnv = null)
{
// prepend the default provider to let users override it easily
array_unshift($providers, new ExpressionLanguageProvider($serviceCompiler));
array_unshift($providers, new ExpressionLanguageProvider($serviceCompiler, $getEnv));

parent::__construct($cache, $providers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection;

use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;

Expand All @@ -19,16 +20,20 @@
*
* To get a service, use service('request').
* To get a parameter, use parameter('kernel.debug').
* To get an env variable, use env('SOME_VARIABLE').
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
{
private ?\Closure $serviceCompiler;

public function __construct(callable $serviceCompiler = null)
private ?\Closure $getEnv;

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

public function getFunctions(): array
Expand All @@ -45,6 +50,16 @@ public function getFunctions(): array
}, function (array $variables, $value) {
return $variables['container']->getParameter($value);
}),

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);
}),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,19 @@ public function testCreateServiceWithExpression()
$this->assertEquals('foobar', $builder->get('foo')->arguments['foo']);
}

public function testEnvExpressionFunction()
{
$container = new ContainerBuilder();
$container->register('bar', 'BarClass')
->setPublic(true)
->setProperty('foo', new Expression('env("BAR_FOO")'));
$container->compile(true);

$_ENV['BAR_FOO'] = 'Foo value';

$this->assertEquals('Foo value', $container->get('bar')->foo);
}

public function testCreateServiceWithAbstractArgument()
{
$this->expectException(RuntimeException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,24 @@ public function testPrivateWithIgnoreOnInvalidReference()
$this->assertInstanceOf(\BazClass::class, $container->get('bar')->getBaz());
}

public function testEnvExpressionFunction()
{
$container = new ContainerBuilder();
$container->register('bar', 'BarClass')
->setPublic(true)
->setProperty('foo', new Expression('env("BAR_FOO")'));
$container->compile();

$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Env_Expression_Function']));

$container = new \Symfony_DI_PhpDumper_Test_Env_Expression_Function();

$_ENV['BAR_FOO'] = 'Foo value';

$this->assertEquals('Foo value', $container->get('bar')->foo);
}

public function testArrayParameters()
{
$container = new ContainerBuilder();
Expand Down