update to PHP-8.1
[m6w6/seekat] / examples / amploop.php
1 #!/usr/bin/env php
2 <?php
3
4 require_once __DIR__."/../vendor/autoload.php";
5
6 use seekat\API;
7 use Amp\Loop as AmpLoop;
8
9 $client = new http\Client("curl", "seekat");
10 $client->configure([
11 "use_eventloop" => new class($client, AmpLoop::get()) implements http\Client\Curl\User {
12
13 /**
14 * Timeout ID
15 */
16 private ?string $timeout = null;
17 private \Closure $runcb;
18
19 function __construct(private http\Client $client, private AmpLoop\Driver $driver) {
20 }
21
22 function init($run) : void {
23 $this->runcb = $run(...);
24 }
25
26 /**
27 * @param resource $socket
28 */
29 function run($socket = null, int $action = null) : void {
30 if ($socket) {
31 $remaining = ($this->runcb)($this->client, $socket, $action);
32 } else {
33 $remaining = ($this->runcb)($this->client);
34 }
35
36 if (!$remaining) {
37 $this->done();
38 }
39 }
40 function once() : void {
41 if (!$this->driver->getInfo()["running"]) {
42 $this->driver->run();
43 }
44 }
45 function send() : never {
46 # unused
47 throw new BadMethodCallException(__FUNCTION__);
48 }
49 function wait(int $timeout_ms = null) : never {
50 # unused
51 throw new BadMethodCallException(__FUNCTION__);
52 }
53 function timer(int $timeout_ms = null) : void {
54 if ($this->timeout) {
55 $this->driver->cancel($this->timeout);
56 }
57
58 $this->timeout = $this->driver->delay($timeout_ms, function() {
59 $this->run();
60 });
61 }
62 function done() : void {
63 if ($this->timeout) {
64 $this->driver->cancel($this->timeout);
65 $this->timeout = null;
66 }
67 }
68
69 /**
70 * @param resource $socket
71 */
72 function socket($socket, int $poll) : void {
73 foreach ((array) $this->driver->getState((string) $socket) as $id) {
74 $this->driver->disable($id);
75 }
76 switch ($poll) {
77 case self::POLL_NONE:
78 break;
79 case self::POLL_IN:
80 $id = $this->driver->onReadable($socket, function($id, $socket) {
81 $this->run($socket, self::POLL_IN);
82 });
83 $this->driver->setState((string) $socket, $id);
84 break;
85 case self::POLL_OUT:
86 $id = $this->driver->onWritable($socket, function($id, $socket) {
87 $this->run($socket, self::POLL_OUT);
88 });
89 $this->driver->setState((string) $socket, $id);
90 break;
91 case self::POLL_INOUT:
92 $id = [
93 $this->driver->onReadable($socket, function($id, $socket) {
94 $this->run($socket, self::POLL_IN);
95 }),
96 $this->driver->onWritable($socket, function($id, $socket) {
97 $this->run($socket, self::POLL_OUT);
98 })
99 ];
100 $this->driver->setState((string) $socket, $id);
101 break;
102 case self::POLL_REMOVE:
103 foreach ((array) $this->driver->getState((string) $socket) as $id) {
104 $this->driver->cancel($id);
105 }
106 $this->driver->setState((string) $socket, null);
107 break;
108 }
109 }
110 }
111 ]);
112
113 $future = API\Future\amp();
114
115 $api = include "examples.inc";
116
117 AmpLoop::run(function() use($api) {
118 list($m6w6, $seekat) = yield [$api->users->m6w6(), $api->repos->m6w6->seekat()];
119 printf("Hi, my name is %s!\n", $m6w6->login);
120 printf("Have fun with %s; %s!\n", $seekat->name, $seekat->description);
121 });