Observer design pattern-like events system
Observer design pattern-like events system., (*2)
composer require sugiphp/events ~1.0
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)
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 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"));
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);