Events

The Events class is part of the Libraries namespace and is designed to manage and handle events in the system. It provides methods to register, trigger, and process events. Below is a detailed explanation of each function and its purpose.

Funtions in Events Libraries

Property $listeners

<?php
protected static $listeners = [];
  • This is a static array that stores the list of listeners for each event.

  • Structure of $listeners:

<?php
[
    'EventName' => [
        priority => [listener1, listener2, ...],
        ...
    ],
    ...
]
  • EventName: The name of the event.

  • priority: The priority of the listener (higher numbers are executed first).

  • listener1, listener2: Callbacks or classes that handle the event.

on()

<?php
public static function on($eventName, $listener, $priority = 0) {}
  • Purpose: Registers a listener (callback or class) for a specific event.

  • Parameters:

    • $eventName: The name of the event (e.g., 'PostsAddEvent').

    • $listener: A callback function or class name that will be executed when the event is triggered.

    • $priority: The priority of the listener (default is 0).

run()

<?php
public static function run($eventName, $payload = null) {}
  • Purpose: Triggers (dispatches) an event.

  • Parameters:

    • $eventName: The name of the event to trigger.

    • $payload: Data to pass to the event (can be an array, object, etc.).

runs()

<?php
public static function runs(array $events) {}
  • Purpose: Triggers multiple events at once.

  • Parameters:

    • $events: An array of events and their associated data, in the format

    <?php
    [
        'EventName1' => $payload1,
        'EventName2' => $payload2,
        ...
    ]

General Workflow

  1. Registering Listeners:

    • Use the on() method to register listeners for events.

    • Listeners can be either callback functions or classes with a handle() method.

  2. Triggering Events:

    • Use the run() method to trigger a single event.

    • When an event is triggered:

      • Registered listeners are executed in order of priority.

      • If no listeners are registered, a default event class in the \App\Events namespace is checked and executed.

  3. Triggering Multiple Events:

    • Use the runs() method to trigger multiple events at once.

Example

Using basically

Events::on('UserRegistered', function ($user) {
    echo "Welcome, {$user['name']}!<br>";
}, 10); // Priority 10


Events::on('UserRegistered', function ($user) {
    echo "Sending welcome email to {$user['email']}...<br>";
}, 5); // Priority 5

$user = ['name' => 'John Doe', 'email' => '[email protected]'];
Events::run('UserRegistered', $user);

// ------------------------------ Output ------------------------------
// Welcome, John Doe!
// Sending welcome email to [email protected]...

Using in Class

class NotifyAdmin
{
    protected $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function handle()
    {
        echo "Notifying admin about new user: {$this->user['name']}<br>";
    }
}
Events::on('UserRegistered', NotifyAdmin::class, 8); // Priority 8

// ------------------------------ Output ------------------------------
// Welcome, John Doe!
// Notifying admin about new user: John Doe
// Sending welcome email to [email protected]...

Using multiple Events

<?php
// Register event 'OrderPlaced'
Events::on('OrderPlaced', function ($order) {
    echo "Order #{$order['id']} has been placed.<br>";
});

// Triggering Mmultiple Events
$events = [
    'UserRegistered' => $user,
    'OrderPlaced' => ['id' => 12345],
];

Events::runs($events);

// ------------------------------ Output ------------------------------
// Welcome, John Doe!
// Notifying admin about new user: John Doe
// Sending welcome email to [email protected]...
// Order #12345 has been placed.

Last updated

Was this helpful?