8000 [DependencyInjection] Fix autocasting null env values to empty string… · symfony/symfony@db34fb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit db34fb0

Browse files
committed
[DependencyInjection] Fix autocasting null env values to empty string with container.env_var_processors_locator
1 parent 69e1127 commit db34fb0

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,9 @@ protected function getEnv(string $name)
391391
$localName = $name;
392392
}
393393

394-
if ($processors->has($prefix)) {
395-
$processor = $processors->get($prefix);
396-
} else {
397-
$processor = new EnvVarProcessor($this);
398-
if (false === $i) {
399-
$prefix = '';
400-
}
394+
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);
395+
if (false === $i && EnvVarProcessor::class === \get_class($processor)) {
396+
$prefix = '';
401397
}
402398

403399
$this->resolving[$envName] = true;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Container;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\DependencyInjection\EnvVarProcessor;
1718
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1819
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1920
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2021
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
2122
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
23+
use Symfony\Component\DependencyInjection\ServiceLocator;
2224
use Symfony\Contracts\Service\ResetInterface;
2325

2426
class ContainerTest extends TestCase
@@ -405,6 +407,31 @@ public function testRequestAnInternalSharedPrivateService()
405407
$c->get('internal_dependency');
406408
$c->get('internal');
407409
}
410+
411+
public function testGetEnvDoesNotAutoCastNullWithDefaultEnvVarProcessor()
412+
{
413+
$container = new Container();
414+
$container->setParameter('env(FOO)', null);
415+
$container->compile();
416+
417+
$this->assertNull((new \ReflectionMethod($container, 'getEnv'))->invoke($container, 'FOO'));
418+
}
419+
420+
public function testGetEnvDoesNotAutoCastNullWithEnvVarProcessorsLocatorReturningDefaultEnvVarProcessor()
421+
{
422+
$container = new Container();
423+
$container->setParameter('env(FOO)', null);
424+
$container->set('container.env_var_processors_locator', new ServiceLocator([
425+
'string' => static function () use ($container): EnvVarProcessor {
426+
return new EnvVarProcessor($container);
427+
},
428+
]));
429+
$container->compile();
430+
431+
$r = new \ReflectionMethod($container, 'getEnv');
432+
$r->setAccessible(true);
433+
$this->assertNull($r->invoke($container, 'FOO'));
434+
}
408435
}
409436

410437
class ProjectServiceContainer extends Container

0 commit comments

Comments
 (0)
0