add Amp\Loop example
[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 $log = new Monolog\Logger("seekat");
10 $log->pushHandler((new Monolog\Handler\StreamHandler(STDERR))->setLevel(Monolog\Logger::INFO));
11
12 $cli = new http\Client("curl", "seekat");
13 $cli->configure([
14 "use_eventloop" => new class($cli, AmpLoop::get()) implements http\Client\Curl\User {
15
16 private $driver;
17 private $timeout;
18 private $client;
19 private $runcb;
20
21 function __construct(http\Client $client, AmpLoop\Driver $driver) {
22 $this->client = $client;
23 $this->driver = $driver;
24 }
25 function init($run) {
26 $this->runcb = $run;
27 }
28 function run($socket = null, int $action = null) {
29 if ($socket) {
30 $remaining = ($this->runcb)($this->client, $socket, $action);
31 } else {
32 $remaining = ($this->runcb)($this->client);
33 }
34
35 if (!$remaining) {
36 $this->done();
37 }
38 }
39 function once() {
40 if (!$this->driver->getInfo()["running"]) {
41 $this->driver->run();
42 }
43 }
44 function send() {
45 # unused
46 throw new BadMethodCallException(__FUNCTION__);
47 }
48 function wait(int $timeout_ms = null) {
49 # unused
50 throw new BadMethodCallException(__FUNCTION__);
51 }
52 function timer(int $timeout_ms = null) {
53 if ($this->timeout) {
54 $this->driver->cancel($this->timeout);
55 }
56
57 $this->timeout = $this->driver->delay($timeout_ms, function() {
58 $this->run();
59 });
60 }
61 function done() {
62 if ($this->timeout) {
63 $this->driver->cancel($this->timeout);
64 $this->timeout = null;
65 }
66 }
67 function socket($socket, int $poll) {
68 foreach ((array) $this->driver->getState((string) $socket) as $id) {
69 $this->driver->disable($id);
70 }
71 switch ($poll) {
72 case self::POLL_NONE:
73 break;
74 case self::POLL_IN:
75 $id = $this->driver->onReadable($socket, function($id, $socket) {
76 $this->run($socket, self::POLL_IN);
77 });
78 $this->driver->setState((string) $socket, $id);
79 break;
80 case self::POLL_OUT:
81 $id = $this->driver->onWritable($socket, function($id, $socket) {
82 $this->run($socket, self::POLL_OUT);
83 });
84 $this->driver->setState((string) $socket, $id);
85 break;
86 case self::POLL_INOUT:
87 $id[] = $this->driver->onReadable($socket, function($id, $socket) {
88 $this->run($socket, self::POLL_IN);
89 });
90 $id[] = $this->driver->onWritable($socket, function($id, $socket) {
91 $this->run($socket, self::POLL_OUT);
92 });
93 $this->driver->setState((string) $socket, $id);
94 break;
95 case self::POLL_REMOVE:
96 foreach ((array) $this->driver->getState((string) $socket) as $id) {
97 $this->driver->cancel($id);
98 }
99 $this->driver->setState((string) $socket, null);
100 break;
101 }
102 }
103 }]);
104
105 $api = new API(API\Future\amp(), [
106 "Authorization" => "token ".getenv("GITHUB_TOKEN")
107 ], null, $cli, $log);
108
109 AmpLoop::run(function() use($api) {
110 list($m6w6, $seekat) = yield [$api->users->m6w6(), $api->repos->m6w6->seekat()];
111 printf("Hi, my name is %s!\n", $m6w6->login);
112 printf("Have fun with %s; %s!\n", $seekat->name, $seekat->description);
113 });