Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DI] Unknown env prefix not recognized as such #33960

Merged
merged 1 commit into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Expand Up @@ -126,9 +126,7 @@ public function getEnv($prefix, $name, \Closure $getEnv)
}

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

if (null === $env = $this->container->getParameter("env($name)")) {
return null;
}
$env = $this->container->getParameter("env($name)");
}
}

if (null === $env) {
if (!isset($this->getProvidedTypes()[$prefix])) {
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
}

return null;
}

if (!is_scalar($env)) {
Expand Down
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
use Symfony\Component\DependencyInjection\EnvVarProcessor;
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;

class EnvVarProcessorTest extends TestCase
{
Expand Down Expand Up @@ -595,4 +596,17 @@ public function loadEnvVars(): array

$this->assertSame(2, $index);
}

public function testGetEnvInvalidPrefixWithDefault()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unsupported env var prefix');
$processor = new EnvVarProcessor(new Container());

$processor->getEnv('unknown', 'default::FAKE', function ($name) {
$this->assertSame('default::FAKE', $name);

return null;
});
}
}