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) { 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() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy