简体   繁体   中英

symfony2 event dispatcher lazy loading listener

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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