Skip to content

Commit

Permalink
[DI] replace "nullable" env processor by improving the "default" one
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 9, 2019
1 parent d4326b2 commit 22823d2
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Expand Up @@ -41,7 +41,6 @@ public static function getProvidedTypes()
'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',
Expand Down Expand Up @@ -84,14 +83,14 @@ public function getEnv($prefix, $name, \Closure $getEnv)
$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);
return $getEnv($next) ?: ('' === $default ? null : $this->container->getParameter($default));
} catch (EnvNotFoundException $e) {
return $this->container->getParameter($default);
return '' === $default ? null : $this->container->getParameter($default);
}
}

Expand Down Expand Up @@ -196,10 +195,6 @@ public function getEnv($prefix, $name, \Closure $getEnv)
return str_getcsv($env);
}

if ('nullable' === $prefix) {
return '' === $env ? null : $env;
}

if ('trim' === $prefix) {
return trim($env);
}
Expand Down
Expand Up @@ -39,7 +39,6 @@ public function testSimpleProcessor()
'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'],
Expand Down
Expand Up @@ -440,7 +440,7 @@ public function testGetEnvKeyChained()
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;
Expand Down

0 comments on commit 22823d2

Please sign in to comment.