From ac742dfc48fd45fb3307abb8f3b3d4f97941591c Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 4 Sep 2016 19:06:49 +0200 Subject: [PATCH 1/2] Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)" This reverts commit c89f80a9cd3f291159ba5e466c7c968cf15d476a, reversing changes made to 386e5e78b4ad8356fe01ac5406872a56b5177134. --- src/Symfony/Component/DependencyInjection/Container.php | 5 +++-- .../Component/DependencyInjection/ContainerBuilder.php | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 3cfdd616c5..f6c953f550 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -223,6 +223,7 @@ class Container implements IntrospectableContainerInterface if ('service_container' === $id || isset($this->aliases[$id]) || isset($this->services[$id]) + || array_key_exists($id, $this->services) ) { return true; } @@ -265,7 +266,7 @@ class Container implements IntrospectableContainerInterface $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 @@ class Container implements IntrospectableContainerInterface $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 70d1efda4c..0813110ded 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -436,7 +436,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface 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 @@ class ContainerBuilder extends Container implements TaggedContainerInterface */ public function hasDefinition($id) { - return isset($this->definitions[strtolower($id)]); + return array_key_exists(strtolower($id), $this->definitions); } /** @@ -800,7 +800,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface { $id = strtolower($id); - if (!isset($this->definitions[$id])) { + if (!array_key_exists($id, $this->definitions)) { throw new ServiceNotFoundException($id); } From 8cb28bf7517d1c2e27da4d3eeb557fbffedc1ec8 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 5 Sep 2016 09:18:51 +0200 Subject: [PATCH 2/2] [DI] Add anti-regression test --- .../Tests/ContainerTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 013ee82316..e451806bb7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -256,6 +256,18 @@ class ContainerTest extends \PHPUnit_Framework_TestCase $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 @@ class ContainerTest extends \PHPUnit_Framework_TestCase $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 @@ class ProjectServiceContainer extends Container 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;