Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

symfony

Now I can tag services eg: form.type witch informs forms component to use this service as form type.

I can't find any documentation on this, how can I define my own tag? And get all services that are tagged with it? Or even pass all tagged with my tag services as a argument to another service?

like image 483
canni Avatar asked Nov 04 '11 17:11

canni


1 Answers

OK I found answer, basically tags are processed by classes that implements CompilerPassInterface compiler pass objects can be added in bunlde file (eg: Symfony\Bundle\FrameworkBundle\FrameworkBundle see method build)

CompilerPass have method process witch receives ContainerBuilder as argument.
ContainerBuilder has method: findTaggedServiceIds, witch can be used to get tagged services IDs, and do whatever you want with them.

To pass results to another service you have to define empty collection argument for it, and using ContainerBuilder replace this argument with found service IDs.

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

like image 73
canni Avatar answered Sep 29 '22 09:09

canni