-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed as not planned
Description
Symfony version(s) affected
7.2.3
Description
When nesting an AutowireLocator into an AutowireLocator, the result is not as expected.
For AutowireIterator this works.
How to reproduce
An example which doesn't work with nested AutowireLocator
<?php
namespace App;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute as DI;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Contracts\Service\Attribute\SubscribedService;
class Service
{
public function __construct(
#[DI\AutowireLocator([
new SubscribedService(key: 'x', type: ServiceLocator::class, attributes: [new DI\AutowireLocator('tag.y', indexAttribute: 'tag.attribute.y')]),
])]
private readonly ContainerInterface $container,
) {
}
}An example with AutowireIterator which works:
<?php
namespace App;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute as DI;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Contracts\Service\Attribute\SubscribedService;
class Service
{
public function __construct(
#[DI\AutowireLocator([
new SubscribedService(key: 'x', type: 'iterable', attributes: [new DI\AutowireIterator('tag.y')]),
])]
private readonly ContainerInterface $container,
) {
}
}Possible Solution
No response
Additional Context
The compiled service for AutowireLocator
protected static function getService($container)
{
return $container->services['service'] = new \App\Service(new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...), [
'x' => [new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...), [], [])],
], [
'x' => '?',
]));
}
The compiled service for AutowireIterator
protected static function getService($container)
{
return $container->services['service'] = new \App\Service(new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...), [
'x' => [new RewindableGenerator(function () use ($container) {
yield 0 => ($container->privates['y.1'] ?? self::getY1Service($container));
yield 1 => ($container->privates['y.2'] ?? self::getY2Service($container));
}, 2)],
], [
'x' => '?',
]));
}