Description
Q | A |
---|---|
Bug report? | no |
Feature request? | yes |
BC Break report? | no |
RFC? | yes/no |
Symfony version | 4.1.x |
I was building a service with a service locater and it occurred to me that you're forced to define both a key
and id
(service id) to add a service to the service locator.
Enter service subscribers, in which it's possible to omit the key if it's the same internally and externally. Example:
public static function getSubscribedServices()
{
return [
LoggerInterface::class,
];
}
This is enough* to add the logger
service to your service with a service subscriber because the key is that same as its service id and it links the two.
<service id="my_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
<argument type="collection">
<argument type="service" key="Psr\Logger\LoggerInterface" id="Psr\Logger\LoggerInterface" />
</argument>
</service>
This is necessary to add the logger
service as an argument to a service locator. With the PSR Logger this isn't a big deal, but with longer classes it's just a hassle.
I would propose to add a new way to add a service to a service locator by only passing a string:
<service id="my_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
<argument type="collection">
<argument>Psr\Logger\LoggerInterface</argument>
</argument>
</service>
This is similar to how it's handled in the service subscribers pass. I've made an attempt to create a simple PR to achieve the functionality, but was unable to do so due to memory errors when building the container and have yet to make another attempt.