Skip to content

Commit b9c30fb

Browse files
bug #51198 [DependencyInjection] Fix autocasting null env values to empty string with container.env_var_processors_locator (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix autocasting `null` env values to empty string with `container.env_var_processors_locator` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #50837 (comment) | License | MIT | Doc PR | no The previous fix doesn't work when `$processors` comes from `container.env_var_processors_locator`. Commits ------- 766bc6e [DependencyInjection] Fix autocasting null env values to empty string with container.env_var_processors_locator
2 parents c0fbe7f + 766bc6e commit b9c30fb

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
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) {
396+
$prefix = '';
401397
}
402398

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

src/Symfony/Component/DependencyInjection/EnvVarProcessorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface EnvVarProcessorInterface
2323
/**
2424
* Returns the value of the given variable as managed by the current instance.
2525
*
26-
* @param string $prefix The namespace of the variable
26+
* @param string $prefix The namespace of the variable; when the empty string is passed, null values should be kept as is
2727
* @param string $name The name of the variable within the namespace
2828
* @param \Closure $getEnv A closure that allows fetching more env vars
2929
*

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

Lines changed: 29 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,33 @@ 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+
$r = new \ReflectionMethod($container, 'getEnv');
418+
$r->setAccessible(true);
419+
$this->assertNull($r->invoke($container, 'FOO'));
420+
}
421+
422+
public function testGetEnvDoesNotAutoCastNullWithEnvVarProcessorsLocatorReturningDefaultEnvVarProcessor()
423+
{
424+
$container = new Container();
425+
$container->setParameter('env(FOO)', null);
426+
$container->set('container.env_var_processors_locator', new ServiceLocator([
427+
'string' => static function () use ($container): EnvVarProcessor {
428+
return new EnvVarProcessor($container);
429+
},
430+
]));
431+
$container->compile();
432+
433+
$r = new \ReflectionMethod($container, 'getEnv');
434+
$r->setAccessible(true);
435+
$this->assertNull($r->invoke($container, 'FOO'));
436+
}
408437
}
409438

410439
class ProjectServiceContainer extends Container

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy