symfony
symfony copied to clipboard
[DependencyInjection] [WIP] Allow service subscribers to return `SubscribedService[]`
| Q | A |
|---|---|
| Branch? | 6.2 |
| Bug fix? | no |
| New feature? | yes |
| Deprecations? | no |
| Tickets | n/a |
| License | MIT |
| Doc PR | todo |
This allows ServiceSubscriberInterface::getSubscribedServices() to return SubscribedService[] which now has the ability to add DI attribute objects (ie Target).
public static function getSubscribedServices(): array
{
return [
new SubscribedService('logger', Psr\Log\LoggerInterface::class),
new SubscribedService(type: Psr\Log\LoggerInterface::class, nullable: true),
new SubscribedService('http_client', HttpClientInterface::class, attributes: new Target('githubApi'))),
];
}
When using the ServiceSubscriberTrait, the SubscribedService attribute can add attributes as well:
#[SubscribedService(attributes: new Target('githubApi'))]
private function httpClient(): ?HttpClientInterface
{
return $this->container->get(__METHOD__);
}
TODO:
- [ ] Implementation
- [ ] Tests
- [ ] Changelog
I like it :) Let's see how DI can leverage that for Autowiring now.
Thank you @kbond.