enl / deferred-dispatcher
Extension for Symfony EventDispatcher which adds the way to defer handle of fired Event into later stage of request execution.
Requires
- php: ~5.6 || ~7.0
- symfony/event-dispatcher: ~2.7 || ~3.0
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is auto-updated.
Last update: 2024-10-29 05:04:44 UTC
README
This package extends Symfony Event Dispatcher and adds an opportunity to defer a group of events to handle them later during request execution.
Bootstrap code:
$eventsToDefer = ['event-to-defer', 'another-event']; $dispatcher = new DeferredEventDispatcher(); $dispatcher->addSubscriber(new DeferredSubscriber($eventsToDefer));
When you dispatch event from the list, it will be intercepted by DeferredSubscriber
and stored to execute later. So that, after you did all the essential things you need to fire special event to replay events:
$dispatcher->dispatch(Events::PLAY_DEFERRED);
Subscribe DeferredSubscriber
to another event
The idea of playing deferred events by firing Events::PLAY_DEFERRED
may be not that good, because it sticks your codebase (not configuration) to this dispatcher.
So that, there is another opportunity:
$subscriber = new DeferredSubscriber($eventsList); $dispatcher->addSubscriber($subscriber); $dispatcher->addListener('kernel.terminate', [$subscriber, 'playDeferred']);