From edd76494578eab1033936acfeeebd927677afb5f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 17 Apr 2013 13:21:11 +0200 Subject: [PATCH] [DependencyInjection] fixed management of scoped services with an invalid behavior set to null (closes #7636) --- .../DependencyInjection/ContainerBuilder.php | 11 +++++++++++ .../Tests/ContainerBuilderTest.php | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index b083860502d8c..5d8d0d1a10f15 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -430,6 +430,12 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV try { return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE); + } catch (InactiveScopeException $e) { + if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { + return null; + } + + throw $e; } catch (InvalidArgumentException $e) { if (isset($this->loading[$id])) { 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 $service = $this->createService($definition, $id); } catch (\Exception $e) { unset($this->loading[$id]); + + if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { + return null; + } + throw $e; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index af0181ecc1f1b..7d2cb278871cc 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -138,6 +138,17 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException() $builder->get('foo'); } + /** + * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get + */ + public function testGetReturnsNullOnInactiveScope() + { + $builder = new ContainerBuilder(); + $builder->register('foo', 'stdClass')->setScope('request'); + + $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE)); + } + /** * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds */