Skip to content

Commit c7486f0

Browse files
GaryPEGEOTnicolas-grekas
authored andcommitted
[DependencyInjection] Add defined prefix for env var processor
1 parent 0f2db57 commit c7486f0

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
6.3
1011
---

EnvVarProcessor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static function getProvidedTypes(): array
5656
'require' => 'bool|int|float|string|array',
5757
'enum' => \BackedEnum::class,
5858
'shuffle' => 'array',
59+
'defined' => 'bool',
5960
];
6061
}
6162

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

107+
if ('defined' === $prefix) {
108+
try {
109+
return '' !== ($getEnv($name) ?? '');
110+
} catch (EnvNotFoundException) {
111+
return false;
112+
}
113+
}
114+
106115
if ('default' === $prefix) {
107116
if (false === $i) {
108117
throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));

Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function testSimpleProcessor()
5050
'require' => ['bool', 'int', 'float', 'string', 'array'],
5151
'enum' => [\BackedEnum::class],
5252
'shuffle' => ['array'],
53+
'defined' => ['bool'],
5354
];
5455

5556
$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());

Tests/EnvVarProcessorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,21 @@ public function testGetEnvCastsNull($expected, string $prefix)
925925
});
926926
}));
927927
}
928+
929+
/**
930+
* @dataProvider provideGetEnvDefined
931+
*/
932+
public function testGetEnvDefined(bool $expected, callable $callback)
933+
{
934+
$this->assertSame($expected, (new EnvVarProcessor(new Container()))->getEnv('defined', 'NO_SOMETHING', $callback));
935+
}
936+
937+
public static function provideGetEnvDefined(): iterable
938+
{
939+
yield 'Defined' => [true, fn () => 'foo'];
940+
yield 'Falsy but defined' => [true, fn () => '0'];
941+
yield 'Empty string' => [false, fn () => ''];
942+
yield 'Null' => [false, fn () => null];
943+
yield 'Env var not defined' => [false, fn () => throw new EnvNotFoundException()];
944+
}
928945
}

0 commit comments

Comments
 (0)