doq1969 2013-06-25 16:19
浏览 44
已采纳

收听Symfony 2中的所有活动

Is it possible to setup an event listener (or do — something else?) to listen for all the events fired by a Symfony 2 AppKernel application for a particular request?

That is, I know I can browse an application with app_dev.php and use the profiler to view a list of all the listeners, but I'm interested in grabbing a list of every event that's been dispatched/fired. I know some event systems have a special global/all listener what would let me receive every event. I'm wondering if Symfony has something similar, or if there's another mechanism to get a list of all the available events on a particular page.

I also know I could add some temporary debugging code to one of the event dispatcher classes

Symfony/Component/EventDispatcher/EventDispatcher.php
Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php
Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php

but I'm looking for something that is less of a hack/less-destructive.

New to Symfony, but not new to programming. Apologies if this is a naive question, but googling about hasn't revealed what I'm after.

  • 写回答

1条回答 默认 最新

  • dongyang7152 2013-06-25 16:38
    关注

    The clean way would be creating your own EventDispatcher which executes your logging or whatever you're trying to do if an event occurs. Have a look at the default one to get an idea of how it works.

    Now first create the class

    use Symfony\Component\EventDispatcher\EventDispatcher;
    
    class MyDispatcher extends EventDispatcher
    {
    
        // sadly those properties aren't protected in EventDispatcher
        private $listeners = array();
        private $sorted = array();
    
    
        public function dispatch($eventName, Event $event = null)
        {
            if (null === $event) {
                $event = new Event();
            }
    
            $event->setDispatcher($this);
            $event->setName($eventName);
    
            // do something with the event here ... i.e. log it
    
            if (!isset($this->listeners[$eventName])) {
                return $event;
            }
    
            $this->doDispatch($this->getListeners($eventName), $eventName, $event);
    
            return $event;
        }
    

    ... then register your MyDispatcher as symfony's default one.

    ( by overwriting the original event_dispatcher service )

    app/config/config.yml

    services:
        event_dispatcher:
            class: Vendor\YourBundle\MyDispatcher
            arguments: [@service_container]
    

    ... or even simpler just override the class parameter being used by symfony when creating the service.

    parameters:
        event_dispatcher.class: Vendor\YourBundle\MyDispatcher
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来