PHP code example of icanboogie / event

1. Go to this page and download the library: Download icanboogie/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/ */

    

icanboogie / event example snippets




namespace ICanBoogie;

/* @var Application $app */

EventCollectionProvider::define(function() use ($app) {

    static $collection;

    return $collection ??= new EventCollection($app->configs['event']);

});

# Getting the event collection

$events = EventCollectionProvider::provide();
# or
$events = get_events();



namespace ICanBoogie\Operation;

use ICanBoogie\Event;
use ICanBoogie\HTTP\Request;
use ICanBoogie\HTTP\Response;
use ICanBoogie\Operation;

class ProcessEvent extends Event
{
    /**
     * Reference to the response result property.
     */
    public mixed $result;

    public function __construct(
        Operation $target,
        public readonly Request $request,
        public readonly Response $response,
        mixed &$result
    ) {
        $this->result = &$result;

        parent::__construct($target);
    }
}



namespace ICanBoogie;

/* @var Event $event */

emit($event);



namespace ICanBoogie

$detach = $events->attach(function(Operation\BeforeProcessEvent $event, Operation $target) {

    // …

});

# or, if the event doesn't have a target

$detach = $events->attach(function(Operation\BeforeProcessEvent $event) {

    // …

});

$detach(); // You can detach if you no longer want to listen.



namespace ICanBoogie

class ValidateOperation
{
    private $rules;

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

    public function __invoke(Operation\BeforeProcessEvent $event, Operation $target)
    {
        // …
    }
}

// …

/* @var $events EventCollection */
/* @var $rules array<string, mixed> */

$events->attach(new ValidateOperation($rules));



namespace ICanBoogie;

use ICanBoogie\Routing\Controller;

// …

/* @var $events EventCollection */

$detach = $events->attach_to($controller, function(Controller\ActionEvent $event, Controller $target) {

    echo "invoked!";

});

$controller_clone = clone $controller;

emit(new Controller\ActionEvent($controller_clone, …));   // nothing happens, it's a clone
emit(new Controller\ActionEvent($controller, …));         // echo "invoked!"

// …

$detach(); // You can detach if you no longer want to listen.



namespace ICanBoogie;

/* @var $events EventCollection */

$n = 0;

$events->once(MyEvent $event, function() use(&$n) {

    $n++;

});

emit(new MyEvent());
emit(new MyEvent());
emit(new MyEvent());

echo $n;   // 1



namespace ICanBoogie;

class CountEvent extends Event
{
    public function __construct(
        public string $count = "0"
    ) {
        parent::__construct();
    }
}

/* @var $events EventCollection */

$events->attach(function(CountEvent $event): void {

    $event->count .= "2";

});

$events->attach(function(CountEvent $event): void {

    $event->count .= "1";

});

$events->attach(function(CountEvent $event): void {

    $event->chain(function(CountEvent $event) {

        $event->count .= "3";

    });
});

$event = emit(new CountEvent(0));

echo $event->count; // 0123



use ICanBoogie\Operation;

function on_event(Operation\ProcessEvent $event, Operation $operation): void
{
    $event->rc = true;
    $event->stop();
}



use ICanBoogie\EventProfiler;

foreach (EventProfiler::$unused as list($time, $type))
{
    // …
}

foreach (EventProfiler::$calls as list($time, $type, $hook, $started_at))
{
    // …
}