Simple clear Event Dispatcher, compatible with Symfony Events

dev-master 2016-01-02 18:17 UTC

This package is not auto-updated.

Last update: 2020-04-03 17:35:50 UTC


Simple & clear PHP Event Dispatcher, compatible with Symfony Events

Can be installed with composer

composer require jakulov/event

Implements Symfony Event Interfaces

1. Dispatcher

There's three common ways to register listeners: setConfig, addListener and use Subscriber class. Config could look like this:

$listener1 = new \Listener\TestListener();
$listener2 = new \Listener\TestPrioritizedListener();
$config = [
    'event.test' => [  // the name of event
        [$listener1, 'onTest', 1], // listener, method and priority
        [$listener2, 'onTest'],
    'event.another_test' => [
        ['@service.listener', 'onAnotherTest'], // you can use reference to DIContainer service
$dispatcher = new \jakulov\Event\EventDispatcher();

Link to container library: jakulov/container

Using addListener is more usual way:

$listener = [new \Listener\TestListener(), 'onTest'];
$dispatcher = new \jakulov\Event\EventDispatcher();
$dispatcher->addListener($event->getName(), $listener);

Also dispatcher requires a Logger instance, implementing Psr Log If don't need in logging events, just do this:

$dispatcher = new \jakulov\Event\EventDispatcher();
$dispatcher->setLogger(new \Psr\Log\NullLogger());

2. Subscriber

Using subscriber class is usual way to setup multiple event listeners at once:

$dispatcher = new \jakulov\Event\EventDispatcher();
$dispatcher->addSubscriber(new \Listener\TestSubscriber());

3. Event

Class \jakulov\Event\AbstractEvent should be parent for all event objects used with dispatcher. You can dispatch events without event object, but objects is the right way to pass event data to listeners.


Run: ./run_tests.sh

Tests are also examples for usage library