Skip to content

Proposal - Automatic service aliases and bindings #44184

@drupol

Description

@drupol

Description

The S.O.L.I.D. principles are a set of five design principles intended to make
software designs more understandable, flexible, and maintainable.

One of these principles is the Open-Closed Principle, which promotes the use
of interfaces instead of concrete implementations.

In Symfony, when injecting services, we usually rely on concrete service implementations
rather than using an interface. This makes our code less flexible and sometimes harder to
test.

This behavior is native in Symfony when there is only one service which implements an interface.
What then when multiple services implements the same interface? It's no more possible,
unless by adding them manually in the container.
Sadly, you have to do it manually and for each service/class.

I propose to update this mechanism and automatically creates aliases based on the discovered
services. For example, it could be enabled by adding a new property in the container
(just like the autowire and autoconfigure properties).

I created a proof-of-concept bundle that implements this and it can be found here.

The bundle implements the feature using a specific tag, there is maybe a better method
to do that, I'm open to some kind of feedback.

Feel free to let me know if this is a good or a bad idea, let's discuss about it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions