Skip to content

Event Dispatcher

Usage

Create an event dispatcher

1
$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
     9
    10
    11
    12
    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

    1
    2
    3
    $dispatcher->addListener('kernel.event', function(Event $event) {
        //do something
    });
    

Add a subscriber

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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());

Dispatches the event to the registered listeners

Just provides the event name.

1
$dispatcher->dispatch('kernel.event');

You can also dispatch with an event instance.

1
$dispatcher->dispatch(new GenericEvent('kernel.event'));

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.

1
2
3
4
5
6
7
8
9
$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

1
2
3
4
$event = new GenericEvent('kernel.event');
$dispatcher->dispatch($event);

$event->isPropagationStopped();

Hooks

This libary also includes a simple action and filter hook system.

Usage Action

1
2
3
4
5
6
7
use Qubus\EventDispatcher\ActionFilter\Observer;

(new Observer)->action->addAction("header", function() {
    echo "Hello!";
});

(new Observer)->action->doAction("header");

Usage Filter

1
2
3
4
5
6
7
use Qubus\EventDispatcher\ActionFilter\Observer;

(new Observer)->filter->addFilter("header", function($value) {
    return $value . " World!";
});

echo (new Observer)->filter->applyFilter("header", "Hello");