8000 feature #30504 [DI] replace "nullable" env processor by improving the… · symfony/symfony@a96308f · GitHub
[go: up one dir, main page]

Skip to content

Commit a96308f

Browse files
committed
feature #30504 [DI] replace "nullable" env processor by improving the "default" one (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [DI] replace "nullable" env processor by improving the "default" one | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Neither `nullable` nor `default` are released yet. I propose to replace the `nullable` processor (see #29767) with an improved `default` one (from #28976). `%env(default::FOO)%` now defaults to `null` when the env var doesn't exist or compares to false". ping @jderusse @bpolaszek Commits ------- c50aad2 [DI] replace "nullable" env processor by improving the "default" one
2 parents 4619ae4 + c50aad2 commit a96308f

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

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

77
* added `%env(trim:...)%` processor to trim a string value
8-
* 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
8+
* added `%env(default:param_name:...)%` processor to fallback to a parameter or to null when using `%env(default::...)%`
109
* added support for deprecating aliases
1110
* made `ContainerParametersResource` final and not implement `Serializable` anymore
1211
* added ability to define an index for a tagged collection

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static function getProvidedTypes()
4141
'int' => 'int',
4242
'json' => 'array',
4343
'key' => 'bool|int|float|string|array',
44-
'nullable' => 'bool|int|float|string|array',
4544
'resolve' => 'string',
4645
'default' => 'bool|int|float|string|array',
4746
'string' => 'string',
@@ -84,15 +83,21 @@ public function getEnv($prefix, $name, \Closure $getEnv)
8483
$next = substr($name, $i + 1);
8584
$default = substr($name, 0, $i);
8685

87-
if (!$this->container->hasParameter($default)) {
86+
if ('' !== $default && !$this->container->hasParameter($default)) {
8887
throw new RuntimeException(sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default));
8988
}
9089

9190
try {
92-
return $getEnv($next);
91+
$env = $getEnv($next);
92+
93+
if ('' !== $env && null !== $env) {
94+
return $env;
95+
}
9396
} catch (EnvNotFoundException $e) {
94-
return $this->container->getParameter($default);
97+
// no-op
9598
}
99+
100+
return '' === $default ? null : $this->container->getParameter($default);
96101
}
97102

98103
if ('file' === $prefix) {
@@ -196,10 +201,6 @@ public function getEnv($prefix, $name, \Closure $getEnv)
196201
return str_getcsv($env);
197202
}
198203

199-
if ('nullable' === $prefix) {
200-
return '' === $env ? null : $env;
201-
}
202-
203204
if ('trim' === $prefix) {
204205
return trim($env);
205206
}

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public function testGetEnvKe 67B8 yChained()
440440
public function testGetEnvNullable($value, $processed)
441441
{
442442
$processor = new EnvVarProcessor(new Container());
443-
$result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) {
443+
$result = $processor->getEnv('default', ':foo', function ($name) use ($value) {
444444
$this->assertSame('foo', $name);
445445

446446
return $value;

0 commit comments

Comments
 (0)
0