8000 [DI] Prevent an ininite loop when using env vars in service names · symfony/symfony@11afeff · GitHub
[go: up one dir, main page]

Skip to content

Commit 11afeff

Browse files
committed
[DI] Prevent an ininite loop when using env vars in service names
1 parent fe07c25 commit 11afeff

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ protected function getEnv($name)
469469
return $this->envCache[$name];
470470
}
471471
if (!$this->has($id = 'container.env_var_processors_locator')) {
472-
$this->set($id, new ServiceLocator(array()));
472+
// Prevent an infinite loop, because set() call getRemovedIds() that may call getEnv()
473+
$this->services[$id] = new ServiceLocator(array());
473474
}
474475
if (!$this->getEnv) {
475476
$this->getEnv = new \ReflectionMethod($this, __FUNCTION__);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,23 @@ public function testReplacingAPreDefinedServiceIsDeprecated()
538538

539539
$this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service');
540540
}
541+
542+
public function testNoInfiniteLoopInGetEnv()
543+
{
544+
$_ENV['bar'] = 'hello';
545+
$this->assertCount(1, (new RemovedIdsCallGetEnvContainer())->getRemovedIds());
546+
unset($_ENV['bar']);
547+
}
548+
}
549+
550+
class RemovedIdsCallGetEnvContainer extends Container
551+
{
552+
public function getRemovedIds()
553+
{
554+
return array(
555+
'foo.'.$this->getEnv('bar') => true,
556+
);
557+
}
541558
}
542559

543560
class ProjectServiceContainer extends Container

0 commit comments

Comments
 (0)
0