8000 feature #29767 Nullable environment variable processor (bpolaszek) · symfony/symfony@12a01a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12a01a2

Browse files
committed
feature #29767 Nullable environment variable processor (bpolaszek)
This PR was merged into the 4.3-dev branch. Discussion ---------- Nullable environment variable processor | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | todo Hey there, Because environment variables are strings by design, empty environment variables are evaluated to `""` by default. In the same way, `MY_ENV_VAR=null` will be evaluated to `"null"`, as a string. What I suggest is to allow some environment variables to be evaluated to `null` (the real one) when their strings are _blank_ or equal _null_, _Null_ or _NULL_. This can be easily done through a new `nullable` processor: ```bash # .env API_KEY= ``` ```yaml # config/services.yaml services: FooService: arguments: $apiKey: %env(nullable:API_KEY)% ``` ```php # src/Services/FooService namespace App\Services; final class FooService { /** * @var string|null */ private $apiKey; /** * FooService constructor. */ public function __construct(?string $apiKey) { $this->apiKey = $apiKey; } public function doSomething() { // Free plan if (null === $this->apiKey) { // ... } } } ``` That's an example that comes to my mind but there can be many others. This can also help in using null coalesce operators in constructors instead of checking if a string equals `""` (which is very PHP4 style). Of course it can be used in conjunction with other types, i.e. `%env(float:nullable:SOME_OPTIONAL_FLOAT_ENV_VAR)%`. What do you think? Commits ------- 3a604ac Nullable environment variable processor
2 parents 3c2dc44 + 3a604ac commit 12a01a2

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

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

77
* added `%env(trim:...)%` processor to trim a string value
88
* added `%env(default:...)%` processor to fallback to a default value
9+
* added `%env(nullable:...)%` processor to allow empty variables to be processed as null values
910
* added support for deprecating aliases
1011

1112
4.2.0

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static function getProvidedTypes()
4141
'int' => 'int',
4242
'json' => 'array',
4343
'key' => 'bool|int|float|string|array',
44+
'nullable' => 'bool|int|float|string|array',
4445
'resolve' => 'string',
4546
'default' => 'bool|int|float|string|array',
4647
'string' => 'string',
@@ -195,6 +196,10 @@ public function getEnv($prefix, $name, \Closure $getEnv)
195196
return str_getcsv($env);
196197
}
197198

199+
if ('nullable' === $prefix) {
200+
return '' === $env ? null : $env;
201+
}
202+
198203
if ('trim' === $prefix) {
199204
return trim($env);
200205
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testSimpleProcessor()
3939
'int' => ['int'],
4040
'json' => ['array'],
4141
'key' => ['bool', 'int', 'float', 'string', 'array'],
42+
'nullable' => ['bool', 'int', 'float', 'string', 'array'],
4243
'resolve' => ['string'],
4344
'default' => ['bool', 'int', 'float', 'string', 'array'],
4445
'string' => ['string'],

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,29 @@ public function testGetEnvKeyChained()
433433
];
434434
}));
435435
}
436+
437+
/**
438+
* @dataProvider validNullables
439+
*/
440+
public function testGetEnvNullable($value, $processed)
441+
{
442+
$processor = new EnvVarProcessor(new Container());
443+
$result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) {
444+
$this->assertSame('foo', $name);
445+
446+
return $value;
447+
});
448+
$this->assertSame($processed, $result);
449+
}
450+
451+
public function validNullables()
452+
{
453+
return [
454+
['hello', 'hello'],
455+
['', null],
456+
['null', 'null'],
457+
['Null', 'Null'],
458+
['NULL', 'NULL'],
459+
];
460+
}
436461
}

0 commit comments

Comments
 (0)
0