Phossa-event is an event management library for PHP. It decoupled from any
packages other than the phossa/phossa-shared
. It requires PHP5.4 only., (*3)
-
Simple usage, (*7)
$dispatcher = new EventDispatcher();
// bind event 'user.login' to a callable
$dispatcher->on('user.login', function(Event $evt) {
// ...
});
// trigger the 'user.login' event, passing data
$dispatcher->trigger('user.login', [ 'user' => $user ]);
or use statically (global dispatching), (*8)
// bind event with priority 60 (0 - 100, high # higher priority)
EventDispatcher::on('user.login', function(Event $evt) {
// ...
}, 60);
// trigger the 'user.login' event
EventDispatcher::trigger('user.login', [ 'user' => $user ]);
-
Trigger for limited times, (*9)
// bind event for once
$dispatcher->one('user.login', function(Event $evt) {
// ...
});
// allow 3 times
$dispatcher->many('user.tag', 3, function(Event $evt) {
// ...
});
-
Event globbing, (*10)
// globbing
$dispatcher->on('user.*', function(Event $evt) {
//...
});
-
Detach events, (*11)
// detach
$dispatcher->off('user.*');
-
Monitoring PHP errors, (*12)
Execute a callable when PHP error happens., (*13)
// callable returns bool
$dispatcher->error(function($errno, $errstr, $errfile, $errline) {
// ...
return true;
});
-
Execute a callable when script finishes, (*14)
// run this after script ends
$dispatcher->ready(function() {
// ...
});
-
Event listener, (*16)
use Phossa\Event\Interfaces;
class MyListener implements Interfaces\EventListenerInteface
{
/*
* Get events and callables MyListener listens to
*/
public function getEventsListening()
{
return array(
'eventName1' => 'method1', // method1 of $this
'eventName2' => array('method2', 20), // priority 20
'eventName3' => array( // multiple callables
[ 'method3', 70 ],
[ 'method4', 50 ]
)
);
}
}
$listener = new MyListener();
-
Event manager, (*17)
// create an event manager
$evtManager = new Event\EventManager();
// attach a listener object
$evtManager->attachListener($listener);
// attach a callable directly to 'oneSpecialEvent'
$callable = function(Event\Event $evt) {
...
};
$evtManager->attachListener($callable, 'oneSpecialEvent');
// detach a callable
$evtManager->detachListener($callable);
-
The event-aware subject, (*18)
use Phossa\Event\Interfaces;
class MyEventAware implements Interfaces\EventAwareInterface
{
// add setEventManager() and triggerEvent()
use Interfaces\EventAwareTrait;
...
}
$subject = new MyEventAware();
-
Combine together, (*19)
// set manager to event-aware subject
$subject->setEventManager($evtManager);
// trigger event
$subject->triggerEvent('eventName2');
-
Event globbing, (*20)
Able to listen to all events by using '' or 'event'., (*21)
use Phossa\Event\Interfaces\EventListenerInterface;
class Listener implements EventListenerInterface
{
public function getEventsListening()
{
return [
'evtTest1' => 'testC',
'evtTest2' => [ 'testD', 20 ],
'evtTest3' => [
[ 'testA', 70 ],
[ 'testB', 50 ]
],
// globbing
'evt*' => 'bingo',
'evtTest*' => 'bingo2',
'*' => 'wow',
];
}
}
-
Event management for static classes., (*22)
The static listener class,, (*23)
use Phossa\Event\Interfaces;
class StaticListener implements Interfaces\EventListenerStaticInteface
{
/*
* Get events and callables StaticListener listens to
*/
public static function getEventsListening()
{
return array(
'eventName1' => 'method1', // method1 of $this
'eventName2' => array('method2', 20), // priority 20
'eventName3' => array( // multiple callables
[ 'method3', 70 ],
[ 'method4', 50 ]
)
);
}
}
The static subject class to use events,, (*24)
use Phossa\Event\Interfaces;
class StaticEventAware implements Interfaces\EventAwareStaticInterface
{
// add setEventManager() and triggerEvent()
use Interfaces\EventAwareStaticTrait;
...
}
The static subject class trigger events as follows,, (*25)
// create an event manager/dispatcher
$evtManager = new Event\EventManager();
// attach a static listener class
$evtManager->attachListener(StaticListener::CLASS);
// set manager/dispatcher to event-aware static subject class
StaticEventAware::setEventManagerStatically($evtManager);
// trigger event by the static subject class
StaticEventAware::triggerEventStatically('eventName2');
-
Composite event manager, (*26)
Able to use composite event manager as follows,, (*27)
use Phossa\Event;
// global event manager
$global_manager = new Event\EventManager();
$global_manager->attachListener($some_global_event_listener);
// local event manager
$local_manager = new Event\Variation\EventManagerComposite();
$local_manager->attachListener($local_listener);
// allow local event manager dispatch events to global event manager
$local_manager->setOtherManager('global', $global_manager);
// the event aware subject
$subject = new MyEventAware();
// set event manager
$subject->setEventManager($local_manager);
// fire up an event, will look into event handling queue from both
// $local_manager and $global_manager
$subject->triggerEvent('some_event');
-
Immutable event manager, (*28)
use Phossa\Event;
// low-level manager to hide
$_evtManager = new EventManager();
$_evtManager->attachListener(...);
...
// expose an immutable event managet to user
$evtManager = new Variation\ImmutableEventManager($_evtManager);
// cause an exception
$evtManager->detachListener( ... );
-
Shareable event manager, a single copy of global manager and lots of
local managers., (*29)
// get global copy by using static method `getInstance()`
$globalEventManager = ShareableEventManager::getInstance();
// normal event managers
$localEventManager = new ShareableEventManager();
// is this the global copy?
if ($evtManager->isShareable()) {
...
} else {
...
}