Skip to content

[DependencyInjection] add #[AutowireLocator] attribute #51392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2023

Conversation

kbond
Copy link
Member

@kbond kbond commented Aug 15, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets n/a
License MIT
Doc PR todo

The AutowireLocator attribute allows configuring service locators inline:

use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;

public function someAction(
    #[AutowireLocator(RouterInterface::class, SluggerInterface::class)]
    ContainerInterface $container,
): Response {
    $container->get(RouterInterface::class);
    $container->get(SluggerInterface::class);
}

You can customize the key and have optional services:

use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;

public function someAction(
    #[AutowireLocator(
        router: RouterInterface::class,
        slugger: '?'.SluggerInterface::class,
    )]
    ContainerInterface $container,
): Response {
    $container->get('router');

    if ($container->has('slugger')) {
        $container->get('slugger');
    }
}

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it, I didn't expect the implementation to be so simple :)

@derrabus
Copy link
Member

In order to address the failing Psalm job, I've opened #51395.

@kbond kbond changed the title [DependencyInjection] add #[Locator] attribute [DependencyInjection] add #[AutowireLocator] attribute Aug 16, 2023
@kbond kbond force-pushed the locator-attribute branch 2 times, most recently from 7d2e338 to 0e0583f Compare August 16, 2023 12:50
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 17, 2023

I'm wondering how we could make these work:

#[AutowireLocator([
	'cache' => new SubscribedService(type: CacheInterface::class: attributes: [new Target('my.cache')]),
	'blobs' => new TaggedLocator('blob'),
)]
ContainerInterface $container,

Any idea?

@kbond
Copy link
Member Author

kbond commented Aug 17, 2023

I thought that too! I was going to take a look at this possibility after this was merged.

BTW, your example can't be an array, it needs to be:

#[AutowireLocator(
    cache: new SubscribedService(type: CacheInterface::class: attributes: [new Target('my.cache')]),
    blobs: new TaggedLocator('blob'),
)]
ContainerInterface $container,

(just making sure you're ok with this syntax)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 21, 2023

I just noticed the failures on high+low deps, can you please have a look?

@kbond
Copy link
Member Author

kbond commented Aug 21, 2023

I believe the test suite will be fixed once merged because of the http-kernel's requirement on this AutowireLocator. Or should I add a conditional in the test?

@kbond kbond force-pushed the locator-attribute branch from 7587b15 to 3028d76 Compare August 21, 2023 12:35
@nicolas-grekas
Copy link
Member

Thank you @kbond.

@nicolas-grekas nicolas-grekas merged commit c64214c into symfony:6.4 Aug 21, 2023
@kbond kbond deleted the locator-attribute branch August 21, 2023 12:40
nicolas-grekas added a commit that referenced this pull request Oct 6, 2023
…ute and improve `#[AutowireLocator]` (nicolas-grekas, kbond)

This PR was merged into the 6.4 branch.

Discussion
----------

[DependencyInjection] Add `#[AutowireIterator]` attribute and improve `#[AutowireLocator]`

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT

This PR is building on #51392 to add an `#[AutowireIterator]` attribute and improve `#[AutowireLocator]`.

The new `#[AutowireIterator]` attribute can be used to describe what `#[AutowireLocator]` can do, except that we get an iterator instead of a container.

And  `#[AutowireLocator]` can now be used instead of `#[TaggedLocator]`: `#[AutowireLocator('foo')]` and done.

In order to describe that you want a list of services, we cannot use named arguments anymore so we have to pass an array now: `#[AutowireLocator(['foo' => 'F', 'bar' => 'B'])]` should be used instead of `#[AutowireLocator(foo: 'F', bar: 'B')]`.

Last but not least, this adds support for nesting `SubscribedService` objects in the list of described services. This provides feature-parity with what we can do when implementing `ServiceSubscriberInterface`.

I didn't deprecate `TaggedIterator` nor `TaggedLocator`. We could, but maybe we should wait for 7.1?

TODO:
- [x] add tests - thanks `@kbond`!

~PS: while writing this, I realize that we may merge both tags in one, and let `AutowirePass` decide if it should build a locator or an iterator based on the type of the argument that has the attribute. We'd "just" need to find a name that'd work for that.~

Commits
-------

a87f2e0 [DependencyInjection] Add tests for `AutowireLocator`/`AutowireIterator`
78018de [DependencyInjection] Add `#[AutowireIterator]` attribute and improve `#[AutowireLocator]`
@fabpot fabpot mentioned this pull request Oct 21, 2023
@fabpot fabpot mentioned this pull request Oct 21, 2023
fabpot added a commit that referenced this pull request Jun 20, 2025
…#[TaggedLocator]` attributes (GromNaN)

This PR was merged into the 8.0 branch.

Discussion
----------

[DependencyInjection] Remove `#[TaggedIterator]` and `#[TaggedLocator]` attributes

| Q             | A
| ------------- | ---
| Branch?       | 8.0
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | -
| License       | MIT

The new attributes `#[AutowireLocator]` and `#[AutowireIterator]` were introduced in Symfony 6.4 by #51392 and #51832.
These replace the previous attributes `#[TaggedIterator]` and `#[TaggedLocator]` which were introduced in Symfony 5.4 by #40406 and subsequently deprecated in 7.1 by #54371

Commits
-------

c096714 Remove TaggedIterator and TaggedLocator attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants