FastEventManager
, (*1)
PHP event manager based on regex. Trigger events and attach listeners, core feature
easy to understand and to extend., (*2)
Install
$ composer install gianarb/fast-event-manager
Usage
Getting Started
This is the basic usage, (*3)
<?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, (*4)
$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., (*5)
$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, (*6)
$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!
}