Skip to content

!service_locator does not use ID of the original service when key not specified #48454

Closed
@Bilge

Description

@Bilge

Symfony version(s) affected

5.4.16

Description

Everything written in Defining a Service Locator is true and accurate, but what is problematic is what's not written. The astute reader will notice we are given two examples for defining a service locator manually using Symfony\Component\DependencyInjection\ServiceLocator: one with service keys and one without, but there is only one example given using the !service_locator shortcut.

The comment for the example not using keys states: if the element has no key, the ID of the original service is used. This is all good, true and correct. So what happens when we do the same using the !service_locator shortcut? Surely the same behaviour, right? Wrong! When we specify a service locator using !service_locator and no keys, for which there is no example present in the documentation, the services are instead keyed using a contiguous integer sequence starting at zero, which suffice it to say, is completely useless.

How to reproduce

# config/services.yaml
services:
    App\CommandBus:
        arguments:
          - !service_locator
              - '@app.command_handler.foo'
              - '@app.command_handler.bar'

Possible Solution

Use the service ID as the key name, just as with the manual service locator definition pattern.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions