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

Établi 4y | 18 mai 2021 à 07:20:09


Connectez-vous pour ajouter un commentaire

Autres messages de ce groupe

SymfonyLive Berlin 2025: Need a MACH-ready Search Engine?

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

As we are now unveiling th

24 janv. 2025 à 11:20:16 | Symfony
SymfonyLive Paris 2025 : Rôles & permissions : développez une marque blanche avec du Feature Flipping

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

23 janv. 2025 à 16:50:03 | Symfony
SymfonyLive Berlin 2025: So you think you know PHPUnit

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

First, a big thank you to

22 janv. 2025 à 08:20:10 | Symfony
SymfonyLive Paris 2025 : Passkeys pour une authentification fluide et sécurisée

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

To

21 janv. 2025 à 11:30:10 | Symfony
Join us for SymfonyDay Chicago – March 17, 2025!

Mark your calendars for March 17, 2025 because SymfonyDay Chicago 2025 promises to be a one-of-a-kind event that you won’t want to miss! This full day is dedicated to celebrating the incredible contri

20 janv. 2025 à 19:20:03 | Symfony
A Week of Symfony #942 (13-19 January 2025)

This week, Symfony celebrated the SymfonyOnline January 2025 conference. In addition, it announced the new Symfony UX Core Team. Lastly, the upcoming Symfony 7.3 version simplified the configuration o

19 janv. 2025 à 08:30:08 | Symfony
Announcing the Symfony UX Core Team

The Symfony UX initiative was announced in December 2020. It was introduced to enhance the developer experience by integrating JavaScript tools and libraries more seamlessly with Symfony applications,

13 janv. 2025 à 15:40:04 | Symfony