How to add custom user service container tags and how to get tagged services in symfony2?

Now I can mark services, for example: form.type witch informs the form component to use this service as the form type.

I can not find the documentation on this, how can I define my own tag? And get all the services that are marked with this? Or even pass all tagged by my tag services as an argument to another service?

+6
source share
1 answer

OK I found the answer, mostly tags are processed by classes that implement CompilerPassInterface compiler objects can be added to the bunlde file (for example: Symfony\Bundle\FrameworkBundle\FrameworkBundle see the build method)

CompilerPass has a process witch method that receives a ContainerBuilder as an argument.
The ContainerBuilder method has a method: findTaggedServiceIds , a witch can be used to get tag identifiers with identifiers and do whatever you need with them.

To pass the results to another service, you need to define an empty collection argument for it, and using ContainerBuilder replace this argument with the service identifiers found.

Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass : Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass

+10
source

Source: https://habr.com/ru/post/900795/


All Articles