Event dispatcher
EventDispatcher class and methods
This library is abandoned. Please consider using a different library.
An event dispatcher is in charge of registering event listeners and dispatching events to them.
Methods
handle
Registers an event listener and returns an \Opis\Routing\Route
object.
signature
/**
* @param string $event A pattern for event name
* @param callable $callback Invoked callback
* @param int $priority An optional priority
* @return \Opis\Routing\Route;
*/
public function handle(string $event, callable $callback, int $priority = 0): Route
usage
use Opis\Events\{
Event,
EventDispatcher
};
$dispatcher = new EventDispatcher();
$route = $target->handle("system.{action}", function (Event $event) {
// Do something ...
}, 1);
$route->where("action", "start|stop");
// ...
For more info about routes you can check the Opis Routing Framework.
dispatch
Dispatches an event (can be a custom event).
signature
/**
* @param \Opis\Events\Event $event
* @return \Opis\Events\Event The dispatched event
*/
public function dispatch(Event $event): Event
usage
use Opis\Events\{
Event,
EventDispatcher
};
$dispatcher = new EventDispatcher();
$event = new Event("system.update", true);
$dispatcher->dispatch($event);
emit
This method acts as a shortcut for dispatching standard events.
signature
/**
* @param string $name Event name
* @param bool $cancellable Optionally, mark the event as cancellable
* @return \Opis\Events\Event
*/
public function emit(string $name, bool $cancellable = false): Event
usage
use Opis\Events\EventDispatcher;
$dispatcher = new EventDispatcher();
$event = $dispatcher->emit("system.update", true);
Serialization
An Opis\Events\EventTarget
object is fully serializable
thanks to Opis Closure.
use Opis\Events\{
Event,
EventDispatcher
};
$dispatcher = new EventDispatcher();
$dispatcher->handle("system.start", function () {
echo "System started";
});
$dispatcher->handle("system.stop", function () {
echo "System stopped";
});
$dispatcher = unserialize(serialize($target));
$dispatcher->emit("system.start");
$dispatcher->emit("system.stop");
// >
// System started
// System stopped