Skip to content

Commit

Permalink
Nullable environment variable processor - symfony#29767
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed Jan 3, 2019
1 parent b948d5a commit 6629fd1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\DependencyInjection;

use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;

/**
Expand Down Expand Up @@ -42,6 +41,7 @@ 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 @@ -195,6 +195,12 @@ public function getEnv($prefix, $name, \Closure $getEnv)
return str_getcsv($env);
}

if ('nullable' === $prefix) {
$nullables = array('', 'null', 'NULL', 'Null');

return \in_array($env, $nullables, true) ? null : $env;
}

throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
}
}
Expand Up @@ -39,6 +39,7 @@ public function testSimpleProcessor()
'int' => array('int'),
'json' => array('array'),
'key' => array('bool', 'int', 'float', 'string', 'array'),
'nullable' => array('bool', 'int', 'float', 'string', 'array'),
'resolve' => array('string'),
'default' => array('bool', 'int', 'float', 'string', 'array'),
'string' => array('string'),
Expand Down
Expand Up @@ -420,4 +420,31 @@ public function testGetEnvKeyChained()
);
}));
}

/**
* @dataProvider validNullables
*/
public function testGetEnvNullable($value, $processed)
{
$processor = new EnvVarProcessor(new Container());

$result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) {
$this->assertSame('foo', $name);

return $value;
});

$this->assertSame($processed, $result);
}

public function validNullables()
{
return array(
array('hello', 'hello'),
array('', null),
array('null', null),
array('Null', null),
array('NULL', null),
);
}
}

0 comments on commit 6629fd1

Please sign in to comment.