Skip to content

Commit c980dbd

Browse files
committed
merged branch fabpot/container-fix (PR symfony#7697)
This PR was merged into the master branch. Discussion ---------- [DependencyInjection] fixed management of scoped services with an invalid behavior set to null | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#7636 | License | MIT | Doc PR | n/a Commits ------- edd7649 [DependencyInjection] fixed management of scoped services with an invalid behavior set to null (closes symfony#7636)
2 parents 30e6fe7 + edd7649 commit c980dbd

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
430430

431431
try {
432432
return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
433+
} catch (InactiveScopeException $e) {
434+
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
435+
return null;
436+
}
437+
438+
throw $e;
433439
} catch (InvalidArgumentException $e) {
434440
if (isset($this->loading[$id])) {
435441
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
@@ -455,6 +461,11 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
455461
$service = $this->createService($definition, $id);
456462
} catch (\Exception $e) {
457463
unset($this->loading[$id]);
464+
465+
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
466+
return null;
467+
}
468+
458469
throw $e;
459470
}
460471

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
138138
$builder->get('foo');
139139
}
140140

141+
/**
142+
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::get
143+
*/
144+
public function testGetReturnsNullOnInactiveScope()
145+
{
146+
$builder = new ContainerBuilder();
147+
$builder->register('foo', 'stdClass')->setScope('request');
148+
149+
$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
150+
}
151+
141152
/**
142153
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds
143154
*/

0 commit comments

Comments
 (0)