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
-------

c50aad2be1 [DI] replace "nullable" env processor by improving the "default" one
This commit is contained in:
Fabien Potencier 2019-03-10 20:40:48 +01:00
commit a96308f0ba
4 changed files with 11 additions and 12 deletions

View File

@ -5,8 +5,7 @@ CHANGELOG
-----
* added `%env(trim:...)%` processor to trim a string value
* added `%env(default:...)%` processor to fallback to a default value
* added `%env(nullable:...)%` processor to allow empty variables to be processed as null values
* added `%env(default:param_name:...)%` processor to fallback to a parameter or to null when using `%env(default::...)%`
* added support for deprecating aliases
* made `ContainerParametersResource` final and not implement `Serializable` anymore
* added ability to define an index for a tagged collection

View File

@ -41,7 +41,6 @@ class EnvVarProcessor implements EnvVarProcessorInterface
'int' => 'int',
'json' => 'array',
'key' => 'bool|int|float|string|array',
'nullable' => 'bool|int|float|string|array',
'resolve' => 'string',
'default' => 'bool|int|float|string|array',
'string' => 'string',
@ -84,15 +83,21 @@ class EnvVarProcessor implements EnvVarProcessorInterface
$next = substr($name, $i + 1);
$default = substr($name, 0, $i);
if (!$this->container->hasParameter($default)) {
if ('' !== $default && !$this->container->hasParameter($default)) {
throw new RuntimeException(sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default));
}
try {
return $getEnv($next);
$env = $getEnv($next);
if ('' !== $env && null !== $env) {
return $env;
}
} catch (EnvNotFoundException $e) {
return $this->container->getParameter($default);
// no-op
}
return '' === $default ? null : $this->container->getParameter($default);
}
if ('file' === $prefix) {
@ -196,10 +201,6 @@ class EnvVarProcessor implements EnvVarProcessorInterface
return str_getcsv($env);
}
if ('nullable' === $prefix) {
return '' === $env ? null : $env;
}
if ('trim' === $prefix) {
return trim($env);
}

View File

@ -39,7 +39,6 @@ class RegisterEnvVarProcessorsPassTest extends TestCase
'int' => ['int'],
'json' => ['array'],
'key' => ['bool', 'int', 'float', 'string', 'array'],
'nullable' => ['bool', 'int', 'float', 'string', 'array'],
'resolve' => ['string'],
'default' => ['bool', 'int', 'float', 'string', 'array'],
'string' => ['string'],

View File

@ -440,7 +440,7 @@ class EnvVarProcessorTest extends TestCase
public function testGetEnvNullable($value, $processed)
{
$processor = new EnvVarProcessor(new Container());
$result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) {
$result = $processor->getEnv('default', ':foo', function ($name) use ($value) {
$this->assertSame('foo', $name);
return $value;