Skip to content

Commit fb844ae

Browse files
srozenicolas-grekas
authored andcommitted
[DI] Allow null as default env value
1 parent 922837a commit fb844ae

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

ParameterBag/EnvPlaceholderParameterBag.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function get($name)
4141
if ($this->has($name)) {
4242
$defaultValue = parent::get($name);
4343

44-
if (!is_scalar($defaultValue)) {
45-
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar, but "%s" given to "%s".', gettype($defaultValue), $name));
44+
if (null !== $defaultValue && !is_scalar($defaultValue)) {
45+
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', gettype($defaultValue), $name));
4646
}
4747
}
4848

@@ -96,8 +96,8 @@ public function resolve()
9696
}
9797
if (is_numeric($default = $this->parameters[$name])) {
9898
$this->parameters[$name] = (string) $default;
99-
} elseif (null !== $default && !is_string($default)) {
100-
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be string or null, %s given.', $env, gettype($default)));
99+
} elseif (null !== $default && !is_scalar($default)) {
100+
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, gettype($default)));
101101
}
102102
}
103103
}

Tests/ParameterBag/EnvPlaceholderParameterBagTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function testResolveEnvAllowsNull()
130130

131131
/**
132132
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
133-
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be string or null, array given.
133+
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be scalar or null, array given.
134134
*/
135135
public function testResolveThrowsOnBadDefaultValue()
136136
{
@@ -139,4 +139,26 @@ public function testResolveThrowsOnBadDefaultValue()
139139
$bag->set('env(Array_Var)', array());
140140
$bag->resolve();
141141
}
142+
143+
public function testGetEnvAllowsNull()
144+
{
145+
$bag = new EnvPlaceholderParameterBag();
146+
$bag->set('env(NULL_VAR)', null);
147+
$bag->get('env(NULL_VAR)');
148+
$bag->resolve();
149+
150+
$this->assertNull($bag->all()['env(null_var)']);
151+
}
152+
153+
/**
154+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
155+
* @expectedExceptionMessage The default value of an env() parameter must be scalar or null, but "array" given to "env(ARRAY_VAR)".
156+
*/
157+
public function testGetThrowsOnBadDefaultValue()
158+
{
159+
$bag = new EnvPlaceholderParameterBag();
160+
$bag->set('env(ARRAY_VAR)', array());
161+
$bag->get('env(ARRAY_VAR)');
162+
$bag->resolve();
163+
}
142164
}

0 commit comments

Comments
 (0)