【发布时间】:2014-02-06 03:31:55
【问题描述】:
如果有的话,Symfony 2 捆绑包开发人员应该如何使用现有 Symfony 2 系统附带的事件调度器?
我一直在挖掘 Symfony 事件调度器的源代码,我所看到的一些内容让我对我作为第三方包创建者应该如何使用 Symfony 附带的事件调度器感到有些困惑.
具体来说,我注意到一个普通的 Symfony 系统有两个事件调度服务。 event_dispatcher 和 debug.event_dispatcher。 HttpKernel 使用哪种服务取决于环境,并由生成的 dev 或 prod 容器文件驱动。
//dev kernel instantiation uses `debug.event_dispatcher` service
new \Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel(
$this->get('debug.event_dispatcher'),
$this,
$this->get('debug.controller_resolver')
);
//prod kernel instantiation uses `event_dispatcher` service
new \Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel(
$this->get('event_dispatcher'),
$this,
new \Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver($this, $this->get('controller_name_converter'), $this->get('monolog.logger.request', ContainerInterface::NULL_ON_INVALID_REFERENCE)));
到目前为止,这一切都说得通——因为它是 debug.event_dispatcher 在网络配置文件的事件选项卡中实现的功能,包括查看调用了哪些侦听器以及未调用哪些侦听器的能力。
但是,我注意到大多数(如果不是全部)第三方捆绑包都使用硬编码的 event_dispatcher 服务调用。例如,JMS/JobQueueBundle 使用以下
$this->dispatcher = $this->getContainer()->get('event_dispatcher');
像这样正确触发的事件,但是debug.event_dispatcher 不知道它们,这意味着 Symfony 网络分析器会错误地将调用的侦听器列为未调用的。此外,尚不清楚捆绑包作者如何避免这种情况,因为他们没有生成容器文件的优势并且 HTTP Kernel 对象不会公开受保护的调度程序对象的访问器。
那么,这是 Symfony 中的一个错误吗?
或者event_dispatcher 服务仅用于内核事件,这意味着所有捆绑包作者都在滥用它?
或者(最有可能的候选人),这是我遗漏或没有考虑的其他东西吗?
【问题讨论】:
-
如果您正在创建自己的包,您可以使用自己的 even 调度程序。使用内置 event_dispatcher 服务的唯一原因是使用您并不总是需要的内核事件。但是你的问题仍然有效。