Skip to content

Commit 32613bb

Browse files
ro0NLnicolas-grekas
authored andcommitted
[DI] Unknown env prefix not regornized as such
1 parent ac8e19e commit 32613bb

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

EnvVarProcessor.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ public function getEnv($prefix, $name, \Closure $getEnv)
126126
}
127127

128128
if (false !== $i || 'string' !== $prefix) {
129-
if (null === $env = $getEnv($name)) {
130-
return null;
131-
}
129+
$env = $getEnv($name);
132130
} elseif (isset($_ENV[$name])) {
133131
$env = $_ENV[$name];
134132
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
@@ -173,10 +171,16 @@ public function getEnv($prefix, $name, \Closure $getEnv)
173171
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
174172
}
175173

176-
if (null === $env = $this->container->getParameter("env($name)")) {
177-
return null;
178-
}
174+
$env = $this->container->getParameter("env($name)");
175+
}
176+
}
177+
178+
if (null === $env) {
179+
if (!isset($this->getProvidedTypes()[$prefix])) {
180+
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
179181
}
182+
183+
return null;
180184
}
181185

182186
if (!is_scalar($env)) {

Tests/EnvVarProcessorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
1010
use Symfony\Component\DependencyInjection\EnvVarProcessor;
1111
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
12+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1213

1314
class EnvVarProcessorTest extends TestCase
1415
{
@@ -595,4 +596,17 @@ public function loadEnvVars(): array
595596

596597
$this->assertSame(2, $index);
597598
}
599+
600+
public function testGetEnvInvalidPrefixWithDefault()
601+
{
602+
$this->expectException(RuntimeException::class);
603+
$this->expectExceptionMessage('Unsupported env var prefix');
604+
$processor = new EnvVarProcessor(new Container());
605+
606+
$processor->getEnv('unknown', 'default::FAKE', function ($name) {
607+
$this->assertSame('default::FAKE', $name);
608+
609+
return null;
610+
});
611+
}
598612
}

0 commit comments

Comments
 (0)