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` at38b9a27976/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php (L103)
which is then preserved at38b9a27976/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php (L123)
need inspiration for a patch still :) Commits -------550819a655
[DI] Unknown env prefix not regornized as such
This commit is contained in:
commit
b1b64c1361
@ -126,9 +126,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (false !== $i || 'string' !== $prefix) {
|
if (false !== $i || 'string' !== $prefix) {
|
||||||
if (null === $env = $getEnv($name)) {
|
$env = $getEnv($name);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} elseif (isset($_ENV[$name])) {
|
} elseif (isset($_ENV[$name])) {
|
||||||
$env = $_ENV[$name];
|
$env = $_ENV[$name];
|
||||||
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
|
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
|
||||||
@ -173,12 +171,18 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
|||||||
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
|
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $env = $this->container->getParameter("env($name)")) {
|
$env = $this->container->getParameter("env($name)");
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (null === $env) {
|
||||||
|
if (!isset($this->getProvidedTypes()[$prefix])) {
|
||||||
|
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_scalar($env)) {
|
if (!is_scalar($env)) {
|
||||||
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
|
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|||||||
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
|
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
|
||||||
use Symfony\Component\DependencyInjection\EnvVarProcessor;
|
use Symfony\Component\DependencyInjection\EnvVarProcessor;
|
||||||
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
|
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
|
||||||
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||||
|
|
||||||
class EnvVarProcessorTest extends TestCase
|
class EnvVarProcessorTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -595,4 +596,17 @@ CSV;
|
|||||||
|
|
||||||
$this->assertSame(2, $index);
|
$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;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user