Skip to content

fwk/Events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fwk\Events (Event Dispatcher)

Scrutinizer Code Quality Build Status Code Coverage Latest Stable Version Total Downloads Latest Unstable Version License

Event Dispatcher for PHP 5.4+

Installation

Via Composer:

{
    "require": {
        "fwk/events": "dev-master",
    }
}

If you don't use Composer, you can still download this repository and add it to your include_path PSR-0 compatible

Documentation

Adding Listeners

use Fwk\Events\Dispatcher,
    Fwk\Events\Event;

$evd = new Dispatcher();

// closures listeners
$evd->on("eventName", function(Fwk\Events\Event $event) {
    // do some stuff
});

// Class methods starting by "on[EventCamelCasedName]" can also be added as 
// listeners
class MyListener 
{
    public function onEventName(Fwk\Events\Event $event) {
        // do some stuff
    }
}

$evd->addListener(new MyListener());

Removing Listeners

/* ... */

// this removes all listeners for a given event
$evd->removeAllListeners("eventName");

// this removes a listener (callable) for a given event
$evd->removeListener("eventName", array($listener, "onListenerMethod"));

Trigger Events

/* ... */

$event = new Fwk\Events\Event("eventName", array(
    "someData" => "someValue"
));
$event->extraData = "extraValue";

// dispatch event
$evd->notify($event);

Contributions / Community

Legal

Fwk is licensed under the 3-clauses BSD license. Please read LICENSE for full details.