Skip to content

Commit 81b8c21

Browse files
Merge branch '6.2' into 6.3
* 6.2: 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 Fix executable bit Readme: Replace Stack Overflow with GitHub Discussions [DoctrineBridge] Remove outdated comment [DependencyInjection] Fix annotation [SecurityBundle] Do not translate `Bearer` header’s `error_description` [String] Fix Inflector for 'status' [DependencyInjection] Fix resource tracking for lazy services [EventDispatcher] [EventDispatcher] Throw exception when listener method cannot be resolved
2 parents 7abf242 + 462409e commit 81b8c21

File tree

8 files changed

+49
-5
lines changed

8 files changed

+49
-5
lines changed

Container.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,15 @@ protected function getEnv(string $name): mixed
372372
$prefix = 'string';
373373
$localName = $name;
374374
}
375-
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);
375+
376+
if ($processors->has($prefix)) {
377+
$processor = $processors->get($prefix);
378+
} else {
379+
$processor = new EnvVarProcessor($this);
380+
if (false === $i) {
381+
$prefix = '';
382+
}
383+
}
376384

377385
$this->resolving[$envName] = true;
378386
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

Dumper/PhpDumper.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Dumper;
1313

1414
use Composer\Autoload\ClassLoader;
15+
use Symfony\Component\Config\Resource\FileResource;
1516
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
1617
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
1718
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@@ -581,8 +582,19 @@ private function generateProxyClasses(): array
581582
continue;
582583
}
583584
$alreadyGenerated[$asGhostObject][$class] = true;
584-
// register class' reflector for resource tracking
585-
$this->container->getReflectionClass($class);
585+
586+
$r = $this->container->getReflectionClass($class);
587+
do {
588+
$file = $r->getFileName();
589+
if (str_ends_with($file, ') : eval()\'d code')) {
590+
$file = substr($file, 0, strrpos($file, '(', -17));
591+
}
592+
if (is_file($file)) {
593+
$this->container->addResource(new FileResource($file));
594+
}
595+
$r = $r->getParentClass() ?: null;
596+
} while ($r?->isUserDefined());
597+
586598
if ("\n" === $proxyCode = "\n".$proxyDumper->getProxyCode($definition, $id)) {
587599
continue;
588600
}

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 ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)))
@@ -192,6 +198,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
192198
}
193199

194200
if (null === $env) {
201+
if ($returnNull) {
202+
return null;
203+
}
204+
195205
if (!isset($this->getProvidedTypes()[$prefix])) {
196206
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
197207
}

Tests/EnvVarProcessorTest.php

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

911911
/**
912912
* @testWith ["", "string"]
913+
* [null, ""]
913914
* [false, "bool"]
914915
* [true, "not"]
915916
* [0, "int"]
916917
* [0.0, "float"]
917918
*/
918-
public function testGetEnvCastsNull($expected, string $prefix)
919+
public function testGetEnvCastsNullBehavior($expected, string $prefix)
919920
{
920921
$processor = new EnvVarProcessor(new Container());
921922

@@ -925,4 +926,17 @@ public function testGetEnvCastsNull($expected, string $prefix)
925926
});
926927
}));
927928
}
929+
930+
public function testGetEnvWithEmptyStringPrefixCastsToString()
931+
{
932+
$processor = new EnvVarProcessor(new Container());
933+
unset($_ENV['FOO']);
934+
$_ENV['FOO'] = 4;
935+
936+
try {
937+
$this->assertSame('4', $processor->getEnv('', 'FOO', function () { $this->fail('Should not be called'); }));
938+
} finally {
939+
unset($_ENV['FOO']);
940+
}
941+
}
928942
}

Tests/Fixtures/AutoconfiguredService1.php

100755100644
File mode changed.

Tests/Fixtures/AutoconfiguredService2.php

100755100644
File mode changed.

Tests/Fixtures/IntTagClass.php

100755100644
File mode changed.

0 commit comments

Comments
 (0)