Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitd42f633

Browse files
committed
[DependencyInjection] Fix autocasting null env values to empty string
1 parent7a8457d commitd42f633

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,15 @@ protected function getEnv(string $name)
390390
$prefix ='string';
391391
$localName =$name;
392392
}
393-
$processor =$processors->has($prefix) ?$processors->get($prefix) :newEnvVarProcessor($this);
393+
394+
if ($processors->has($prefix)) {
395+
$processor =$processors->get($prefix);
396+
}else {
397+
$processor =newEnvVarProcessor($this);
398+
if (false ===$i &&'string' ===$prefix) {
399+
$prefix ='';
400+
}
401+
}
394402

395403
$this->resolving[$envName] =true;
396404
try {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
126126
}
127127
}
128128

129+
$returnNull =false;
130+
if ('' ===$prefix) {
131+
$returnNull =true;
132+
$prefix ='string';
133+
}
134+
129135
if (false !==$i ||'string' !==$prefix) {
130136
$env =$getEnv($name);
131137
}elseif (isset($_ENV[$name])) {
@@ -177,6 +183,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
177183
}
178184

179185
if (null ===$env) {
186+
if ($returnNull) {
187+
returnnull;
188+
}
189+
180190
if (!isset($this->getProvidedTypes()[$prefix])) {
181191
thrownewRuntimeException(sprintf('Unsupported env var prefix "%s".',$prefix));
182192
}

‎src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,13 @@ public static function provideGetEnvUrlPath()
763763

764764
/**
765765
* @testWith ["", "string"]
766+
* [null, ""]
766767
* [false, "bool"]
767768
* [true, "not"]
768769
* [0, "int"]
769770
* [0.0, "float"]
770771
*/
771-
publicfunctiontestGetEnvCastsNull($expected,string$prefix)
772+
publicfunctiontestGetEnvCastsNullBehavior($expected,string$prefix)
772773
{
773774
$processor =newEnvVarProcessor(newContainer());
774775

@@ -778,4 +779,19 @@ public function testGetEnvCastsNull($expected, string $prefix)
778779
});
779780
}));
780781
}
782+
783+
publicfunctiontestGetEnvWithEmptyStringPrefixCastsToString()
784+
{
785+
$processor =newEnvVarProcessor(newContainer());
786+
unset($_ENV['FOO']);
787+
$_ENV['FOO'] =4;
788+
789+
try {
790+
$this->assertSame('4',$processor->getEnv('','FOO',staticfunction () {
791+
$this->fail('Should not be called');
792+
}));
793+
}finally {
794+
unset($_ENV['FOO']);
795+
}
796+
}
781797
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp