robinvdvleuten / lazy-event-dispatcher
An event dispatcher that holds any events until flushed.
Installs: 4 504
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: ^7.0
- symfony/event-dispatcher: ^2.8 || ^3.0 || ^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2022-02-04 09:35:07 UTC
README
An event dispatcher that holds any events until flushed.
Use Case
If you want to make use of the kernel.terminate event to do some "heavy" action after the response has already streamed back to the client. Symfony does this already by default but with this listener you'll have support for any custom event classes.
Installation
The recommended way to install the library is through Composer.
composer require robinvdvleuten/lazy-event-dispatcher
Install the listener as a service afterwards;
services: app.lazy_event_dispatcher: class: Rvdv\LazyEventDispatcher\LazyEventDispatcher arguments: - "@event_dispatcher" tags: - { name: kernel.event_listener, event: kernel.terminate, method: flush }
Then add a custom compiler pass to have a new event listener type;
<?php namespace AppBundle; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; use Symfony\Component\HttpKernel\Bundle\Bundle; class AppBundle extends Bundle { /** * {@inheritdoc} */ public function build(ContainerBuilder $container) { $container->addCompilerPass( new RegisterListenersPass('app.lazy_event_dispatcher', 'lazy.event_listener', 'lazy.event_subscriber'), PassConfig::TYPE_BEFORE_REMOVING ); } }
You'll then can register any "lazy" event listeners like this;
services: app.custom_event_listener: class: AppBundle\EventListener\CustomEventListener tags: - { name: lazy.event_listener, event: custom_event }
License
MIT © Robin van der Vleuten