berlioz/event-manager

PHP event manager/dispatcher, respecting PSR-14 (Event Dispatcher) standard.

v1.0.0 2021-09-08 20:46 UTC

This package is auto-updated.

Last update: 2024-10-09 03:47:27 UTC


README

Latest Version Software license Build Status Quality Grade Total Downloads

Berlioz Event Manager is a PHP event manager/dispatcher, respecting PSR-14 (Event Dispatcher) standard.

For more information, and use of Berlioz Framework, go to website and online documentation : https://getberlioz.com

Installation

Composer

You can install Berlioz Event Manager with Composer, it's the recommended installation.

$ composer require berlioz/event-manager

Dependencies

  • PHP ^8.0
  • Packages:
    • psr/event-dispatcher

Usage

Dispatcher

To initialize the event dispatcher:

use Berlioz\EventManager\EventDispatcher;

$dispatcher = new EventDispatcher();

To listen an event:

use Berlioz\EventManager\EventDispatcher;

$callback = function($event) {
    // Do something
    return $event;
};

/** @var EventDispatcher $dispatcher */

// A named event
$dispatcher->addEventListener('event.name', $callback);

// Your event object
$dispatcher->addEventListener(MyEvent::class, $callback);

To dispatch an event:

/** @var EventDispatcher $dispatcher */
use Berlioz\EventManager\Event\CustomEvent;
use Berlioz\EventManager\EventDispatcher;

// A named event
$dispatcher->dispatch(new CustomEvent('event.name'));

// Your event object
$dispatcher->dispatch(new MyEvent());

Priority

You can define a priority in your listeners. The highest priority is in the first executions.

use Berlioz\EventManager\Listener\ListenerInterface;

/** ... */

// Normal priority (0)
$dispatcher->addEventListener('event.name', $callback, ListenerInterface::PRIORITY_NORMAL);
// High priority (100)
$dispatcher->addEventListener('event.name', $callback, ListenerInterface::PRIORITY_HIGH);
// Low priority (-100)
$dispatcher->addEventListener('event.name', $callback, ListenerInterface::PRIORITY_LOW);

The priority argument is an integer ; you can so define your priority with integer value instead of constant.

Add delegate dispatcher

You can delegate dispatch to another dispatcher who respects PSR-14. The delegated dispatchers are called after, only if event isn't stopped.

use Berlioz\EventManager\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addEventDispatcher(new MyCustomDispatcher());

Add listener provider

You can add listener providers. Providers are called in the order of addition.

use Berlioz\EventManager\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListenerProvider(new MyListenerProvider());

Default listener

The default listener is \Berlioz\EventManager\Listener\Listener. You can define your own default provider, he must implement \Berlioz\EventManager\Listener\ListenerInterface interface.

To declare this into the dispatcher:

use Berlioz\EventManager\EventDispatcher;
use Berlioz\EventManager\Provider\ListenerProvider;$myDefaultProvider = new ListenerProvider();

$dispatcher = new EventDispatcher(defaultProvider: $myDefaultProvider);