New in Symfony 5.3: Service Autowiring with Attributes

Autowiring Iterators/Locators with Attributes¶

        Contributed by 
        Alexander M. Turek
        and Nicolas Grekas
        in #40406.

The traditional way of working with service tags in Symfony applications involves these steps: Apply some tag to one or more services (either manually or applying a tag automatically to all services that implement some interface); Add some service configuration to tell Symfony to inject all services tagged with that tag into another service; Prepare that other service to receive tagged services as a PHP iterator, This process can quickly become repetitive and boring. That’s why in Symfony 5.3 we’re improving this feature to inject tagged services with PHP attributes. Imagine that your application needs to inject all services tagged with a custom tag called app.handler. First, apply this tag automatically to all services whose classes implement a certain PHP interface: 1 2 3 4 5 6# config/services.yaml services: _instanceof: App\Handler\HandlerInterface: tags: ['app.handler']

...

Now, use the new #[TaggedIterator] PHP attribute to inject all the services tagged with that tag. You don’t need to add any extra configuration; just adding this new attribute is enough: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15// src/Handler/HandlerCollection.php namespace App\Handler; use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; class HandlerCollection { private $handlers; public function __construct(

[TaggedIterator('app.handler')] iterator $handlers

) {
    $this->handlers = $handlers;
}

} Similarly, Symfony 5.3 includes a new #[TaggedLocator] PHP attribute to inject a service locator with all services tagged with some tag: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16// src/Handler/HandlerCollection.php namespace App\Handler; use Psr\Container\ContainerInterface; use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; class HandlerCollection { private $handlers; public function __construct(

[TaggedLocator('app.handler')] ContainerInterface $handlers

) {
    $this->handlers = $handlers;
}

} Selecting Autowire Alias with Attributes¶

        Contributed by 
        Nicolas Grekas 
        in #40800.

Autowiring aliases are needed when your application uses autowiring and there are multiple implementations of the same type. For example, consider the following scoped HTTP client created to work with GitHub API: 1 2 3 4 5 6 7 8 9# config/packages/framework.yaml framework: http_client: scoped_clients: githubApi: scope: 'https://api\.github\.com' headers: Accept: 'application/vnd.github.v3+json'

...

If you want to inject this scoped HTTP client in a service, it’s not enough to type-hint the constructor argument with HttpClientInterface. You must use the interface as the type-hint and the autowiring alias (githubApi) as the variable name: 1 2 3 4 5 6 7 8 9 10 11 12 13use Symfony\Contracts\HttpClient\HttpClientInterface; class GitHubDownloader { private $githubApi; public function construct(HttpClientInterface $githubApi) { $this->githubApi = $githubApi; } // ... } This mechanism works well, but having to use some specific variable names is too rigid for some developers. In Symfony 5.3 you can use any variable name because we’ve introduced a #[Target] attribute to select the autowiring alias. This is how the same example looks in Symfony 5.3: 1 2 3 4 5 6 7 8 9 10 11 12 13 14use Symfony\Component\DependencyInjection\Attribute\Target; use Symfony\Contracts\HttpClient\HttpClientInterface; class GitHubDownloader { private $httpClient; public function construct(#[Target('githubApi')] HttpClientInterface $httpClient) { $this->httpClient = $httpClient; } // ... }

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/nGM5c82fuGc/new-in-symfony-5-3-service-autowiring-with-attributes

Utworzony 4y | 18 maj 2021, 07:20:09


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

22 kwi 2025, 09:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

20 kwi 2025, 08:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

16 kwi 2025, 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

15 kwi 2025, 14:50:24 | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

13 kwi 2025, 09:40:02 | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

10 kwi 2025, 16:40:34 | Symfony
SymfonyCon Amsterdam 2025: Last days to enjoy early bird tickets!

The international Symfony conference of the year, SymfonyCon Amsterdam 2025, will take place in the Netherlands on November 27 & 28, 2025!

⏳ Early bird registration ends this Wednesday! D

8 kwi 2025, 09:10:13 | Symfony