-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
cli.php
50 lines (37 loc) · 1.23 KB
/
cli.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
<?php
// $ php examples/cli.php
// $ REDIS_URI=localhost:6379 php examples/cli.php
use React\EventLoop\Loop;
require __DIR__ . '/../vendor/autoload.php';
$redis = new Clue\React\Redis\RedisClient(getenv('REDIS_URI') ?: 'localhost:6379');
Loop::addReadStream(STDIN, function () use ($redis) {
$line = fgets(STDIN);
if ($line === false || $line === '') {
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
Loop::removeReadStream(STDIN);
$redis->end();
return;
}
$line = rtrim($line);
if ($line === '') {
return;
}
$params = explode(' ', $line);
$method = array_shift($params);
assert(is_callable([$redis, $method]));
$promise = $redis->$method(...$params);
// special method such as end() / close() called
if (!$promise instanceof React\Promise\PromiseInterface) {
return;
}
$promise->then(function ($data) {
echo '# reply: ' . json_encode($data) . PHP_EOL;
}, function ($e) {
echo '# error reply: ' . $e->getMessage() . PHP_EOL;
});
});
$redis->on('close', function() {
echo '## DISCONNECTED' . PHP_EOL;
Loop::removeReadStream(STDIN);
});
echo '# Entering interactive mode ready, hit CTRL-D to quit' . PHP_EOL;