Skip to content

Latest commit

 

History

History
136 lines (98 loc) · 2.55 KB

EventDispatcher.md

File metadata and controls

136 lines (98 loc) · 2.55 KB

Usage

Create an event dispatcher

$dispatcher = new Qubus\EventDispatcher\Dispatcher();

Add a listener for the specified event

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
});

Add a subscriber

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.

$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);

Propagation

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();

Hooks

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

Usage Action

use Qubus\EventDispatcher\ActionFilter\Observer;

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

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

Usage Filter

use Qubus\EventDispatcher\ActionFilter\Observer;

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

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