eventee/eventee

Eventee is a minimalistic and powerfull event dispatching library for PHP

1.0.0 2016-04-26 19:05 UTC

This package is auto-updated.

Last update: 2024-04-27 05:40:47 UTC


README

Eventee is a minimalistic and powerfull event dispatching library for PHP.

TOC

Installation

Make sure you have composer installed (More information here). And your php version is >= PHP 5.5.

composer require eventee/eventee

Listening to an event

$hub = new \Eventee\EventHub();
$hub->addListener(\Eventee\Event::class, function() {
    echo 'Hello world!';
});

Dispatching an event

$hub = new \Eventee\EventHub();
$hub->addListener(\Eventee\Event::class, function() {
    echo 'Hello world!';
});
// Will echo 'Hello World'.
$hub->dispatch(new \Eventee\Event());

Creating custom event

class OnUserCreated extends Event
{
    private $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function getUser()
    {
        return $this->user;
    }
}

$hub = new \Eventee\EventHub();
$hub->addListener(OnUserCreated::class, function(OnUserCreated $e) {
    echo sprintf('Hello world, %s!', $e->getUser());
});
// Will echo 'Hello World, John!'.
$hub->dispatch(new OnUserCreated('John'));

Stopping event from propagation

$hub = new \Eventee\EventHub();
$hub->addListener(\Eventee\Event::class, function(Event $e) {
    $e->stop();
    echo 'Hello ';
});
$hub->addListener(\Eventee\Event::class, function(Event $e) {
    echo 'World';
});
// Will echo 'Hello '.
$hub->dispatch(new \Eventee\Event());

Checking if listener exists

$hub = new \Eventee\EventHub();
$listener = function(Event $e) {
    echo 'Hello ';
}
$hub->addListener(\Eventee\Event::class, $listener);

// Will echo 'Hello world!'
if ($hub->hasListener(\Eventee\Event::class, $listener) {
    echo 'Hello World!';
}

Removing listener

$hub = new \Eventee\EventHub();
$listener = function(Event $e) {
    echo 'Hello ';
}
$hub->addListener(\Eventee\Event::class, $listener);

$hub->removeListener(\Eventee\Event::class, $listener);

// No output this time.
if ($hub->hasListener(\Eventee\Event::class, $listener) {
    echo 'Hello World!';
}