Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] [DI] Compiler pass to remove optional/invalid services #36701

Closed
@alcaeus

Description

@alcaeus

Description
The idea for this feature came when looking at a PR in DoctrineBundle. For optional integrations (e.g. integrating Doctrine ORM with the Messenger component), we provide a bunch of service definitions that are then removed in the extension if the necessary class doesn't exist (see example). To avoid having this kind of boilerplate code, a compiler pass could act upon the information in a tag:

<service id="potentially_missing" class="Potentially\Missing\Class">
    <tag name="symfony.remove_service" if="!class_exists('Potentially\Missing\Class')" />
</service>

Ideally, the if part would be an expression, allowing users to include any logic there. For example, if another service has a dependency on the (now non-existent) service, it would also have to be removed:

<service id="has_dependency" class="existing_class">
    <argument type="service" id="potentially_missing" />
    <tag name="symfony.remove_service" if="!container.has('Potentially\Missing\Class')" />
</service>

This would require the compiler pass to do multiple passes over the container until it removed all services. Since this only happens during compilation and is an opt-in feature, the performance impact should hopefully not be too severe.

I'd like your opinion and ideas as to how this could be used (or abused).

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependencyInjectionFeatureRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions