cleup/events

A convenient event system for modifying and extending the web application platform

v1.0.1 2024-06-17 06:59 UTC

This package is auto-updated.

Last update: 2024-09-17 07:27:57 UTC


README

Installation

Install the cleup/events library using composer:

composer require cleup/events

Usage

Event initialization
use Cleup\Components\Events\Event;

# Simple event
Event::apply('customEvent');

# Event with arguments
$name = 'Eduard';
$age = 30;
$fields = array(
    'login' => 'priveted',
    'type' => 'admin'
);
Event::apply('customEvent', $name, $age, $fields, ...);
Add a new event
use Cleup\Components\Events\Event;

# With the function
Event::add('customEvent', function () {
   print_r("Hi, I've been added to the event thread");
});

# Using the function name
function helloWorld () {
    print_r('Hello World!');
}

Event::add('customEvent', 'helloWorld');

# Using the class method
Event::add('customEvent', [Example::class, 'get']);
Modifiers for adding an event

Assign the position of the callback execution.

use Cleup\Components\Events\Event;

Event::add('getPosts', function (&$postList) {
    // ...
})->position(100);

Create an ID for the event. You can use this ID to delete a specific event

use Cleup\Components\Events\Event;

Event::add('getPosts', function (&$postList) {
    // ...
})->id('isBadPost');

Execute once. The modifier can be useful if the event is executed multiple times or in a loop.

use Cleup\Components\Events\Event;

Event::add('postItem', function ($post) {
    // This event will be deleted immediately after execution
})->once();

$posts = array(
    0 => [
        'id' => 1
        'title' => 'First post'
    ],
    1 => [
        'id' => 2
        'title' => 'Hello world!'
    ]
)

foreach($posts as $post) {
    // The event will be executed only once and will be deleted
    Event::apply('postItem', $post);
}
Delete event
Event::delete('getdPosts');

// Delete event by ID
Event::delete('getdPosts', 'isBadPost');