-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping-pong.php
52 lines (40 loc) · 976 Bytes
/
ping-pong.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
use PHPinnacle\Ensign\DispatcherBuilder;
require __DIR__ . '/../vendor/autoload.php';
class Ping
{
public $times;
public function __construct(int $times)
{
$this->times = $times;
}
}
class Pong extends Ping
{
}
class Stop
{
}
Amp\Loop::run(function () {
$builder = new DispatcherBuilder;
$builder
->register(Ping::class, function (Ping $cmd) {
if ($cmd->times > 0) {
$cmd->times--;
echo 'Ping!' . \PHP_EOL;
yield new Pong($cmd->times);
} else {
yield new Stop();
}
})
->register(Pong::class, function (Pong $cmd) {
echo 'Pong!' . \PHP_EOL;
yield new Ping($cmd->times);
})
->register(Stop::class, function () {
echo 'Stop!' . \PHP_EOL;
})
;
$dispatcher = $builder->build();
yield $dispatcher->dispatch(new Ping(\rand(2, 10)));
});