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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号