Skip to content

all-ride/ride-lib-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ride: Event Library

Event library of the PHP Ride framework.

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.

A generic implementation is provided by the GenericEventManager class.

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.

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.

EventLoader

The GenericEventManager has lazy load capabilities through the EventLoader interface.

This interface's purpose is to start resolving listeners when the event is triggered for the first time.

Code Sample

Check this code sample to see the possibilities of this library:

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

composer require ride/lib-event

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages