Skip to content

Commit 564af72

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [Translation] Throw exception if symfony/finder is not installed for PhpExtractor fix tests Fix tests failing with DBAL 3 Fix circular reference in autowired decorators [Security] Do not deauthenticate token on user change if not an AbstractToken
2 parents ef3659e + 4b8d9fd commit 564af72

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Compiler/AutowirePass.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot,
150150
$this->decoratedClass = null;
151151
$this->getPreviousValue = null;
152152

153-
if ($isRoot && ($definition = $this->container->getDefinition($this->currentId)) && null !== ($this->decoratedId = $definition->innerServiceId) && $this->container->has($this->decoratedId)) {
154-
$this->decoratedClass = $this->container->findDefinition($this->decoratedId)->getClass();
153+
if ($isRoot && ($definition = $this->container->getDefinition($this->currentId)) && ($decoratedDefinition = $definition->getDecoratedService()) && null !== ($innerId = $decoratedDefinition[0]) && $this->container->has($innerId)) {
154+
// If the class references to itself and is decorated, provide the inner service id and class to not get a circular reference
155+
$this->decoratedClass = $this->container->findDefinition($innerId)->getClass();
156+
$this->decoratedId = $decoratedDefinition[1] ?? $this->currentId.'.inner';
155157
}
156158

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

Tests/Compiler/AutowirePassTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,8 @@ public function testAutowireDecorator()
951951
->setAutowired(true)
952952
;
953953

954-
(new DecoratorServicePass())->process($container);
955954
(new AutowirePass())->process($container);
955+
(new DecoratorServicePass())->process($container);
956956

957957
$definition = $container->getDefinition(Decorator::class);
958958
$this->assertSame(Decorator::class.'.inner', (string) $definition->getArgument(1));
@@ -974,8 +974,8 @@ public function testAutowireDecoratorChain()
974974
->setAutowired(true)
975975
;
976976

977-
(new DecoratorServicePass())->process($container);
978977
(new AutowirePass())->process($container);
978+
(new DecoratorServicePass())->process($container);
979979

980980
$definition = $container->getDefinition(DecoratedDecorator::class);
981981
$this->assertSame(DecoratedDecorator::class.'.inner', (string) $definition->getArgument(0));
@@ -992,8 +992,8 @@ public function testAutowireDecoratorRenamedId()
992992
->setAutowired(true)
993993
;
994994

995-
(new DecoratorServicePass())->process($container);
996995
(new AutowirePass())->process($container);
996+
(new DecoratorServicePass())->process($container);
997997

998998
$definition = $container->getDefinition(Decorator::class);
999999
$this->assertSame('renamed', (string) $definition->getArgument(1));
@@ -1010,12 +1010,11 @@ public function testDoNotAutowireDecoratorWhenSeveralArgumentOfTheType()
10101010
->setAutowired(true)
10111011
;
10121012

1013-
(new DecoratorServicePass())->process($container);
10141013
try {
10151014
(new AutowirePass())->process($container);
10161015
$this->fail('AutowirePass should have thrown an exception');
10171016
} catch (AutowiringFailedException $e) {
1018-
$this->assertSame('Cannot autowire service "Symfony\Component\DependencyInjection\Tests\Compiler\NonAutowirableDecorator": argument "$decorated1" of method "__construct()" references interface "Symfony\Component\DependencyInjection\Tests\Compiler\DecoratorInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "Symfony\Component\DependencyInjection\Tests\Compiler\NonAutowirableDecorator", "Symfony\Component\DependencyInjection\Tests\Compiler\NonAutowirableDecorator.inner".', (string) $e->getMessage());
1017+
$this->assertSame('Cannot autowire service "Symfony\Component\DependencyInjection\Tests\Compiler\NonAutowirableDecorator": argument "$decorated1" of method "__construct()" references interface "Symfony\Component\DependencyInjection\Tests\Compiler\DecoratorInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "Symfony\Component\DependencyInjection\Tests\Compiler\Decorated", "Symfony\Component\DependencyInjection\Tests\Compiler\NonAutowirableDecorator".', (string) $e->getMessage());
10191018
}
10201019
}
10211020

@@ -1069,4 +1068,23 @@ public function testArgumentWithTarget()
10691068

10701069
$this->assertSame(BarInterface::class.' $imageStorage', (string) $container->getDefinition('with_target')->getArgument(0));
10711070
}
1071+
1072+
public function testDecorationWithServiceAndAliasedInterface()
1073+
{
1074+
$container = new ContainerBuilder();
1075+
1076+
$container->register(DecoratorImpl::class, DecoratorImpl::class)
1077+
->setAutowired(true)
1078+
->setPublic(true);
1079+
$container->setAlias(DecoratorInterface::class, DecoratorImpl::class)->setPublic(true);
1080+
$container->register(DecoratedDecorator::class, DecoratedDecorator::class)
1081+
->setAutowired(true)
1082+
->setPublic(true)
1083+
->setDecoratedService(DecoratorImpl::class);
1084+
1085+
$container->compile();
1086+
1087+
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorInterface::class));
1088+
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorImpl::class));
1089+
}
10721090
}

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ interface DecoratorInterface
376376
{
377377
}
378378

379+
class DecoratorImpl implements DecoratorInterface
380+
{
381+
}
382+
379383
class Decorated implements DecoratorInterface
380384
{
381385
public function __construct($quz = null, \NonExistent $nonExistent = null, DecoratorInterface $decorated = null, array $foo = [])

0 commit comments

Comments
 (0)