PRIORITY_MIN
PRIORITY_MIN = 99 : integer
Minimum event handler priority
Event manager.
use donbidon\Core\Event\Manager;
use donbidon\Core\Event\Args;
function firstEventHandler($name, I_Registry $args)
{
echo sprintf("--- %s(%s)\n", __FUNCTION__, $name);
$args->set(__FUNCTION__, true);
print_r($args->get());
}
function secondEventHandler($name, I_Registry $args)
{
echo sprintf("--- %s(%s)\n", __FUNCTION__, $name);
$args->set(__FUNCTION__, true);
print_r($args->get());
}
function thirdEventHandler($name, I_Registry $args)
{
echo sprintf("--- %s(%s)\n", __FUNCTION__, $name);
$args->set(__FUNCTION__, true);
print_r($args->get());
}
function breakingEventHandler($name, I_Registry $args)
{
$args->set(__FUNCTION__, true);
$args->set(':break:', true);
echo sprintf("--- %s(%s)\n", __FUNCTION__, $name);
print_r($args->get());
}
$evtManager = new Manager;
$evtManager->addHandler('event', 'firstEventHandler');
$evtManager->addHandler('event', 'breakingEventHandler');
$evtManager->addHandler('event', 'secondEventHandler');
$evtManager->addHandler('event', 'thirdEventHandler', Manager::PRIORITY_HIGH);
$evtManager->addHandler('otherEvent', 'thirdEventHandler');
$evtManager->addHandler('otherEvent', 'firstEventHandler');
$ars = new Args(['arg' => 'value']);
$evtManager->fire('event', $ars);
$ars = new Args;
$evtManager->fire('otherEvent', $ars);
outputs
--- thirdEventHandler(event)
Array
(
[arg] => value
[thirdEventHandler] => 1
)
--- firstEventHandler(event)
Array
(
[arg] => value
[thirdEventHandler] => 1
[firstEventHandler] => 1
)
--- breakingEventHandler(event)
Array
(
[arg] => value
[thirdEventHandler] => 1
[firstEventHandler] => 1
[breakingEventHandler] => 1
[:break:] => 1
)
--- thirdEventHandler(otherEvent)
Array
(
[thirdEventHandler] => 1
)
--- firstEventHandler(otherEvent)
Array
(
[thirdEventHandler] => 1
[firstEventHandler] => 1
)
addHandler(string $name, callback $handler, integer $priority = self::PRIORITY_DEFAULT) : void
Adds event handler.
To break event handling handler must call $args->set(':break:', true).
string | $name | Event name |
callback | $handler | Event handler callback |
integer | $priority | Event priority: Manager::PRIORITY_LOW, Manager::PRIORITY_DEFAULT or Manager::PRIORITY_HIGH, lower number means higher priority |
In case of invalid priority.
dropHandlers(string $name = '', mixed $handler = null) : void
Drops event handlers.
Example:
// drop all 'someEventName' event handlers
Manager::dropHandler('someEventName');
// drop all 'someEventName' event handlers processing by $object methods only
Manager::dropHandler('someEventName', $object);
string | $name | Event name |
mixed | $handler | Handler or its part criteria |
In case of dropping debug event.
fire(string $name, \donbidon\Core\Registry\I_Registry $args, boolean $fireAgain = false) : void
Fires event.
string | $name | Event name |
\donbidon\Core\Registry\I_Registry | $args | Event arguments |
boolean | $fireAgain | Allow to fire event again |
When invalid handler passed or event already fired.