Skip to content

Commit b1b64c1

Browse files
bug #33960 [DI] Unknown env prefix not recognized as such (ro0NL)
This PR was submitted for the 4.3 branch but it was merged into the 4.4 branch instead. Discussion ---------- [DI] Unknown env prefix not recognized as such | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #... <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This is a failing test to illustrate the difference between real and fake env vars when using an unknown prefix, followed by the `default` prefix. ``` %env(unknown:default::REAL)% // Unsupported env var prefix "unknown". %env(unknown:default::FAKE)% // null ``` For `default::FAKE` we get `null` at https://github.com/symfony/symfony/blob/38b9a27976d36c4ffbfeb9e666319d77ffc8b3c0/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php#L103 which is then preserved at https://github.com/symfony/symfony/blob/38b9a27976d36c4ffbfeb9e666319d77ffc8b3c0/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php#L123 need inspiration for a patch still :) Commits ------- 550819a [DI] Unknown env prefix not regornized as such
2 parents a59ce75 + 550819a commit b1b64c1

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

+10-6
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)) {

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

+14
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)