PHP code example of chipslays / event

1. Go to this page and download the library: Download chipslays/event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chipslays / event example snippets


$event->on(..., function () {...}, $sort);
$event->on(..., '\App\Controller@method', $sort);

$event->on(..., [function ($param1, $param2) {...}, $param1, $param2], $sort);
$event->on(..., ['\App\Controller@method', $param1, $param2], $sort);

$event->on(true, ..., ...);

use Chipslays\Event\Event;

message' => [
        'text' => 'hello',
    ],
    'user' => [
        'id' => 1234567890,
        'firstname' => 'John',
        'lastname' => 'Doe'
    ],
]);

// Callable function
$event->on('message.text', function () {
    echo 'This event has `text`';
});

// Class
$event->on('message.text', '\App\SomeController@greeting');

$event->run();

$event->on(['message.text' => 'hello'], function () {
    echo 'Hello 👋';
});

$event->on(['*.text' => 'hello'], function () {
    echo 'Hello 👋';
});

// At least one "OR Bye OR Goodbye" logic
$event->on([['message.text' => 'Bye'], ['message.text' => 'Goodbye']], function () {
    echo 'Bye!';
});

$event->on(['*.text' => 'My name is {name}'], function ($name) {
    echo "Nice to meet you, {$name}!";
});

// {user} - is a  a optional parameter, it may not be in text
$event->on(['*.text' => '/ban {user} {:time?}'], function ($user = null, $time = null) {
    echo "Banned: {$user}, time:" . $time ?? strtotime('+7 day');
});

$event->on('{message}', function ($message) {
    echo "Your message: {$message}";
});

$event->on(['*.text' => '/^hello$/i'], function () {
    echo "Hello!";
});

// For prevent chain function must return False
$event->on(['*.text' => 'Hi!'], function () {
    echo "Hello!";
    return false;
});

$event->on(['*.firstname' => 'John'], function () {
    echo "This text will never be displayed";
});

// You can use sort for events
$event->on(['*.firstname' => 'John'], function () {
    echo "This text will never be displayed";
}, 500);

$event->on(['*.text' => 'Hi!'], function () {
    echo "Hello!";
    return false;
}, 400);

// Pass callback args
$event->on('something', [function ($name, $email) {
    ...
}, 'John', '[email protected]']);

$event->on('something', ['SomeController@insert', 'John', '[email protected]']);

use Chipslays\Event\EventTrait;

class MyClass
{
    use EventTrait;

    // ...
}


use Chipslays\Event\EventTrait;

// Redefine methods in other trait
trait MyEventTrait
{
    /**
     * @param array|string $event
     * @param callable|string|array $callback
     * @param integer $sort
     * @return void
     */
    public function on($event, $callback, int $sort = 500)
    {
        // do something before...

        $this->addEvent($event, $callback, $sort);

        // do something after...
    }

    /**
     * @return void
     */
    public function run()
    {
        if ($this->handleEvents()) {
            echo 'At least one event was caught';
        } else {
            echo 'No event was caught';
        }
    }
}

// Create custom class
class MyEventClass
{
    use MyEventTrait, EventTrait {
        MyEventTrait::on insteadof EventTrait;
        MyEventTrait::run insteadof EventTrait;
    }

    // place some methods...
}

$event = new MyEventClass(['message' => ['text' => 'hello']]);

$event->on(['message.text' => 'hello'], function () {
    echo "👋 Hello!";
});

$event->run();