$dispatcher = new Qubus\EventDispatcher\Dispatcher();
Add a listener for the specified event
There are two types of listeners: callable and EventListener
Qubus\EventDispatcher\EventListener
1 2 3 4 5 6 7 8 9101112
use Qubus\EventDispatcher\Event;
use Qubus\EventDispatcher\EventListener;
class FooListener implements EventListener
{
public function handle(Event $event)
{
//do something
}
}
$dispatcher->addListener('kernel.event', new FooListener());
use Qubus\EventDispatcher\Event;
use Qubus\EventDispatcher\EventSubscriber;
class FooSubscriber implements EventSubscriber
{
public static function getSubscribedEvents()
{
return [
'kernel.event' => 'onFoo',
'bar.event' => 'onBar'
];
}
public function onFoo(Event $event)
{
//do something
}
public function onBar()
{
//do something
}
}
$dispatcher->addSubscriber(new FooSubscriber());
Or dispatch with the event name using a constant EVENT_NAME which is located in the Event interface:
1
$dispatcher->dispatch(GenericEvent::EVENT_NAME);
Propagation
You can call stopPropagation to stop event propagation on the event instance.
123456789
$dispatcher->addListener('kernel.event', function(Event $event){
$event->stopPropagation();
});
$emitter->addListener('kernel.event', function ($event) {
// This will not be triggered
});
$dispatcher->dispatch('kernel.event');
Checks whether propagation is stopped
1234
$event = new GenericEvent('kernel.event');
$dispatcher->dispatch($event);
$event->isPropagationStopped();
Hooks
This libary also includes a simple action and filter hook system.
Usage Action
1234567
use Qubus\EventDispatcher\ActionFilter\Observer;
(new Observer)->action->addAction("header", function() {
echo "Hello!";
});
(new Observer)->action->doAction("header");
Usage Filter
1234567
use Qubus\EventDispatcher\ActionFilter\Observer;
(new Observer)->filter->addFilter("header", function($value) {
return $value . " World!";
});
echo (new Observer)->filter->applyFilter("header", "Hello");