[DI] Trigger a deprecated error on the container builder

This commit is contained in:
Baptiste Clavié 2015-08-10 12:33:01 +02:00
parent 2f37cb184f
commit 954247d550
2 changed files with 26 additions and 0 deletions

View File

@ -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;

View File

@ -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
*/