\donbidon\Core\EventManager

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
)

Summary

Methods
Properties
Constants
addHandler()
dropHandlers()
disableHandler()
enableHandler()
hasHandlers()
getHandlers()
fire()
setDebug()
getDebugEvents()
No public properties found
PRIORITY_MIN
PRIORITY_LOW
PRIORITY_DEFAULT
PRIORITY_HIGH
PRIORITY_MAX
No protected methods found
$debugEvents
N/A
No private methods found
No private properties found
N/A

Constants

PRIORITY_MIN

PRIORITY_MIN = 99 : integer

Minimum event handler priority

PRIORITY_LOW

PRIORITY_LOW = 75 : integer

Low event handler priority

PRIORITY_DEFAULT

PRIORITY_DEFAULT = 50 : integer

Default event handler priority

PRIORITY_HIGH

PRIORITY_HIGH = 25 : integer

High event handler priority

PRIORITY_MAX

PRIORITY_MAX = 0 : integer

Maximum event handler priority

Properties

$debugEvents

$debugEvents : array

Array of event debugger hidden events

Type

array

Methods

addHandler()

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).

Parameters

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

Throws

\InvalidArgumentException

In case of invalid priority.

dropHandlers()

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);

Parameters

string $name

Event name

mixed $handler

Handler or its part criteria

Throws

\InvalidArgumentException

In case of dropping debug event.

disableHandler()

disableHandler(string  $name) : void

Disables handler.

Parameters

string $name

Event name

Throws

\InvalidArgumentException

In case of disabling debug event.

enableHandler()

enableHandler(string  $name) : void

Enables handler.

Parameters

string $name

Event name

hasHandlers()

hasHandlers(string  $name, mixed  $handler = null) : boolean

Returns true if there are any handlers for specified event.

Parameters

string $name

Event name

mixed $handler

Handler or its part criteria

Returns

boolean

getHandlers()

getHandlers(string  $name = null) : array

Returns handlers for passed event or all.

Parameters

string $name

Event name

Returns

array

fire()

fire(string  $name, \donbidon\Core\Registry\I_Registry  $args, boolean  $fireAgain = false) : void

Fires event.

Parameters

string $name

Event name

\donbidon\Core\Registry\I_Registry $args

Event arguments

boolean $fireAgain

Allow to fire event again

Throws

\RuntimeException

When invalid handler passed or event already fired.

setDebug()

setDebug(boolean  $debug) : void

Enables/disables debug events.

Parameters

boolean $debug

Enable/disable flag

getDebugEvents()

getDebugEvents() : array

Returns list of debug events.

Returns

array