Skip to content
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

[DependencyInjection] Add defined prefix for env var processor #50791

Merged
merged 1 commit into from
Jun 28, 2023
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.
Jump to
Jump to file
Failed to load files.
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 @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate `ContainerAwareInterface` and `ContainerAwareTrait`, use dependency injection instead
* Add `defined` env var processor that returns `true` for defined and neither null nor empty env vars

6.3
---
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function getProvidedTypes(): array
'require' => 'bool|int|float|string|array',
'enum' => \BackedEnum::class,
'shuffle' => 'array',
'defined' => 'bool',
];
}

Expand Down Expand Up @@ -103,6 +104,14 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
return $backedEnumClassName::tryFrom($backedEnumValue) ?? throw new RuntimeException(sprintf('Enum value "%s" is not backed by "%s".', $backedEnumValue, $backedEnumClassName));
}

if ('defined' === $prefix) {
try {
return '' !== ($getEnv($name) ?? '');
} catch (EnvNotFoundException) {
return false;
}
}

if ('default' === $prefix) {
if (false === $i) {
throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function testSimpleProcessor()
'require' => ['bool', 'int', 'float', 'string', 'array'],
'enum' => [\BackedEnum::class],
'shuffle' => ['array'],
'defined' => ['bool'],
];

$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,4 +925,21 @@ public function testGetEnvCastsNull($expected, string $prefix)
});
}));
}

/**
* @dataProvider provideGetEnvDefined
*/
public function testGetEnvDefined(bool $expected, callable $callback)
{
$this->assertSame($expected, (new EnvVarProcessor(new Container()))->getEnv('defined', 'NO_SOMETHING', $callback));
}

public static function provideGetEnvDefined(): iterable
{
yield 'Defined' => [true, fn () => 'foo'];
yield 'Falsy but defined' => [true, fn () => '0'];
yield 'Empty string' => [false, fn () => ''];
yield 'Null' => [false, fn () => null];
yield 'Env var not defined' => [false, fn () => throw new EnvNotFoundException()];
}
}