简体   繁体   English

如何添加自己的自定义服务容器标签,以及如何在symfony2中获取标记服务?

[英]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. 现在我可以标记服务,例如: form.type witch通知表单组件将此服务用作表单类型。

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 ) 好的我找到了答案,基本上标签是由实现CompilerPassInterface的类处理的, CompilerPassInterface器传递对象可以在bunlde文件中添加(例如: Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle参见方法build

CompilerPass have method process witch receives ContainerBuilder as argument. CompilerPass有方法process接收ContainerBuilder作为参数。
ContainerBuilder has method: findTaggedServiceIds , witch can be used to get tagged services IDs, and do whatever you want with them. ContainerBuilder有方法: findTaggedServiceIds ,可以用来获取标记的服务ID,并用它们做任何你想做的事情。

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. 要将结果传递给另一个服务,您必须为它定义空集合参数,并使用ContainerBuilder将此参数替换为找到的服务ID。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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