dida/eventbus

A lightweight PHP Event Bus library.

v1.1.0 2024-05-14 08:58 UTC

This package is auto-updated.

Last update: 2024-05-14 09:03:07 UTC


README

A lightweight PHP Event Bus library for Dida Framework.

API

public static $context        = null;
public static $global_context = null;

public static function addEvent(string $event): void;
public static function removeEvent(string $event): void;
public static function hasEvent(string $event):bool

public static function hook(string $event, callable $callback, array $parameters = [], ?string $callback_id = null);
public static function unhook(string $event, ?string $callback_id = null): void;

public static function trigger(string $event): void;

Code


use Dida\EventBus;

// Add an event.
EventBus::addEvent("YOUR.EVENT");

// Hook some callbacks.
...
EventBus::hook("YOUR.EVENT", your_callback_1, [param1, param2, ...]);
...
EventBus::hook("YOUR.EVENT", your_callback_2, [param1, param2, ...]);
...
EventBus::hook("YOUR.EVENT", your_callback_3, [param1, param2, ...]);

// Optional: Put some data to $context or $global_context if needed.
EventBus::$context = some_data
EventBus::$global_context = global_data

// Trigger an event and execute all hooked callbacks.
// If $context/$global_context is set, now callbacks can use them.
EventBus::trigger("YOUR.EVENT");