[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(trim:...)%` processor to trim a string value
|
||||||
* added `%env(default:...)%` processor to fallback to a default value
|
* added `%env(default:param_name:...)%` processor to fallback to a parameter or to null when using `%env(default::...)%`
|
||||||
* added `%env(nullable:...)%` processor to allow empty variables to be processed as null values
|
|
||||||
* added support for deprecating aliases
|
* added support for deprecating aliases
|
||||||
* made `ContainerParametersResource` final and not implement `Serializable` anymore
|
* made `ContainerParametersResource` final and not implement `Serializable` anymore
|
||||||
* added ability to define an index for a tagged collection
|
* added ability to define an index for a tagged collection
|
||||||
|
@ -41,7 +41,6 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
'int' => 'int',
|
'int' => 'int',
|
||||||
'json' => 'array',
|
'json' => 'array',
|
||||||
'key' => 'bool|int|float|string|array',
|
'key' => 'bool|int|float|string|array',
|
||||||
'nullable' => 'bool|int|float|string|array',
|
|
||||||
'resolve' => 'string',
|
'resolve' => 'string',
|
||||||
'default' => 'bool|int|float|string|array',
|
'default' => 'bool|int|float|string|array',
|
||||||
'string' => 'string',
|
'string' => 'string',
|
||||||
@ -84,15 +83,21 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
$next = substr($name, $i + 1);
|
$next = substr($name, $i + 1);
|
||||||
$default = substr($name, 0, $i);
|
$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));
|
throw new RuntimeException(sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $getEnv($next);
|
$env = $getEnv($next);
|
||||||
} catch (EnvNotFoundException $e) {
|
|
||||||
return $this->container->getParameter($default);
|
if ('' !== $env && null !== $env) {
|
||||||
|
return $env;
|
||||||
}
|
}
|
||||||
|
} catch (EnvNotFoundException $e) {
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
|
||||||
|
return '' === $default ? null : $this->container->getParameter($default);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('file' === $prefix) {
|
if ('file' === $prefix) {
|
||||||
@ -196,10 +201,6 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
return str_getcsv($env);
|
return str_getcsv($env);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('nullable' === $prefix) {
|
|
||||||
return '' === $env ? null : $env;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('trim' === $prefix) {
|
if ('trim' === $prefix) {
|
||||||
return trim($env);
|
return trim($env);
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,6 @@ class RegisterEnvVarProcessorsPassTest extends TestCase
|
|||||||
'int' => ['int'],
|
'int' => ['int'],
|
||||||
'json' => ['array'],
|
'json' => ['array'],
|
||||||
'key' => ['bool', 'int', 'float', 'string', 'array'],
|
'key' => ['bool', 'int', 'float', 'string', 'array'],
|
||||||
'nullable' => ['bool', 'int', 'float', 'string', 'array'],
|
|
||||||
'resolve' => ['string'],
|
'resolve' => ['string'],
|
||||||
'default' => ['bool', 'int', 'float', 'string', 'array'],
|
'default' => ['bool', 'int', 'float', 'string', 'array'],
|
||||||
'string' => ['string'],
|
'string' => ['string'],
|
||||||
|
@ -440,7 +440,7 @@ class EnvVarProcessorTest extends TestCase
|
|||||||
public function testGetEnvNullable($value, $processed)
|
public function testGetEnvNullable($value, $processed)
|
||||||
{
|
{
|
||||||
$processor = new EnvVarProcessor(new Container());
|
$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);
|
$this->assertSame('foo', $name);
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
|
Reference in New Issue
Block a user