-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
cli.php
50 lines (37 loc) · 1.24 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;
}
$args = explode(' ', $line);
$command = strtolower(array_shift($args));
// special method such as end() / close() called
if (in_array($command, ['end', 'close'])) {
$redis->$command();
return;
}
$promise = $redis->callAsync($command, ...$args);
$promise->then(function ($data): void {
echo '# reply: ' . json_encode($data) . PHP_EOL;
}, function (Throwable $e): void {
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;