Skip to content

Commit 1ecfd57

Browse files
Merge branch '5.4' into 6.0
* 5.4: (26 commits) [Dotenv] Fix testBootEnv() to start from a fresh context fix tests relax expected exception message for forward-compatibility with 5.4 fix expected exception messages after changes made in Definition class [DependencyInjection] show class name on DI errors skip command completion tests with older Symfony Console versions Use GitHub issue form templates Fix CS add ResponseIsUnprocessable Add missing translations for Persian (fa) skip command completion tests with older Symfony Console versions prevent issues with timezones and DST by using only UNIX timestamps Add the missing translations for Bahasa Indonesia (id) [Finder] Fix .gitignore infinite loop Update README.md fix messenger DI dependency for registerAttributeForAutoconfiguration [Messenger] Autoconfigurable attributes Fix deprecations on PHP 8.2 [Dotenv] Fix testLoadEnv() .env.dist isolation Since 5.0, throws \UnexpectedValueException has been removed. ...
2 parents a689715 + 497eb27 commit 1ecfd57

File tree

8 files changed

+44
-15
lines changed

8 files changed

+44
-15
lines changed

Compiler/AutowirePass.php

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

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

169167
$patchedIndexes = [];

Compiler/PassConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ public function __construct()
6262
new AutowireRequiredMethodsPass(),
6363
new AutowireRequiredPropertiesPass(),
6464
new ResolveBindingsPass(),
65+
new ServiceLocatorTagPass(),
66+
new DecoratorServicePass(),
6567
new CheckDefinitionValidityPass(),
6668
new AutowirePass(false),
6769
new ServiceLocatorTagPass(),
68-
new DecoratorServicePass(),
6970
new ResolveTaggedIteratorArgumentPass(),
7071
new ResolveServiceSubscribersPass(),
7172
new ResolveReferencesToAliasesPass(),

Definition.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ public function addArgument(mixed $argument): static
252252
public function replaceArgument(int|string $index, mixed $argument): static
253253
{
254254
if (0 === \count($this->arguments)) {
255-
throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
255+
throw new OutOfBoundsException(sprintf('Cannot replace arguments for class "%s" if none have been configured yet.', $this->class));
256256
}
257257

258258
if (\is_int($index) && ($index < 0 || $index > \count($this->arguments) - 1)) {
259-
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, \count($this->arguments) - 1));
259+
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d] of the arguments of class "%s".', $index, \count($this->arguments) - 1, $this->class));
260260
}
261261

262262
if (!\array_key_exists($index, $this->arguments)) {
263-
throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
263+
throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist in class "%s".', $index, $this->class));
264264
}
265265

266266
$this->arguments[$index] = $argument;
@@ -296,7 +296,7 @@ public function getArguments(): array
296296
public function getArgument(int|string $index): mixed
297297
{
298298
if (!\array_key_exists($index, $this->arguments)) {
299-
throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
299+
throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist in class "%s".', $index, $this->class));
300300
}
301301

302302
return $this->arguments[$index];

Tests/Compiler/AutowirePassTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,8 @@ public function testAutowireDecorator()
990990
->setAutowired(true)
991991
;
992992

993-
(new AutowirePass())->process($container);
994993
(new DecoratorServicePass())->process($container);
994+
(new AutowirePass())->process($container);
995995

996996
$definition = $container->getDefinition(Decorator::class);
997997
$this->assertSame(Decorator::class.'.inner', (string) $definition->getArgument(1));
@@ -1013,8 +1013,8 @@ public function testAutowireDecoratorChain()
10131013
->setAutowired(true)
10141014
;
10151015

1016-
(new AutowirePass())->process($container);
10171016
(new DecoratorServicePass())->process($container);
1017+
(new AutowirePass())->process($container);
10181018

10191019
$definition = $container->getDefinition(DecoratedDecorator::class);
10201020
$this->assertSame(DecoratedDecorator::class.'.inner', (string) $definition->getArgument(0));
@@ -1031,8 +1031,8 @@ public function testAutowireDecoratorRenamedId()
10311031
->setAutowired(true)
10321032
;
10331033

1034-
(new AutowirePass())->process($container);
10351034
(new DecoratorServicePass())->process($container);
1035+
(new AutowirePass())->process($container);
10361036

10371037
$definition = $container->getDefinition(Decorator::class);
10381038
$this->assertSame('renamed', (string) $definition->getArgument(1));
@@ -1049,11 +1049,12 @@ public function testDoNotAutowireDecoratorWhenSeveralArgumentOfTheType()
10491049
->setAutowired(true)
10501050
;
10511051

1052+
(new DecoratorServicePass())->process($container);
10521053
try {
10531054
(new AutowirePass())->process($container);
10541055
$this->fail('AutowirePass should have thrown an exception');
10551056
} catch (AutowiringFailedException $e) {
1056-
$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());
1057+
$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());
10571058
}
10581059
}
10591060

