Skip to content

Commit

Permalink
bug #19643 [DependencyInjection] Fix service autowiring inheritance (…
Browse files Browse the repository at this point in the history
…chalasr)

This PR was merged into the 2.8 branch.

Discussion
----------

[DependencyInjection] Fix service autowiring inheritance

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19615
| License       | MIT
| Doc PR        | n/a

This makes services inherit the `autowire` attribute from their parent and fix the ability to override it from the child.

Fixed cases:

- Simple inheritance
```yaml
parent:
    class: Foo
    abstract: true
    autowire: true

child:
    class: Foo
```

- Set in the child (only)
```yaml
parent:
    class: Foo
    abstract: true

child:
    class: Foo
    autowire: true
```

- Set in the parent, changed in the child
```yaml
parent:
    class: Foo
    abstract: true
    autowire: true

child:
    class: Foo
    autowire: false
```

Commits
-------

fb95bdc [DIC] Fix service autowiring inheritance
  • Loading branch information
fabpot committed Aug 19, 2016
2 parents fad374e + fb95bdc commit fc483cf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Expand Up @@ -136,6 +136,7 @@ private function resolveDefinition(ContainerBuilder $container, DefinitionDecora
$def->setFile($parentDef->getFile());
$def->setPublic($parentDef->isPublic());
$def->setLazy($parentDef->isLazy());
$def->setAutowired($parentDef->isAutowired());

// overwrite with values specified in the decorator
$changes = $definition->getChanges();
Expand Down Expand Up @@ -169,6 +170,9 @@ private function resolveDefinition(ContainerBuilder $container, DefinitionDecora
if (isset($changes['deprecated'])) {
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
}
if (isset($changes['autowire'])) {
$def->setAutowired($definition->isAutowired());
}
if (isset($changes['decorated_service'])) {
$decoratedService = $definition->getDecoratedService();
if (null === $decoratedService) {
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/DependencyInjection/DefinitionDecorator.php
Expand Up @@ -164,6 +164,16 @@ public function setDeprecated($boolean = true, $template = null)
return parent::setDeprecated($boolean, $template);
}

/**
* {@inheritdoc}
*/
public function setAutowired($autowired)
{
$this->changes['autowire'] = true;

return parent::setAutowired($autowired);
}

/**
* Gets an argument to pass to the service constructor/factory method.
*
Expand Down
Expand Up @@ -233,6 +233,36 @@ public function testSetLazyOnServiceIsParent()
$this->assertTrue($container->getDefinition('child1')->isLazy());
}

public function testSetAutowiredOnServiceHasParent()
{
$container = new ContainerBuilder();

$container->register('parent', 'stdClass');

$container->setDefinition('child1', new DefinitionDecorator('parent'))
->setAutowired(true)
;

$this->process($container);

$this->assertTrue($container->getDefinition('child1')->isAutowired());
}

public function testSetAutowiredOnServiceIsParent()
{
$container = new ContainerBuilder();

$container->register('parent', 'stdClass')
->setAutowired(true)
;

$container->setDefinition('child1', new DefinitionDecorator('parent'));

$this->process($container);

$this->assertTrue($container->getDefinition('child1')->isAutowired());
}

public function testDeepDefinitionsResolving()
{
$container = new ContainerBuilder();
Expand Down
Expand Up @@ -95,6 +95,16 @@ public function testSetLazy()
$this->assertEquals(array('lazy' => true), $def->getChanges());
}

public function testSetAutowired()
{
$def = new DefinitionDecorator('foo');

$this->assertFalse($def->isAutowired());
$this->assertSame($def, $def->setAutowired(false));
$this->assertFalse($def->isAutowired());
$this->assertEquals(array('autowire' => true), $def->getChanges());
}

public function testSetArgument()
{
$def = new DefinitionDecorator('foo');
Expand Down

0 comments on commit fc483cf

Please sign in to comment.