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
You can install Berlioz Event Manager with Composer, it's the recommended installation.
$ composer require berlioz/event-manager
- PHP ^8.0
- Packages:
- psr/event-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());
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.
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());
You can add listener providers. Providers are called in the order of addition.
use Berlioz\EventManager\EventDispatcher;
$dispatcher = new EventDispatcher();
$dispatcher->addListenerProvider(new MyListenerProvider());
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);