|
1 | 1 | <?php |
2 | 2 |
|
3 | | -use Clue\Redis\Protocol\Model\ErrorReply; |
4 | 3 | use Clue\React\Redis\Client; |
5 | 4 | use Clue\React\Redis\Factory; |
6 | | -use Clue\Redis\Protocol\Model\ModelInterface; |
| 5 | +use React\Promise\PromiseInterface; |
7 | 6 |
|
8 | 7 | require __DIR__ . '/../vendor/autoload.php'; |
9 | 8 |
|
|
15 | 14 | $factory->createClient('localhost')->then(function (Client $client) use ($loop) { |
16 | 15 | echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL; |
17 | 16 |
|
18 | | - $client->on('data', function (ModelInterface $data) { |
19 | | - if ($data instanceof ErrorReply) { |
20 | | - echo '# error reply: ' . $data->getMessage() . PHP_EOL; |
21 | | - } else { |
22 | | - echo '# reply: ' . json_encode($data->getValueNative()) . PHP_EOL; |
23 | | - } |
24 | | - }); |
25 | | - |
26 | 17 | $loop->addReadStream(STDIN, function () use ($client, $loop) { |
27 | 18 | $line = fgets(STDIN); |
28 | 19 | if ($line === false || $line === '') { |
29 | 20 | echo '# CTRL-D -> Ending connection...' . PHP_EOL; |
30 | | - $client->end(); |
31 | | - } else { |
32 | | - $line = rtrim($line); |
| 21 | + $loop->removeReadStream(STDIN); |
| 22 | + return $client->end(); |
| 23 | + } |
33 | 24 |
|
34 | | - if ($line === '') { |
| 25 | + $line = rtrim($line); |
| 26 | + if ($line === '') { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + $params = explode(' ', $line); |
| 31 | + $method = array_shift($params); |
| 32 | + $promise = call_user_func_array(array($client, $method), $params); |
35 | 33 |
|
36 | | - } else { |
37 | | - $params = explode(' ', $line); |
38 | | - $method = array_shift($params); |
39 | | - call_user_func_array(array($client, $method), $params); |
40 | | - } |
| 34 | + // special method such as end() / close() called |
| 35 | + if (!$promise instanceof PromiseInterface) { |
| 36 | + return; |
41 | 37 | } |
| 38 | + |
| 39 | + $promise->then(function ($data) { |
| 40 | + echo '# reply: ' . json_encode($data) . PHP_EOL; |
| 41 | + }, function ($e) { |
| 42 | + echo '# error reply: ' . $e->getMessage() . PHP_EOL; |
| 43 | + }); |
42 | 44 | }); |
43 | 45 |
|
44 | 46 | $client->on('close', function() use ($loop) { |
|
0 commit comments