noccylabs/tinyevent

$ git tag
0.1.0
0.1.1
0.1.1.1
0.1.2
0.1.2.1
0.1.3

$ git branch
* master

TinyEvent

This is a tiny event dispatcher library. It comes with a trait that can be used in classes to add emitter capabilities, as well as a regular dispatcher that can be used normally.

EventDispatcher

The dispatcher is used separately and can be shared between other classes and instances.

use NoccyLabs\TinyEvent\EventDispatcher;

$dispatcher = new EventDispatcher();

$dispatcher->addListener("test", function ($event) {
    echo "Got event of type: ".$event->getType()."\n";
});

$dispatcher->dispatchEvent("test");

EventEmitter

Emitters are integrated into objects.

use NoccyLabs\TinyEvent\EventEmitterInterface;
use NoccyLabs\TinyEvent\EventEmitterTrait;

class MyClass implements EventEmitterInterface {

    use EventEmitterTrait;

    public function test() {
        $this->emitEvent("test");
    }
}

$obj = new MyClass();

$obj->addListener("test", function ($event) {
    echo "Got event of type: ".$event->getType()."\n";
});

$obj->test();

Invocation order

The first listener added will be the last to receive the event. This is so that the most recent listener can stop propagation in order to override behavior etc.

$dispatcher->addListener("test", function ($event) {
    echo "First subscriber, called last\n";
});
$dispatcher->addListener("test", function ($event) {
    echo "Second subscriber, called first\n";
});

User data

User data can be attached to the event object. In addition, data can be added to the addListener call to be included with the actual event call.

$dispatcher = new EventDispatcher();

$dispatcher->addListener("test", function ($event, $foo) {
    echo "Got event of type: ".$event->getType()."\n";
    echo "Foo is: ".$foo."\n";
    echo "Bazinga is: ".($event->bazinga?"true":"false")."\n";
}, "Hello World");

$evt = new Event();
$evt->bazinga = true;
$dispatcher->dispatchEvent("test", $evt);