PHP code example of paket / kurir

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

    

paket / kurir example snippets


final class MyEvent implements Event
{
    private $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function getMessage(): string
    {
        return $this->message;
    }
}

$kurir = new Kurir();

$kurir->subscribe(function (MyEvent $event) {
   echo $event->getMessage();
});

$kurir->emit(new MyEvent('Hello, World!'));

$listener = $eventSource->subscribe(function (MyEvent $event) {
   echo $event->getMessage();
});

$eventSource->unsubscribe($listener);

final class InsertedRecordEvent implements Event
{
    private $id;
    private $record;

    public function __construct(int $id, array $record)
    {
        $this->id = $id;
        $this->record = $record;
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function getRecord(): array
    {
        return $this->record;
    }
}

interface Storage
{
    public function insert(string $location, array $record): int;
}

final class RecordRepository
{
    private $storage;
    private $eventEmitter;

    public function __construct(Storage $storage, EventEmitter $eventEmitter)
    {
        $this->storage = $storage;
        $this->eventEmitter = $eventEmitter;
    }

    public function insertRecord(array $record)
    {
        $id = $this->storage->insert('record', $record);
        $this->eventEmitter->emit(new InsertedRecordEvent($id, $record));
    }
}

final class RecordLog
{
    private $eventSource;
    private $listener;

    public function __construct(EventSource $eventSource)
    {
        $this->eventSource = $eventSource;
        $this->listener = $this->eventSource->subscribe(function (InsertedRecordEvent $event) {
            echo "Inserted record {$event->getId()}";
        });
    }

    public function __destruct()
    {
        $this->eventSource->unsubscribe($this->listener);
    }
}


$kurir = new Kurir();
$repository = new RecordRepository($storage, $kurir);
$log = new RecordLog($kurir);
$repository->insertRecord(['action' => 'login', 'user' => 'joe', 'time' => 1631375296]);