[DependencyInjection] Autowiring: support parent/decorators
This commit is contained in:
parent
8c4e756c1b
commit
71d502a174
@ -138,15 +138,15 @@ class AutowirePass implements CompilerPassInterface
|
|||||||
*/
|
*/
|
||||||
private function populateAvailableType($id, Definition $definition)
|
private function populateAvailableType($id, Definition $definition)
|
||||||
{
|
{
|
||||||
if (!$definition->getClass()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($definition->getAutowiringTypes() as $type) {
|
foreach ($definition->getAutowiringTypes() as $type) {
|
||||||
$this->definedTypes[$type] = true;
|
$this->definedTypes[$type] = true;
|
||||||
$this->types[$type] = $id;
|
$this->types[$type] = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$definition->getClass()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
|
if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
|
||||||
$this->extractInterfaces($id, $reflectionClass);
|
$this->extractInterfaces($id, $reflectionClass);
|
||||||
$this->extractAncestors($id, $reflectionClass);
|
$this->extractAncestors($id, $reflectionClass);
|
||||||
|
@ -118,6 +118,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
|
|||||||
$def->setArguments($parentDef->getArguments());
|
$def->setArguments($parentDef->getArguments());
|
||||||
$def->setMethodCalls($parentDef->getMethodCalls());
|
$def->setMethodCalls($parentDef->getMethodCalls());
|
||||||
$def->setProperties($parentDef->getProperties());
|
$def->setProperties($parentDef->getProperties());
|
||||||
|
$def->setAutowiringTypes($parentDef->getAutowiringTypes());
|
||||||
if ($parentDef->getFactoryClass(false)) {
|
if ($parentDef->getFactoryClass(false)) {
|
||||||
$def->setFactoryClass($parentDef->getFactoryClass(false));
|
$def->setFactoryClass($parentDef->getFactoryClass(false));
|
||||||
}
|
}
|
||||||
@ -202,6 +203,11 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
|
|||||||
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
|
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// merge autowiring types
|
||||||
|
foreach ($definition->getAutowiringTypes() as $autowiringType) {
|
||||||
|
$def->addAutowiringType($autowiringType);
|
||||||
|
}
|
||||||
|
|
||||||
// these attributes are always taken from the child
|
// these attributes are always taken from the child
|
||||||
$def->setAbstract($definition->isAbstract());
|
$def->setAbstract($definition->isAbstract());
|
||||||
$def->setScope($definition->getScope(false), false);
|
$def->setScope($definition->getScope(false), false);
|
||||||
|
@ -189,7 +189,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('foo', $definition->getArgument(2));
|
$this->assertEquals('foo', $definition->getArgument(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDontTriggeruAutowiring()
|
public function testDontTriggerAutowiring()
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
@ -274,6 +274,26 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
|
$this->assertFalse($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testProcessMergeAutowiringTypes()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->addAutowiringType('Foo')
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new DefinitionDecorator('parent'))
|
||||||
|
->addAutowiringType('Bar')
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertEquals(array('Foo', 'Bar'), $def->getAutowiringTypes());
|
||||||
|
}
|
||||||
|
|
||||||
protected function process(ContainerBuilder $container)
|
protected function process(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$pass = new ResolveDefinitionTemplatesPass();
|
$pass = new ResolveDefinitionTemplatesPass();
|
||||||
|
Reference in New Issue
Block a user