PHP code example of cspray / labrador-async-event

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

    

cspray / labrador-async-event example snippets


 declare(strict_types=1);

namespace Labrador\AsyncEvent\Demo;

use Amp\Future;
use Labrador\AsyncEvent\AbstractEvent;
use Labrador\AsyncEvent\Event;
use Labrador\AsyncEvent\Listener;
use Labrador\CompositeFuture\CompositeFuture;

final class MyDomainObject {}

/**
 * @extends AbstractEvent<MyDomainObject>
 */
final class MyDomainEvent extends AbstractEvent {
    public function __construct(MyDomainObject $object) {
        parent::__construct('my-domain-event', $object);
    }
}

/**
 * @implements Listener<MyDomainObject>
 */
final class MyListener implements Listener {
    public function handle(Event $event) : Future|CompositeFuture|null {
        return null;
    }

}

 declare(strict_types=1);

namespace Labrador\AsyncEvent\Demo;

use Amp\CompositeException;use Labrador\AsyncEvent\AmpEmitter;

$emitter = new AmpEmitter();

// You can remove the Listener later by calling $registration->remove()
$registration = $emitter->register('my-domain-event', new MyListener());
$myDomainObject = new MyDomainObject();

// Emit an event and call an await method on the CompositeFuture returned
$emitter->emit(new MyDomainEvent($myDomainObject))->await();

// Queue a fire & forget event, pass a callback to `finished()` if you want 
// to know when the listeners for queued event are finished
$emitter->queue(new MyDomainEvent($myDomainObject))
    ->finished(
        static fn(?CompositeException $exception, array $values) => doSomething()
    );