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