8000 [DI] Force env params to be string|null · symfony/symfony@93c25f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93c25f8

Browse files
[DI] Force env params to be string|null
1 parent b376d92 commit 93c25f8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,26 @@ public function mergeEnvPlaceholders(self $bag)
7979
}
8080
}
8181
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function resolve()
87+
{
88+
if ($this->resolved) {
89+
return;
90+
}
91+
parent::resolve();
92+
93+
foreach ($this->envPlaceholders as $env => $placeholders) {
94+
if (!isset($this->parameters[$name = strtolower("env($env)")])) {
95+
continue;
96+
}
97+
if (is_numeric($default = $this->parameters[$name])) {
98+
$this->parameters[$name] = (string) $default;
99+
} elseif (!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)));
101+
}
102+
}
103+
}
82104
}

src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,25 @@ public function testMergeWithDifferentIdentifiersForPlaceholders()
109109
$this->assertNotEquals($firstPlaceholder, $secondPlaceholder);
110110
$this->assertCount(2, $merged[$envName]);
111111
}
112+
113+
public function testResolveEnvCastsIntToString()
114+
{
115+
$bag = new EnvPlaceholderParameterBag();
116+
$bag->get('env(INT)');
117+
$bag->set('env(INT)', 2);
118+
$bag->resolve();
119+
$this->assertSame('2', $bag->all()['env(int)']);
120+
}
121+
122+
/**
123+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
124+
* @expectedExceptionMessage The default value of env parameter "ARRAY" must be string or null, array given.
125+
*/
126+
public function testResolveThrowsOnBadDefaultValue()
127+
{
128+
$bag = new EnvPlaceholderParameterBag();
129+
$bag->get('env(ARRAY)');
130+
$bag->set('env(ARRAY)', array());
131+
$bag->resolve();
132+
}
112133
}

0 commit comments

Comments
 (0)
0