2017 © Pedro Peláez
 

library events

Observer design pattern-like events system

image

sugiphp/events

Observer design pattern-like events system

  • Monday, May 30, 2016
  • by tzappa
  • Repository
  • 3 Watchers
  • 3 Stars
  • 450 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

Events

Build Status, (*1)

Observer design pattern-like events system., (*2)

Installation

composer require sugiphp/events ~1.0

Usage

Event

Event is a simple object identified by it's unique name. When an event is fired the Event Dispatcher notifies registered listeners for that particular event name., (*3)

Listener

Any function or method that takes no more than one argument can act as a listener. When an event is fired the dispatcher calls all registered listeners (functions) one by one., (*4)

Dispatcher

Dispatcher have most significant role in the events systems. All events are fired via the dispatcher. The dispatcher checks for any listeners that are registered with that event and notifies them., (*5)

// create a dispatcher
$dispatcher = new Dispatcher();
// register one or more listeners for one or more events
$dispatcher->addListener("user.login", function ($event) {
    // this function will be executed when an event with name "user.login" is fired
});

// fires an event
$dispatcher->dispatch(new Event("user.login"));

Passing data

All listeners should have only one parameter - the event. If we need to pass additional info to those functions we can transport the date with the Event., (*6)

$dispatcher->addListener("user.login", function ($event) {
    // get one property
    echo $event->getParam("id"); // 1
    // get a property as Array
    echo $event["username"]; // "demo"
    // fetch all data
    $event->getParams(); // array("id" => 1, "username" => "demo")
});
$event = new Event("user.login", array("id" => 1, "username" => "demo"));
$dispatcher->dispatch($event);

You might need to exchange data between one listener and another. You can do that by adding and altering the data in the event with setParam() method., (*7)

$dispatcher->addListener("user.login", function ($event) {
    if ("mike" == $event["username"]) {
        // array access
        $event["is_admin"] = true;
    } else {
        // using setParam() method
        $event->setParam("is_admin", false);
    }
});

$dispatcher->addListener("user.login", function ($event) {
    if ($event["is_admin"]) {
        echo "Hello Admin";
    }
});

$event = new Event("user.login", array("username" => "mike"));
$dispatcher->dispatch($event);


The Versions

30/05 2016

dev-master

9999999-dev

Observer design pattern-like events system

  Sources   Download

MIT

The Requires

  • php >=5.5

 

by Plamen Popov

dispatcher event observer

16/12 2014

1.0.0

1.0.0.0

Observer design pattern-like events system

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Plamen Popov

dispatcher event observer