forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
57 lines (45 loc) · 1.59 KB
/
cli.php
File metadata and controls
57 lines (45 loc) · 1.59 KB
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
53
54
55
56
57
<?php
use Clue\React\Redis\Client;
use Clue\React\Redis\Factory;
use React\EventLoop\Loop;
use React\Promise\PromiseInterface;
require __DIR__ . '/../vendor/autoload.php';
$factory = new Factory();
echo '# connecting to redis...' . PHP_EOL;
$factory->createClient('localhost')->then(function (Client $client) {
echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL;
Loop::addReadStream(STDIN, function () use ($client) {
$line = fgets(STDIN);
if ($line === false || $line === '') {
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
Loop::removeReadStream(STDIN);
return $client->end();
}
$line = rtrim($line);
if ($line === '') {
return;
}
$params = explode(' ', $line);
$method = array_shift($params);
$promise = call_user_func_array(array($client, $method), $params);
// special method such as end() / close() called
if (!$promise instanceof PromiseInterface) {
return;
}
$promise->then(function ($data) {
echo '# reply: ' . json_encode($data) . PHP_EOL;
}, function ($e) {
echo '# error reply: ' . $e->getMessage() . PHP_EOL;
});
});
$client->on('close', function() {
echo '## DISCONNECTED' . PHP_EOL;
Loop::removeReadStream(STDIN);
});
}, function (Exception $error) {
echo 'CONNECTION ERROR: ' . $error->getMessage() . PHP_EOL;
if ($error->getPrevious()) {
echo $error->getPrevious()->getMessage() . PHP_EOL;
}
exit(1);
});