Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Nested AutowireLocator is not working #59758

Copy link
Copy link
Open
@deguif

Description

@deguif
Issue body actions

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' => '?',
        ]));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.