Tests/Compiler/IntegrationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,33 @@ public function testCanDecorateServiceLocator()
186186
$this->assertSame($container->get('foo'), $container->get(DecoratedServiceLocator::class)->get('foo'));
187187
}
188188

189+
public function testAliasDecoratedService()
190+
{
191+
$container = new ContainerBuilder();
192+
193+
$container->register('service', ServiceLocator::class)
194+
->setPublic(true)
195+
->setArguments([[]])
196+
;
197+
$container->register('decorator', DecoratedServiceLocator::class)
198+
->setDecoratedService('service')
199+
->setAutowired(true)
200+
->setPublic(true)
201+
;
202+
$container->setAlias(ServiceLocator::class, 'decorator.inner')
203+
->setPublic(true)
204+
;
205+
$container->register('user_service', DecoratedServiceLocator::class)
206+
->setAutowired(true)
207+
;
208+
209+
$container->compile();
210+
211+
$this->assertInstanceOf(DecoratedServiceLocator::class, $container->get('service'));
212+
$this->assertInstanceOf(ServiceLocator::class, $container->get(ServiceLocator::class));
213+
$this->assertSame($container->get('service'), $container->get('decorator'));
214+
}
215+
189216
/**
190217
* @dataProvider getYamlCompileTests
191218
*/

Tests/DefinitionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function testGetArgumentShouldCheckBounds()
284284
public function testReplaceArgumentShouldCheckBounds()
285285
{
286286
$this->expectException(\OutOfBoundsException::class);
287-
$this->expectExceptionMessage('The index "1" is not in the range [0, 0].');
287+
$this->expectExceptionMessage('The index "1" is not in the range [0, 0] of the arguments of class "stdClass".');
288288
$def = new Definition('stdClass');
289289

290290
$def->addArgument('foo');
@@ -294,7 +294,7 @@ public function testReplaceArgumentShouldCheckBounds()
294294
public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
295295
{
296296
$this->expectException(\OutOfBoundsException::class);
297-
$this->expectExceptionMessage('Cannot replace arguments if none have been configured yet.');
297+
$this->expectExceptionMessage('Cannot replace arguments for class "stdClass" if none have been configured yet.');
298298
$def = new Definition('stdClass');
299299
$def->replaceArgument(0, 'bar');
300300
}

Tests/Fixtures/php/services_subscriber.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function isCompiled(): bool
4444
public function getRemovedIds(): array
4545
{
4646
return [
47+
'.service_locator.DlIAmAe' => true,
4748
'.service_locator.t5IGRMW' => true,
4849
'.service_locator.zFfA7ng' => true,
4950
'.service_locator.zFfA7ng.foo_service' => true,

Tests/Fixtures/php/services_subscriber_php81.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function isCompiled(): bool
4444
public function getRemovedIds(): array
4545
{
4646
return [
47+
'.service_locator.JmEob1b' => true,
4748
'.service_locator.KIgkoLM' => true,
4849
'.service_locator.qUb.lJI' => true,
4950
'.service_locator.qUb.lJI.foo_service' => true,

0 commit comments

Comments
 (0)