Skip to content

Commit 954247d

Browse files
committed
[DI] Trigger a deprecated error on the container builder
1 parent 2f37cb1 commit 954247d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ public function createService(Definition $definition, $id, $tryProxy = true)
931931
throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
932932
}
933933

934+
if ($definition->isDeprecated()) {
935+
@trigger_error(sprintf('The service %s relies on a deprecated definition. You should avoid using it.', $id), E_USER_DEPRECATED);
936+
}
937+
934938
if ($tryProxy && $definition->isLazy()) {
935939
$container = $this;
936940

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ public function testDefinitions()
6363
}
6464
}
6565

66+
public function testCreateDeprecatedService()
67+
{
68+
$definition = new Definition('stdClass');
69+
$definition->setDeprecated(true);
70+
71+
$that = $this;
72+
$wasTriggered = false;
73+
74+
set_error_handler(function ($errno, $errstr) use ($that, &$wasTriggered) {
75+
$that->assertSame(E_USER_DEPRECATED, $errno);
76+
$that->assertSame('The service deprecated_foo relies on a deprecated definition. You should avoid using it.', $errstr);
77+
$wasTriggered = true;
78+
});
79+
80+
$builder = new ContainerBuilder();
81+
$builder->createService($definition, 'deprecated_foo');
82+
83+
restore_error_handler();
84+
85+
$this->assertTrue($wasTriggered);
86+
}
87+
6688
/**
6789
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::register
6890
*/

0 commit comments

Comments
 (0)