[DI] Support deprecated definitions in decorators

This commit is contained in:
Baptiste Clavié 2015-09-09 11:35:13 +02:00
parent 0b3d0a0bd9
commit 83f4e9cf45
3 changed files with 46 additions and 0 deletions

View File

@ -127,6 +127,9 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
if ($parentDef->getFactoryService(false)) {
$def->setFactoryService($parentDef->getFactoryService(false));
}
if ($parentDef->isDeprecated()) {
$def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
}
$def->setFactory($parentDef->getFactory());
$def->setConfigurator($parentDef->getConfigurator());
$def->setFile($parentDef->getFile());
@ -162,6 +165,9 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
if (isset($changes['lazy'])) {
$def->setLazy($definition->isLazy());
}
if (isset($changes['deprecated'])) {
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
}
if (isset($changes['decorated_service'])) {
$decoratedService = $definition->getDecoratedService();
if (null === $decoratedService) {

View File

@ -180,6 +180,16 @@ class DefinitionDecorator extends Definition
return parent::setDecoratedService($id, $renamedId, $priority);
}
/**
* {@inheritdoc}
*/
public function setDeprecated($boolean = true, $template = null)
{
$this->changes['deprecated'] = true;
return parent::setDeprecated($boolean, $template);
}
/**
* Gets an argument to pass to the service constructor/factory method.
*

View File

@ -244,6 +244,36 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo', 'foo_inner', 0), $container->getDefinition('child1')->getDecoratedService());
}
public function testDecoratedServiceCopiesDeprecatedStatusFromParent()
{
$container = new ContainerBuilder();
$container->register('deprecated_parent')
->setDeprecated(true)
;
$container->setDefinition('decorated_deprecated_parent', new DefinitionDecorator('deprecated_parent'));
$this->process($container);
$this->assertTrue($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
}
public function testDecoratedServiceCanOverwriteDeprecatedParentStatus()
{
$container = new ContainerBuilder();
$container->register('deprecated_parent')
->setDeprecated(true)
;
$container->setDefinition('decorated_deprecated_parent', new DefinitionDecorator('deprecated_parent'))
->setDeprecated(false)
;
$this->process($container);
$this->assertFalse($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
}
protected function process(ContainerBuilder $container)
{
$pass = new ResolveDefinitionTemplatesPass();