symfony2事件调度程序延迟加载监听器

问题描述:

I'm creating some of my own events using Symfony Event Dispatcher, which works fine.

I noticed however, that the listener I configured in symfony is not lazy loaded, it is always initialized. It is rarely used however.

The config in my services.yml looks like:

my.handler:
    class: Acme\AcmeBundle\DependencyInjection\MyHandler
    arguments:
      - @my.dependency
    tags:
      - { name: kernel.event_listener, event: my.event, method: handle }

Is there a way to configure this in such a way that @my.handler is only initialised when the event is fired? Because now it is initialised (along with all its dependencies) when it is pushed in the Dispatcher.

There is documentation about a ContainerAwareEventDispatcher: http://symfony.com/doc/current/components/event_dispatcher/container_aware_dispatcher.html But this only explains how to use it directly in PHP, not how to configure it in a standard symfony2 project.

我正在使用Symfony Event Dispatcher创建一些自己的事件,它可以正常工作。 p>

但是我注意到,我在symfony中配置的监听器不是延迟加载的,它总是被初始化。 但很少使用它。 p>

我的服务中的配置。 yml看起来像: p>

  my.handler:
 class:Acme \ AcmeBundle \ DependencyInjection \ MyHandler 
 arguments:
  -  @ my.dependency 
 tags:
  -  {name:kernel.event_listener,event:my.event,method:handle} 
  code>  pre> 
 
 

有没有办法以@my的方式配置它。 只有在事件被触发时才初始化handler? 因为现在它在Dispatcher中被推送时被初始化(及其所有依赖项)。 p>

有关于ContainerAwareEventDispatcher的文档:\ n http://symfony.com/doc/current/com ponents / event_dispatcher / container_aware_dispatcher.html 但这仅解释了如何在PHP中直接使用它,而不是如何在标准symfony2项目中配置它。 p> div>

You can define it as a Lazy Services adding the relative tag (as described here ) as example:

my.handler:
    class: Acme\AcmeBundle\DependencyInjection\MyHandler
    lazy: true
    arguments:
      - @my.dependency
    tags:
      - { name: kernel.event_listener, event: my.event, method: handle }

Remember to install the ProxyManager bridge as described in the doc.

Hope this help