8000 bug #20590 [DI] Allow null as default env value (sroze) · symfony/symfony@3c1361c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c1361c

Browse files
bug #20590 [DI] Allow null as default env value (sroze)
This PR was squashed before being merged into the 3.2 branch (closes #20590). Discussion ---------- [DI] Allow null as default env value | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ø | License | MIT | Doc PR | ø This PR allows the environment variable default value to be null. The way, we can allow optional environment variables, such as in this example: ```yaml parameters: # Default values env(DATABASE_HOST): database env(DATABASE_PORT): ~ env(DATABASE_NAME): symfony env(DATABASE_USERNAME): root env(DATABASE_PASSWORD): ~ # Database configuration database_host: "%env(DATABASE_HOST)%" database_port: "%env(DATABASE_PORT)%" database_name: "%env(DATABASE_NAME)%" database_user: "%env(DATABASE_USERNAME)%" database_password: "%env(DATABASE_PASSWORD)%" ``` Commits ------- 519a471 [DI] Allow null as default env value
2 parents 59f9949 + 519a471 commit 3c1361c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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)
0