简体   繁体   English

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

[英]symfony2 event dispatcher lazy loading listener

I'm creating some of my own events using Symfony Event Dispatcher, which works fine. 我正在使用Symfony Event Dispatcher创建一些自己的事件,效果很好。

I noticed however, that the listener I configured in symfony is not lazy loaded, it is always initialized. 但是我注意到,我在symfony中配置的监听器不是延迟加载的,它总是被初始化。 It is rarely used however. 但是很少使用。

The config in my services.yml looks like: 我的services.yml中的配置如下所示:

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? 有没有一种配置方式使得@ my.handler仅在事件触发时被初始化? 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. 有关于ContainerAwareEventDispatcher的文档: http ://symfony.com/doc/current/components/event_dispatcher/container_aware_dispatcher.html但这仅说明了如何直接在PHP中使用它,而不是如何在标准symfony2项目中对其进行配置。

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. 请记住按照文档中的说明安装ProxyManager桥

Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM