Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Make it possible to omit keys for service locaters #26892

Copy link
Copy link
Closed
@codedmonkey

Description

@codedmonkey
Issue body actions
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.