Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 766bc6e

Browse filesBrowse files
committed
[DependencyInjection] Fix autocasting null env values to empty string with container.env_var_processors_locator
1 parent c0fbe7f commit 766bc6e
Copy full SHA for 766bc6e

File tree

3 files changed

+33
-8
lines changed
Filter options

3 files changed

+33
-8
lines changed

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+3-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/EnvVarProcessorInterface.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
+29Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.