From 954247d55011514c9ba404a6c4ba51115ce4918e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Mon, 10 Aug 2015 12:33:01 +0200 Subject: [PATCH] [DI] Trigger a deprecated error on the container builder --- .../DependencyInjection/ContainerBuilder.php | 4 ++++ .../Tests/ContainerBuilderTest.php | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 5ac89cde57..dcd1be632a 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -931,6 +931,10 @@ class ContainerBuilder extends Container implements TaggedContainerInterface throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id)); } + if ($definition->isDeprecated()) { + @trigger_error(sprintf('The service %s relies on a deprecated definition. You should avoid using it.', $id), E_USER_DEPRECATED); + } + if ($tryProxy && $definition->isLazy()) { $container = $this; diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 7e5564c92b..8853dd4d08 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -63,6 +63,28 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase } } + public function testCreateDeprecatedService() + { + $definition = new Definition('stdClass'); + $definition->setDeprecated(true); + + $that = $this; + $wasTriggered = false; + + set_error_handler(function ($errno, $errstr) use ($that, &$wasTriggered) { + $that->assertSame(E_USER_DEPRECATED, $errno); + $that->assertSame('The service deprecated_foo relies on a deprecated definition. You should avoid using it.', $errstr); + $wasTriggered = true; + }); + + $builder = new ContainerBuilder(); + $builder->createService($definition, 'deprecated_foo'); + + restore_error_handler(); + + $this->assertTrue($wasTriggered); + } + /** * @covers Symfony\Component\DependencyInjection\ContainerBuilder::register */