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;

use ICanBoogie\Event\Config;

/* @var Config $config */

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

    static $collection;

    return $collection ??= new EventCollection($config);

});

# Getting the event collection

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



namespace App;

use ICanBoogie\Event;

final class CacheClearedEvent extends Event
{
}



namespace App\Document;

use App\Document;
use ICanBoogie\Event;

final class SavedEvent extends Event
{
    public function __construct(
        Document $document,
        public readonly bool $created,
    ) {
        parent::__construct($document);
    }
}



namespace ICanBoogie;

/* @var Event $event */

emit($event); // returns $event, for chaining



namespace App;

use App\Document\SavedEvent;

/* @var ICanBoogie\EventCollection $events */

$detach = $events->attach(function(SavedEvent $event, Document $document) {

    // …

});

# or, to listen to the event whatever its sender

$detach = $events->attach(function(SavedEvent $event) {

    // …

});

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



namespace App;

use App\Document\SavedEvent;

final class AuditTrail
{
    public function __construct(
        private readonly AuditLog $log,
    ) {
    }

    public function __invoke(SavedEvent $event, Document $document): void
    {
        $this->log->record($document, $event->created ? 'created' : 'updated');
    }
}

// …

/* @var ICanBoogie\EventCollection $events */
/* @var AuditLog $log */

$events->attach(new AuditTrail($log));



namespace App;

use App\Document\SavedEvent;

use function ICanBoogie\emit;

/* @var ICanBoogie\EventCollection $events */
/* @var Document $document */

$detach = $events->attach_to($document, function(SavedEvent $event, Document $sender) {

    echo "invoked!";

});

$document_clone = clone $document;

emit(new SavedEvent($document_clone, created: false));   // nothing happens, it's a clone
emit(new SavedEvent($document, created: false));         // echo "invoked!"

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



namespace App;

use function ICanBoogie\emit;

/* @var ICanBoogie\EventCollection $events */

$n = 0;

$events->once(function(CacheClearedEvent $event) use (&$n): void {

    $n++;

});

emit(new CacheClearedEvent());
emit(new CacheClearedEvent());
emit(new CacheClearedEvent());

echo $n;   // 1



use ICanBoogie\Event\ConfigBuilder;
use ICanBoogie\HTTP\RecoverEvent;

$config = new ConfigBuilder()
    ->attach(MyEvent::class, $listener)                                     // event without sender
    ->attach_to(RuntimeException::class, RecoverEvent::class, $listener)    // event with sender, by sender class
    ->enable_discovery()                                                    // #[Listener] methods, found by build()
    ->build();

$events = new EventCollection($config);



namespace App;

use App\Document\SavedEvent;
use ICanBoogie\Event\Listener;

final class OnSaved
{
    #[Listener]
    public function __invoke(SavedEvent $event): void
    {
        // …
    }

    #[Listener]
    public static function on_invoice_saved(SavedEvent $event, Invoice $sender): void
    {
        // …
    }
}



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());

echo $event->count; // 0123



namespace App;

use App\Document\SavedEvent;

function skip_drafts(SavedEvent $event, Document $document): void
{
    if ($document->is_draft) {
        $event->stop();
    }
}



class MyEvent extends Event
{
   public function __construct(RuntimeException $exception)
   {
       parent::__construct($exception); // the exception is the sender
   }
}

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



$builder->attach_to(Throwable::class, RecoverEvent::class, $listener);



use ICanBoogie\EventProfiler;

foreach (EventProfiler::iter_unused() as $record)    // UnusedRecord
{
    echo "$record->type at $record->time";
}

foreach (EventProfiler::iter_calls() as $record)     // CallRecord
{
    echo "$record->type took $record->duration, handled by " . get_debug_type($record->listener);
}

EventProfiler::reset_unused();
EventProfiler::reset_calls();