lium/event-dispatcher

Standard implementation of PSR-14 for event handling.

v0.1.0 2019-12-08 10:34 UTC

This package is auto-updated.

Last update: 2024-04-22 23:12:56 UTC


README

A strict PSR-14 implementation built with simplicity in mind.

Why?

The main goal behind this library is to be a strict implementation of the PSR-14.

Installation

composer require lium/event-dispatcher

Usage

Here is a quick example to show you how to use it :

<?php

use App\Event\UserLoggedIn;
use Lium\EventDispatcher\EventDispatcher;
use Lium\EventDispatcher\ListenerProvider\DefaultListenerProvider;

// Listeners definitions
$listenerCalledForEveryEvent = function (object $event) {
    // This listener will match all events because its argument has the scalar type "object"
    echo sprintf('A listener has been called with the event "%s".', get_class($event));
};

$updateUserLastLoginDate = function (UserHasLoggedIn $event) {
    $user = $event->getUser();
    echo sprintf('The user "%s" has logged in.', $user->getUsername());
};

// Initialization
$listenerProvider = new DefaultListenerProvider([
    $listenerCalledForEveryEvent,
    $updateUserLastLoginDate,
]);

$eventDispatcher = new EventDispatcher($listenerProvider);

// Later in your code...
$eventDispatcher->dispatch(new UserHasLoggedIn($user));

The previous example will output :

A listener has been called with the event "App\Event\UserLoggedIn".
The user "johndoe" has logged in.

You can check the examples directory to see more examples.

Contributing

See the CONTRIBUTING file.

License

This project is available with the MIT license. For the full copyright, thanks to read the license file.