使用Symfony 2事件调度程序

Ala*_*orm 13 php events symfony

如果有的话,Symfony 2捆绑开发人员应该如何使用库存Symfony 2系统附带的事件调度程序?

我一直在寻找Symfony事件调度员的来源,而我所看到的一些内容让我有点困惑,我是第三方捆绑创建者应该如何使用Symfony附带的事件调度程序.

具体来说,我注意到一个股票Symfony系统有两个事件调度程序服务.的event_dispatcherdebug.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)));
Run Code Online (Sandbox Code Playgroud)

到目前为止,这一切都有意义 - 因为它是debug.event_dispatcher在Web配置文件的事件选项卡中实现功能,包括能够查看调用哪些侦听器以及未调用哪些侦听器.

但是,我注意到大多数(如果不是全部)第三方捆绑包使用硬编码event_dispatcher服务调用.例如,JMS/JobQueueBundle使用以下内容

$this->dispatcher = $this->getContainer()->get('event_dispatcher');
Run Code Online (Sandbox Code Playgroud)

活动派出这样火正确,debug.event_dispatcher不知道它们,这意味着Symfony的网络分析器会错误地列出一个叫听者没有道理.此外,目前尚不清楚捆绑作者如何避免这种情况,因为他们没有生成容器文件的优势,并且 HTTP内核对象不会公开受保护调度程序对象的访问者.

那么,这是Symfony中的一个错误吗?

或者该event_dispatcher服务仅用于内核事件,这意味着所有这些捆绑作者都在滥用它?

或者(最有可能的候选人),是否是我缺少或未考虑的其他内容?

Ala*_*orm 7

看起来我上面描述的场景不适用于最新版本的Symfony(2.4.1).具体来说,在2.4.1生成的应用容器文件中

app/cache/dev/appDevDebugProjectContainer.php
Run Code Online (Sandbox Code Playgroud)

包含以下内容

$this->aliases = array(
    //...
    'event_dispatcher' => 'debug.event_dispatcher',
    //...
);
Run Code Online (Sandbox Code Playgroud)

也就是说,与2.3.6我正在研究的Symfony 项目不同,该event_dispatcher服务已被别名debug.event_dispatcher服务(当Symfony以开发模式运行时).这意味着当其他捆绑包event_dispatcher在开发模式下请求服务时,他们真正获得了debug.event_dispatcher服务.这样可以debug.event_dispatcher了解所有事件,并且可以正确地报告已分派的事件.

虽然这不是一个具体的答案,但它确实表明Symfony团队意识到了这个问题,这让我相信他们打算让Bundle开发人员为event_dispatch他们自己的活动使用这项服务.