From bf412ebb881aaf93f7cb7ac6c5914849ddfaf69c Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 23 Aug 2016 13:12:40 +0000 Subject: [PATCH 1/2] allow synthetic before compilation --- .../Component/DependencyInjection/ContainerBuilder.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index cb3789165891a..9eb17c6028caa 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -401,10 +401,6 @@ public function has($id) */ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { - if (!$this->compiled) { - @trigger_error(sprintf('Calling %s() before compiling the container is deprecated since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED); - } - $id = strtolower($id); if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) { @@ -415,6 +411,10 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV return $this->get($this->aliasDefinitions[$id]); } + if (!$this->compiled) { + @trigger_error(sprintf('Calling %s() before compiling the container is deprecated for non-synthetic services since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED); + } + try { $definition = $this->getDefinition($id); } catch (ServiceNotFoundException $e) { From d88a0ff0ae753551b1ce2e3c439a8f095866b0ff Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 23 Aug 2016 18:06:33 +0000 Subject: [PATCH 2/2] added tests --- .../Tests/ContainerBuilderTest.php | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index eb7890b506f6b..65cab12b3dd6a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -144,9 +144,29 @@ public function testGetReturnsRegisteredService() { $builder = new ContainerBuilder(); $builder->set('bar', $bar = new \stdClass()); + + $this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id before compilation'); + $builder->compile(); - $this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id'); + $this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id after compilation'); + } + + /** + * @group legacy + * @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered + */ + public function testGetReturnsRegisteredServiceFromDefinitionBeforeCompilationIsDeprecated() + { + $deprecations = array( + 'Calling Symfony\Component\DependencyInjection\ContainerBuilder::get() before compiling the container is deprecated for non-synthetic services since version 3.2 and will throw an exception in 4.0.', + ); + + ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () { + $builder = new ContainerBuilder(); + $builder->register('foo', 'stdClass'); + $this->assertEquals(new \stdClass(), $builder->get('foo')); + }); } public function testRegisterDoesNotOverrideExistingService()