Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 441322f

Browse files
bug #28159 [DI] Fix autowire inner service (hason)
This PR was merged into the 4.1 branch. Discussion ---------- [DI] Fix autowire inner service | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25631 | License | MIT | Doc PR | - This PR fix multiple levels of decoration. Unfortunately, this [good question](#25631 (comment)) in origin PR has not been heard 🎧 😄. @dunglas @chalasr Commits ------- b79d097 [DI] Fix autowire inner service
2 parents a31b5d0 + b79d097 commit 441322f

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ private function doProcessValue($value, $isRoot = false)
144144
*/
145145
private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot): array
146146
{
147+
$this->decoratedId = null;
148+
$this->decoratedClass = null;
149+
$this->getPreviousValue = null;
150+
147151
if ($isRoot && ($definition = $this->container->getDefinition($this->currentId)) && $this->container->has($this->decoratedId = $definition->innerServiceId)) {
148152
$this->decoratedClass = $this->container->findDefinition($this->decoratedId)->getClass();
149-
} else {
150-
$this->decoratedId = null;
151-
$this->decoratedClass = null;
152153
}
153154

154155
foreach ($this->methodCalls as $i => $call) {

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,29 @@ public function testAutowireDecorator()
834834
$this->assertSame(Decorator::class.'.inner', (string) $definition->getArgument(1));
835835
}
836836

837+
public function testAutowireDecoratorChain()
838+
{
839+
$container = new ContainerBuilder();
840+
$container->register(LoggerInterface::class, NullLogger::class);
841+
$container->register(Decorated::class, Decorated::class);
842+
$container
843+
->register(Decorator::class, Decorator::class)
844+
->setDecoratedService(Decorated::class)
845+
->setAutowired(true)
846+
;
847+
$container
848+
->register(DecoratedDecorator::class, DecoratedDecorator::class)
849+
->setDecoratedService(Decorated::class)
850+
->setAutowired(true)
851+
;
852+
853+
(new DecoratorServicePass())->process($container);
854+
(new AutowirePass())->process($container);
855+
856+
$definition = $container->getDefinition(DecoratedDecorator::class);
857+
$this->assertSame(DecoratedDecorator::class.'.inner', (string) $definition->getArgument(0));
858+
}
859+
837860
public function testAutowireDecoratorRenamedId()
838861
{
839862
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ public function __construct(LoggerInterface $logger, DecoratorInterface $decorat
373373
}
374374
}
375375

376+
class DecoratedDecorator implements DecoratorInterface
377+
{
378+
public function __construct(DecoratorInterface $decorator)
379+
{
380+
}
381+
}
382+
376383
class NonAutowirableDecorator implements DecoratorInterface
377384
{
378385
public function __construct(LoggerInterface $logger, DecoratorInterface $decorated1, DecoratorInterface $decorated2)

0 commit comments

Comments
 (0)