for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
use Amp\Delayed;
use PHPinnacle\Ensign\HandlerRegistry;
use PHPinnacle\Ensign\SignalDispatcher;
require __DIR__ . '/../vendor/autoload.php';
class SimpleEvent
{
public $num;
public function __construct(int $num)
$this->num = $num;
}
class SimpleCommand
public $delay;
public function __construct(int $num, int $delay = 100)
$this->delay = $delay;
Amp\Loop::run(function () {
$handlers = new HandlerRegistry();
$handlers
->register(SimpleCommand::class, function (SimpleCommand $cmd) {
yield new Delayed($cmd->delay); // Just do some heavy calculations
yield SimpleEvent::class => new SimpleEvent($cmd->num + $cmd->num);
yield new Delayed($cmd->delay); // Do more work
yield ensign_send(new SimpleEvent($cmd->num * $cmd->num));
return $cmd->num;
})
->register(SimpleEvent::class, function (SimpleEvent $event) {
echo \sprintf('Signal dispatched with value: %d at %s' . \PHP_EOL, $event->num, \microtime(true));
;
$dispatcher = new SignalDispatcher($handlers);
$task = $dispatcher->dispatch(new SimpleCommand(10));
$data = yield $task;
echo \sprintf('Task resolved with value: %d at %s' . \PHP_EOL, $data, \microtime(true));
});