bug #21072 [DI] Fix method autowiring in ResolveDefinitionTemplatesPass (dunglas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Fix method autowiring in ResolveDefinitionTemplatesPass

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

cc @nicolas-grekas

Commits
-------

57661e4 [DI] Fix method autowiring in ResolveDefinitionTemplatesPass
This commit is contained in:
Fabien Potencier 2016-12-30 07:43:06 +01:00
commit 61a67ecc5a
5 changed files with 21 additions and 20 deletions

View File

@ -137,11 +137,11 @@ class ChildDefinition extends Definition
/**
* {@inheritdoc}
*/
public function setAutowired($autowired)
public function setAutowiredMethods(array $autowiredMethods)
{
$this->changes['autowire'] = true;
$this->changes['autowired_methods'] = true;
return parent::setAutowired($autowired);
return parent::setAutowiredMethods($autowiredMethods);
}
/**

View File

@ -141,7 +141,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
$def->setFile($parentDef->getFile());
$def->setPublic($parentDef->isPublic());
$def->setLazy($parentDef->isLazy());
$def->setAutowired($parentDef->isAutowired());
$def->setAutowiredMethods($parentDef->getAutowiredMethods());
// overwrite with values specified in the decorator
$changes = $definition->getChanges();
@ -166,8 +166,8 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
if (isset($changes['deprecated'])) {
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
}
if (isset($changes['autowire'])) {
$def->setAutowired($definition->isAutowired());
if (isset($changes['autowired_methods'])) {
$def->setAutowiredMethods($definition->getAutowiredMethods());
}
if (isset($changes['decorated_service'])) {
$decoratedService = $definition->getDecoratedService();
@ -199,7 +199,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
}
// append method calls
if (count($calls = $definition->getMethodCalls()) > 0) {
if ($calls = $definition->getMethodCalls()) {
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
}

View File

@ -70,14 +70,14 @@ class ChildDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('lazy' => true), $def->getChanges());
}
public function testSetAutowired()
public function testSetAutowiredMethods()
{
$def = new ChildDefinition('foo');
$this->assertFalse($def->isAutowired());
$this->assertSame($def, $def->setAutowired(false));
$this->assertFalse($def->isAutowired());
$this->assertSame(array('autowire' => true), $def->getChanges());
$this->assertSame($def, $def->setAutowiredMethods(array('foo', 'bar')));
$this->assertEquals(array('foo', 'bar'), $def->getAutowiredMethods());
$this->assertSame(array('autowired_methods' => true), $def->getChanges());
}
public function testSetArgument()

View File

@ -214,15 +214,16 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
{
$container = new ContainerBuilder();
$container->register('parent', 'stdClass');
$container->register('parent', 'stdClass')
->setAutowiredMethods(array('foo', 'bar'));
$container->setDefinition('child1', new ChildDefinition('parent'))
->setAutowired(true)
->setAutowiredMethods(array('baz'))
;
$this->process($container);
$this->assertTrue($container->getDefinition('child1')->isAutowired());
$this->assertEquals(array('baz'), $container->getDefinition('child1')->getAutowiredMethods());
}
public function testSetAutowiredOnServiceIsParent()
@ -230,14 +231,14 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
$container = new ContainerBuilder();
$container->register('parent', 'stdClass')
->setAutowired(true)
->setAutowiredMethods(array('__construct', 'set*'))
;
$container->setDefinition('child1', new ChildDefinition('parent'));
$this->process($container);
$this->assertTrue($container->getDefinition('child1')->isAutowired());
$this->assertEquals(array('__construct', 'set*'), $container->getDefinition('child1')->getAutowiredMethods());
}
public function testDeepDefinitionsResolving()

View File

@ -72,14 +72,14 @@ class DefinitionDecoratorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('lazy' => true), $def->getChanges());
}
public function testSetAutowired()
public function testSetAutowiredMethods()
{
$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());
$this->assertSame($def, $def->setAutowiredMethods(array('foo', 'bar')));
$this->assertEquals(array('foo', 'bar'), $def->getAutowiredMethods());
$this->assertEquals(array('autowired_methods' => true), $def->getChanges());
}
public function testSetArgument()