symfony icon indicating copy to clipboard operation
symfony copied to clipboard

[DependencyInjection] [WIP] Allow service subscribers to return `SubscribedService[]`

Open kbond opened this issue 3 years ago • 1 comments

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

kbond avatar Jul 28 '22 16:07 kbond

I like it :) Let's see how DI can leverage that for Autowiring now.

nicolas-grekas avatar Jul 29 '22 10:07 nicolas-grekas

Thank you @kbond.

fabpot avatar Aug 19 '22 13:08 fabpot