$dispatcher = new Qubus\EventDispatcher\Dispatcher();
There are two types of listeners: callable
and EventListener
Qubus\EventDispatcher\EventListener
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());
callable
$dispatcher->addListener('kernel.event', function(Event $event){
//do something
});
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());
Just provides the event name.
$dispatcher->dispatch('kernel.event');
You can also dispatch with an event instance.
$dispatcher->dispatch(new GenericEvent('kernel.event'));
Or dispatch with the event name using a constant EVENT_NAME
which is located in the Event
interface:
$dispatcher->dispatch(GenericEvent::EVENT_NAME);
You can call stopPropagation
to stop event propagation on the event instance.
$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
$event = new GenericEvent('kernel.event');
$dispatcher->dispatch($event);
$event->isPropagationStopped();
This libary also includes a simple action and filter hook system.
use Qubus\EventDispatcher\ActionFilter\Observer;
(new Observer)->action->addAction("header", function() {
echo "Hello!";
});
(new Observer)->action->doAction("header");
use Qubus\EventDispatcher\ActionFilter\Observer;
(new Observer)->filter->addFilter("header", function($value) {
return $value . " World!";
});
echo (new Observer)->filter->applyFilter("header", "Hello");