Skip to content

Commit 462409e

Browse files
Merge branch '5.4' into 6.2
* 5.4: cs fix [Messenger] Fix passing options set via tags to handler descriptors random_bytes length should be an int greater than 0 enforce UTC timezone in test [DependencyInjection] Fix autocasting null env values to empty string Fix executable bit Readme: Replace Stack Overflow with GitHub Discussions [DependencyInjection] Fix annotation [String] Fix Inflector for 'status' [EventDispatcher] [EventDispatcher] Throw exception when listener method cannot be resolved
2 parents b27b20d + 6858b2d commit 462409e

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

Container.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,15 @@ protected function getEnv(string $name): mixed
354354
$prefix = 'string';
355355
$localName = $name;
356356
}
357-
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);
357+
358+
if ($processors->has($prefix)) {
359+
$processor = $processors->get($prefix);
360+
} else {
361+
$processor = new EnvVarProcessor($this);
362+
if (false === $i) {
363+
$prefix = '';
364+
}
365+
}
358366

359367
$this->resolving[$envName] = true;
360368
try {

ContainerAwareTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
trait ContainerAwareTrait
2020
{
2121
/**
22-
* @var ContainerInterface
22+
* @var ContainerInterface|null
2323
*/
2424
protected $container;
2525

EnvVarProcessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
143143
}
144144
}
145145

146+
$returnNull = false;
147+
if ('' === $prefix) {
148+
$returnNull = true;
149+
$prefix = 'string';
150+
}
151+
146152
if (false !== $i || 'string' !== $prefix) {
147153
$env = $getEnv($name);
148154
} elseif (isset($_ENV[$name])) {
@@ -194,6 +200,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
194200
}
195201

196202
if (null === $env) {
203+
if ($returnNull) {
204+
return null;
205+
}
206+
197207
if (!isset($this->getProvidedTypes()[$prefix])) {
198208
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
199209
}

Tests/EnvVarProcessorTest.php

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

855855
/**
856856
* @testWith ["", "string"]
857+
* [null, ""]
857858
* [false, "bool"]
858859
* [true, "not"]
859860
* [0, "int"]
860861
* [0.0, "float"]
861862
*/
862-
public function testGetEnvCastsNull($expected, string $prefix)
863+
public function testGetEnvCastsNullBehavior($expected, string $prefix)
863864
{
864865
$processor = new EnvVarProcessor(new Container());
865866

@@ -869,4 +870,17 @@ public function testGetEnvCastsNull($expected, string $prefix)
869870
});
870871
}));
871872
}
873+
874+
public function testGetEnvWithEmptyStringPrefixCastsToString()
875+
{
876+
$processor = new EnvVarProcessor(new Container());
877+
unset($_ENV['FOO']);
878+
$_ENV['FOO'] = 4;
879+
880+
try {
881+
$this->assertSame('4', $processor->getEnv('', 'FOO', function () { $this->fail('Should not be called'); }));
882+
} finally {
883+
unset($_ENV['FOO']);
884+
}
885+
}
872886
}

0 commit comments

Comments
 (0)