Autowiring Iterators/Locators with Attributes
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')] iterable $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
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 13
use 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 14
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class GitHubDownloader
{
private $httpClient;
public function __construct(#[Target('githubApi')] HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
// ...
}
I didn't know I absolutely needed this until now <3
Nice changes, love it
Another nice feature. But a little typo in the first class... the type must be
iterable
instead ofiterator
.@Micha thanks for reporting this error. It's fixed now.
I am happy to see annotations getting a lot more love in 5.3!
Cool! Checked it, and lacks working with promoted properties, would be awesome!
I.e.: