gianarb/fast-event-manager

This package is abandoned and no longer maintained. No replacement package was suggested.

Fast event manager, simples and regex based

Fund package maintenance!
gianarb

0.1.0 2015-11-09 14:39 UTC

This package is not auto-updated.

Last update: 2020-08-21 20:13:05 UTC


README

Scrutinizer Code Quality Code Coverage Build Status

PHP event manager based on regex. Trigger events and attach listeners, core feature easy to understand and to extend.

Install

$ composer install gianarb/fast-event-manager

Usage

Getting Started

This is the basic usage

<?php
use FastEventManager\EventManager;

$eventManager = new EventManager();

$eventManager->attach("post-save", function ($assertArg) {
    // DO STUFF
});

$assert = false;
$eventManager->trigger("/post-save/", $assert);

Priority

FastEventManager support priority listeners

$eventManager = new EventManager();

$eventManager->attach("post-save", function ($assertArg) {
    echo "Hi";
}, 100);

$eventManager->attach("post-save", function ($assertArg) {
    echo " dev!";
}, 10);

$eventManager->trigger("/post-save/");

// output "Hi dev!"

Regex

FastEventManager resolve regex, you can trigger more events.

$eventManager = new EventManager();

$eventManager->attach("post-save", function ($assertArg) {
    echo "Hi";
});

$eventManager->attach("pload", function ($assertArg) {
    echo " none!";
});

$eventManager->attach("post-load", function ($assertArg) {
    echo " dev!";
});

$eventManager->trigger("/post-(save|load)/i", $assert);

// output "Hi dev!"

Stop Propagation

At the moment we decided to don't support this feature into the core of FastEventManager because there are a lot of implementation around this feature. This is an example

$eventManager = new EventManager();
$count = 0;
$eventManager->attach("post", function () use (&$count) {
    $count++;
}, 100);
$eventManager->attach("post", function () use (&$count) {
    throw new \Exception();
}, 110);
$eventManager->attach("post", function () use (&$count) {
    $count++;
}, 120);
try {
    $eventManager->trigger("/post/");
} catch (\Exception $exc) {
    // STOP!
}