Skip to content
/ Event Public

The EventEmitter is a simple pattern that allows you to create an object that emits events, and allow you to listen to those events.

License

Notifications You must be signed in to change notification settings

JBZoo/Event

Repository files navigation

JBZoo / Event

CI Coverage Status Psalm Coverage Psalm Level CodeFactor
Stable Version Total Downloads Dependents GitHub License

The EventEmitter is a simple pattern that allows you to create an object that emits events, and allow you to listen to those events.

Install

composer require jbzoo/event

Simple example

use JBZoo\Event\EventManager;

$eManager = new EventManager();

// Simple
$eManager->on('create', function () {
    echo "Something action";
});

// Just do it!
$eManager->trigger('create');

Set priority

By supplying a priority, you are ensured that subscribers handle in a specific order. The default priority is EventManager::MID. Anything below that will be triggered earlier, anything higher later. If there are two subscribers with the same priority, they will execute in an undefined, but deterministic order.

// Run it first
$eManager->on('create', function () {
    echo "Something high priority action";
}, EventManager::HIGH);

// Run it latest
$eManager->on('create', function () {
    echo "Something another action";
}, EventManager::LOW);

// Custom index
$eManager->on(