[DI] Fix autowire inner service

This commit is contained in:
Martin Hasoň 2018-08-08 13:02:30 +02:00
parent cfc112a484
commit b79d097c2a
3 changed files with 34 additions and 3 deletions

View File

@ -144,11 +144,12 @@ class AutowirePass extends AbstractRecursivePass
*/ */
private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot): array private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot): array
{ {
$this->decoratedId = null;
$this->decoratedClass = null;
$this->getPreviousValue = null;
if ($isRoot && ($definition = $this->container->getDefinition($this->currentId)) && $this->container->has($this->decoratedId = $definition->innerServiceId)) { if ($isRoot && ($definition = $this->container->getDefinition($this->currentId)) && $this->container->has($this->decoratedId = $definition->innerServiceId)) {
$this->decoratedClass = $this->container->findDefinition($this->decoratedId)->getClass(); $this->decoratedClass = $this->container->findDefinition($this->decoratedId)->getClass();
} else {
$this->decoratedId = null;
$this->decoratedClass = null;
} }
foreach ($this->methodCalls as $i => $call) { foreach ($this->methodCalls as $i => $call) {

View File

@ -834,6 +834,29 @@ class AutowirePassTest extends TestCase
$this->assertSame(Decorator::class.'.inner', (string) $definition->getArgument(1)); $this->assertSame(Decorator::class.'.inner', (string) $definition->getArgument(1));
} }
public function testAutowireDecoratorChain()
{
$container = new ContainerBuilder();
$container->register(LoggerInterface::class, NullLogger::class);
$container->register(Decorated::class, Decorated::class);
$container
->register(Decorator::class, Decorator::class)
->setDecoratedService(Decorated::class)
->setAutowired(true)
;
$container
->register(DecoratedDecorator::class, DecoratedDecorator::class)
->setDecoratedService(Decorated::class)
->setAutowired(true)
;
(new DecoratorServicePass())->process($container);
(new AutowirePass())->process($container);
$definition = $container->getDefinition(DecoratedDecorator::class);
$this->assertSame(DecoratedDecorator::class.'.inner', (string) $definition->getArgument(0));
}
public function testAutowireDecoratorRenamedId() public function testAutowireDecoratorRenamedId()
{ {
$container = new ContainerBuilder(); $container = new ContainerBuilder();

View File

@ -373,6 +373,13 @@ class Decorator implements DecoratorInterface
} }
} }
class DecoratedDecorator implements DecoratorInterface
{
public function __construct(DecoratorInterface $decorator)
{
}
}
class NonAutowirableDecorator implements DecoratorInterface class NonAutowirableDecorator implements DecoratorInterface
{ {
public function __construct(LoggerInterface $logger, DecoratorInterface $decorated1, DecoratorInterface $decorated2) public function __construct(LoggerInterface $logger, DecoratorInterface $decorated1, DecoratorInterface $decorated2)