简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM