enl/deferred-dispatcher

Extension for Symfony EventDispatcher which adds the way to defer handle of fired Event into later stage of request execution.

v1.0.2 2016-11-04 06:27 UTC

This package is auto-updated.

Last update: 2024-11-29 05:09:30 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']);