Description
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).