diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 3cfdd616c5a17..f6c953f550993 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -223,6 +223,7 @@ public function has($id) if ('service_container' === $id || isset($this->aliases[$id]) || isset($this->services[$id]) + || array_key_exists($id, $this->services) ) { return true; } @@ -265,7 +266,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE $id = $this->aliases[$id]; } // Re-use shared service instance if it exists. - if (isset($this->services[$id])) { + if (isset($this->services[$id]) || array_key_exists($id, $this->services)) { return $this->services[$id]; } @@ -347,7 +348,7 @@ public function initialized($id) $id = $this->aliases[$id]; } - return isset($this->services[$id]); + return isset($this->services[$id]) || array_key_exists($id, $this->services); } /** diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 70d1efda4cad7..0813110deda8f 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -436,7 +436,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV return $service; } - if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) { + if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) { return $this->get($this->aliasDefinitions[$id]); } @@ -784,7 +784,7 @@ public function setDefinition($id, Definition $definition) */ public function hasDefinition($id) { - return isset($this->definitions[strtolower($id)]); + return array_key_exists(strtolower($id), $this->definitions); } /** @@ -800,7 +800,7 @@ public function getDefinition($id) { $id = strtolower($id); - if (!isset($this->definitions[$id])) { + if (!array_key_exists($id, $this->definitions)) { throw new ServiceNotFoundException($id); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 013ee82316084..e451806bb7807 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -256,6 +256,18 @@ public function testGetReturnsNullOnInactiveScope() $this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE)); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException + * @expectedExcepionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service. + */ + public function testGetSyntheticServiceAlwaysThrows() + { + require_once __DIR__.'/Fixtures/php/services9.php'; + + $container = new \ProjectServiceContainer(); + $container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE); + } + public function testHas() { $sc = new ProjectServiceContainer(); @@ -287,14 +299,17 @@ public function testEnterLeaveCurrentScope() $container->addScope(new Scope('foo')); $container->enterScope('foo'); + $container->set('foo', new \stdClass(), 'foo'); $scoped1 = $container->get('scoped'); $scopedFoo1 = $container->get('scoped_foo'); $container->enterScope('foo'); + $container->set('foo', new \stdClass(), 'foo'); $scoped2 = $container->get('scoped'); $scoped3 = $container->get('SCOPED'); $scopedFoo2 = $container->get('scoped_foo'); + $container->set('foo', null, 'foo'); $container->leaveScope('foo'); $scoped4 = $container->get('scoped'); $scopedFoo3 = $container->get('scoped_foo'); @@ -641,6 +656,12 @@ protected function getScopedSynchronizedFooService() return $this->services['scoped_bar'] = $this->scopedServices['foo']['scoped_bar'] = new \stdClass(); } + protected function synchronizeFooService() + { + // Typically get the service to pass it to a setter + $this->get('foo'); + } + protected function synchronizeScopedSynchronizedFooService() { $this->synchronized = true;