Ride: Event Library
Event library of the PHP Ride framework., (*1)
What's In This Library
EventManager
The EventManager interface is the facade for this event system.
You can add and remove event listeners and trigger events from here., (*2)
A generic implementation is provided by the GenericEventManager class., (*3)
Event
When you trigger an event on the EventManager, an Event instance is created and passed to the listeners.
The Event class holds the name of the event, potential arguments and gives you the possibility to stop cascading to the following listeners., (*4)
EventListener
An EventListener instance defines the event, the callback and weight of a listener.
Listeners with a lower weight, will be invoked before listeners with a higher weight., (*5)
EventLoader
The GenericEventManager has lazy load capabilities through the EventLoader interface., (*6)
This interface's purpose is to start resolving listeners when the event is triggered for the first time., (*7)
Code Sample
Check this code sample to see the possibilities of this library:, (*8)
<?php
use ride\library\event\loader\io\EventListenerIO;
use ride\library\event\loader\GenericEventLoader;
use ride\library\event\GenericEventManager;
use ride\library\event\Event;
$eventManager = new GenericEventManager();
// add some event listeners
$eventManager->addEventListener('event', 'callback'); // provide a name of the event and a callback
$eventManager->addEventListener('event', array('MyClass', 'callback'), 10); // added a weight to influence order
$eventManager->addEventListener('test', 'onEvent');
// trigger an event
$eventManager->triggerEvent('test');
$eventManager->triggerEvent('test', array('var' => 'value'));
// event listener callback
function onEvent(Event $event) {
echo $event->getName();
echo $event->getArgument('var');
$event->setPreventDefault(); // stop the listener cascade after this listener
}
// you can lazy load the events through an EventLoader
class YourEventListenerIO implements EventListenerIO {
public function readEventListeners() {
return array(
'event' => array(
new EventListener('event', 'callback'),
);
);
}
}
$eventListenerIO = new YourEventListenerIO();
$eventLoader = new GenericEventLoader($eventListenerIO);
// all the events will be read at the first trigger, but only initialized when
// the actual event is triggered
$eventManager->setEventLoader($eventLoader);
Installation
You can use Composer to install this library., (*9)
composer require ride/lib-event