提问人:Hevyweb 提问时间:11/12/2023 最后编辑:Hevyweb 更新时间:11/16/2023 访问量:41
如何在 Symfony 5.4+ 中通过其类名获取服务的标签
How to get tag of the service by its class name in Symfony 5.4+
问:
初始信息:Symfony 5.4+,PHP 8+。
例如,我有一个类 EventService。它是 EventInterface 类型的构造函数中的第一个参数。
<?php
class EventService
{
public function __construct(private EventInterface $event)
{
}
public function execute()
{
// obtain tags of the $event
}
}
有几个事件实现了此接口
<?php
class Event implements EventInterface
{
// no tags property in here
}
在service.yaml中,我声明
Event:
public: true
tags:
- { name: 'foo', priority: 20 }
- { name: 'bar', priority: -10 }
如何获取EventService中$event对象的“foo”和“bar”标签?
我尝试使用ContainerBuilder,但它给了我一个错误“找不到服务”。
$containerBuilder->findDefinitions($event::class)->getTags()
答: 暂无答案
评论