[DI] replace "nullable" env processor by improving the "default" one
This commit is contained in:
parent
c45bbd4442
commit
c50aad2be1
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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'],
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user