Skip to content

Commit 98df799

Browse files
authored
Merge pull request #117 from clue-labs/docs
Improve documentation structure and simplify example code
2 parents eefd5c2 + bd3dba1 commit 98df799

12 files changed

+451
-403
lines changed

README.md

Lines changed: 227 additions & 171 deletions
Large diffs are not rendered by default.

examples/cli.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@
33
// $ php examples/cli.php
44
// $ REDIS_URI=localhost:6379 php examples/cli.php
55

6-
use Clue\React\Redis\Client;
7-
use Clue\React\Redis\Factory;
86
use React\EventLoop\Loop;
9-
use React\Promise\PromiseInterface;
107

118
require __DIR__ . '/../vendor/autoload.php';
129

13-
$factory = new Factory();
10+
$factory = new Clue\React\Redis\Factory();
1411

1512
echo '# connecting to redis...' . PHP_EOL;
1613

17-
$factory->createClient(getenv('REDIS_URI') ?: 'localhost:6379')->then(function (Client $client) {
14+
$factory->createClient(getenv('REDIS_URI') ?: 'localhost:6379')->then(function (Clue\React\Redis\Client $redis) {
1815
echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL;
1916

20-
Loop::addReadStream(STDIN, function () use ($client) {
17+
Loop::addReadStream(STDIN, function () use ($redis) {
2118
$line = fgets(STDIN);
2219
if ($line === false || $line === '') {
2320
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
2421
Loop::removeReadStream(STDIN);
25-
return $client->end();
22+
return $redis->end();
2623
}
2724

2825
$line = rtrim($line);
@@ -32,10 +29,10 @@
3229

3330
$params = explode(' ', $line);
3431
$method = array_shift($params);
35-
$promise = call_user_func_array(array($client, $method), $params);
32+
$promise = call_user_func_array(array($redis, $method), $params);
3633

3734
// special method such as end() / close() called
38-
if (!$promise instanceof PromiseInterface) {
35+
if (!$promise instanceof React\Promise\PromiseInterface) {
3936
return;
4037
}
4138

@@ -46,7 +43,7 @@
4643
});
4744
});
4845

49-
$client->on('close', function() {
46+
$redis->on('close', function() {
5047
echo '## DISCONNECTED' . PHP_EOL;
5148

5249
Loop::removeReadStream(STDIN);

examples/incr.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
// $ php examples/incr.php
44
// $ REDIS_URI=localhost:6379 php examples/incr.php
55

6-
use Clue\React\Redis\Factory;
7-
86
require __DIR__ . '/../vendor/autoload.php';
97

10-
$factory = new Factory();
11-
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
8+
$factory = new Clue\React\Redis\Factory();
9+
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
1210

13-
$client->incr('test');
11+
$redis->incr('test');
1412

15-
$client->get('test')->then(function ($result) {
13+
$redis->get('test')->then(function ($result) {
1614
var_dump($result);
1715
}, function (Exception $e) {
1816
echo 'Error: ' . $e->getMessage() . PHP_EOL;
1917
exit(1);
2018
});
2119

22-
//$client->end();
20+
//$redis->end();

examples/publish.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
// $ php examples/publish.php
44
// $ REDIS_URI=localhost:6379 php examples/publish.php channel message
55

6-
use Clue\React\Redis\Factory;
7-
86
require __DIR__ . '/../vendor/autoload.php';
97

10-
$factory = new Factory();
11-
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
8+
$factory = new Clue\React\Redis\Factory();
9+
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
1210

1311
$channel = isset($argv[1]) ? $argv[1] : 'channel';
1412
$message = isset($argv[2]) ? $argv[2] : 'message';
1513

16-
$client->publish($channel, $message)->then(function ($received) {
14+
$redis->publish($channel, $message)->then(function ($received) {
1715
echo 'Successfully published. Received by ' . $received . PHP_EOL;
1816
}, function (Exception $e) {
1917
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;
2018
exit(1);
2119
});
2220

23-
$client->end();
21+
$redis->end();

examples/subscribe.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@
33
// $ php examples/subscribe.php
44
// $ REDIS_URI=localhost:6379 php examples/subscribe.php channel
55

6-
use Clue\React\Redis\Factory;
76
use React\EventLoop\Loop;
87

98
require __DIR__ . '/../vendor/autoload.php';
109

11-
$factory = new Factory();
12-
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
10+
$factory = new Clue\React\Redis\Factory();
11+
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
1312

1413
$channel = isset($argv[1]) ? $argv[1] : 'channel';
1514

16-
$client->subscribe($channel)->then(function () {
15+
$redis->subscribe($channel)->then(function () {
1716
echo 'Now subscribed to channel ' . PHP_EOL;
18-
}, function (Exception $e) use ($client) {
19-
$client->close();
17+
}, function (Exception $e) use ($redis) {
18+
$redis->close();
2019
echo 'Unable to subscribe: ' . $e->getMessage() . PHP_EOL;
2120
});
2221

23-
$client->on('message', function ($channel, $message) {
22+
$redis->on('message', function ($channel, $message) {
2423
echo 'Message on ' . $channel . ': ' . $message . PHP_EOL;
2524
});
2625

2726
// automatically re-subscribe to channel on connection issues
28-
$client->on('unsubscribe', function ($channel) use ($client) {
27+
$redis->on('unsubscribe', function ($channel) use ($redis) {
2928
echo 'Unsubscribed from ' . $channel . PHP_EOL;
3029

31-
Loop::addPeriodicTimer(2.0, function ($timer) use ($client, $channel){
32-
$client->subscribe($channel)->then(function () use ($timer) {
30+
Loop::addPeriodicTimer(2.0, function ($timer) use ($redis, $channel){
31+
$redis->subscribe($channel)->then(function () use ($timer) {
3332
echo 'Now subscribed again' . PHP_EOL;
3433
Loop::cancelTimer($timer);
3534
}, function (Exception $e) {

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
interface Client extends EventEmitterInterface
2323
{
2424
/**
25-
* Invoke the given command and return a Promise that will be resolved when the request has been replied to
25+
* Invoke the given command and return a Promise that will be fulfilled when the request has been replied to
2626
*
2727
* This is a magic method that will be invoked when calling any redis
2828
* command on this instance.

src/Factory.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ public function createClient($uri)
103103
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null);
104104
if (isset($args['password']) || isset($parts['pass'])) {
105105
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']);
106-
$promise = $promise->then(function (StreamingClient $client) use ($pass, $uri) {
107-
return $client->auth($pass)->then(
108-
function () use ($client) {
109-
return $client;
106+
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) {
107+
return $redis->auth($pass)->then(
108+
function () use ($redis) {
109+
return $redis;
110110
},
111-
function (\Exception $e) use ($client, $uri) {
112-
$client->close();
111+
function (\Exception $e) use ($redis, $uri) {
112+
$redis->close();
113113

114114
$const = '';
115115
$errno = $e->getCode();
@@ -131,13 +131,13 @@ function (\Exception $e) use ($client, $uri) {
131131
// use `?db=1` query or `/1` path (skip first slash)
132132
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
133133
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1);
134-
$promise = $promise->then(function (StreamingClient $client) use ($db, $uri) {
135-
return $client->select($db)->then(
136-
function () use ($client) {
137-
return $client;
134+
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) {
135+
return $redis->select($db)->then(
136+
function () use ($redis) {
137+
return $redis;
138138
},
139-
function (\Exception $e) use ($client, $uri) {
140-
$client->close();
139+
function (\Exception $e) use ($redis, $uri) {
140+
$redis->close();
141141

142142
$const = '';
143143
$errno = $e->getCode();

src/LazyClient.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ private function client()
5353
$subscribed =& $this->subscribed;
5454
$psubscribed =& $this->psubscribed;
5555
$loop = $this->loop;
56-
return $pending = $this->factory->createClient($this->target)->then(function (Client $client) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
56+
return $pending = $this->factory->createClient($this->target)->then(function (Client $redis) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
5757
// connection completed => remember only until closed
58-
$client->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
58+
$redis->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
5959
$pending = null;
6060

6161
// foward unsubscribe/punsubscribe events when underlying connection closes
@@ -77,21 +77,21 @@ private function client()
7777
});
7878

7979
// keep track of all channels and patterns this connection is subscribed to
80-
$client->on('subscribe', function ($channel) use (&$subscribed) {
80+
$redis->on('subscribe', function ($channel) use (&$subscribed) {
8181
$subscribed[$channel] = true;
8282
});
83-
$client->on('psubscribe', function ($pattern) use (&$psubscribed) {
83+
$redis->on('psubscribe', function ($pattern) use (&$psubscribed) {
8484
$psubscribed[$pattern] = true;
8585
});
86-
$client->on('unsubscribe', function ($channel) use (&$subscribed) {
86+
$redis->on('unsubscribe', function ($channel) use (&$subscribed) {
8787
unset($subscribed[$channel]);
8888
});
89-
$client->on('punsubscribe', function ($pattern) use (&$psubscribed) {
89+
$redis->on('punsubscribe', function ($pattern) use (&$psubscribed) {
9090
unset($psubscribed[$pattern]);
9191
});
9292

9393
Util::forwardEvents(
94-
$client,
94+
$redis,
9595
$self,
9696
array(
9797
'message',
@@ -103,7 +103,7 @@ private function client()
103103
)
104104
);
105105

106-
return $client;
106+
return $redis;
107107
}, function (\Exception $e) use (&$pending) {
108108
// connection failed => discard connection attempt
109109
$pending = null;
@@ -122,9 +122,9 @@ public function __call($name, $args)
122122
}
123123

124124
$that = $this;
125-
return $this->client()->then(function (Client $client) use ($name, $args, $that) {
125+
return $this->client()->then(function (Client $redis) use ($name, $args, $that) {
126126
$that->awake();
127-
return \call_user_func_array(array($client, $name), $args)->then(
127+
return \call_user_func_array(array($redis, $name), $args)->then(
128128
function ($result) use ($that) {
129129
$that->idle();
130130
return $result;
@@ -148,11 +148,11 @@ public function end()
148148
}
149149

150150
$that = $this;
151-
return $this->client()->then(function (Client $client) use ($that) {
152-
$client->on('close', function () use ($that) {
151+
return $this->client()->then(function (Client $redis) use ($that) {
152+
$redis->on('close', function () use ($that) {
153153
$that->close();
154154
});
155-
$client->end();
155+
$redis->end();
156156
});
157157
}
158158

@@ -166,8 +166,8 @@ public function close()
166166

167167
// either close active connection or cancel pending connection attempt
168168
if ($this->promise !== null) {
169-
$this->promise->then(function (Client $client) {
170-
$client->close();
169+
$this->promise->then(function (Client $redis) {
170+
$redis->close();
171171
});
172172
if ($this->promise !== null) {
173173
$this->promise->cancel();
@@ -208,8 +208,8 @@ public function idle()
208208
$idleTimer =& $this->idleTimer;
209209
$promise =& $this->promise;
210210
$idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) {
211-
$promise->then(function (Client $client) {
212-
$client->close();
211+
$promise->then(function (Client $redis) {
212+
$redis->close();
213213
});
214214
$promise = null;
215215
$idleTimer = null;

tests/FactoryLazyClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function testWillResolveIfConnectorResolves()
5050
$stream->expects($this->never())->method('write');
5151

5252
$this->connector->expects($this->never())->method('connect')->willReturn(Promise\resolve($stream));
53-
$client = $this->factory->createLazyClient('localhost');
53+
$redis = $this->factory->createLazyClient('localhost');
5454

55-
$this->assertInstanceOf('Clue\React\Redis\Client', $client);
55+
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
5656
}
5757

5858
public function testWillWriteSelectCommandIfTargetContainsPath()
@@ -148,15 +148,15 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter
148148
public function testWillRejectIfConnectorRejects()
149149
{
150150
$this->connector->expects($this->never())->method('connect')->with('127.0.0.1:2')->willReturn(Promise\reject(new \RuntimeException()));
151-
$client = $this->factory->createLazyClient('redis://127.0.0.1:2');
151+
$redis = $this->factory->createLazyClient('redis://127.0.0.1:2');
152152

153-
$this->assertInstanceOf('Clue\React\Redis\Client', $client);
153+
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
154154
}
155155

156156
public function testWillRejectIfTargetIsInvalid()
157157
{
158-
$client = $this->factory->createLazyClient('http://invalid target');
158+
$redis = $this->factory->createLazyClient('http://invalid target');
159159

160-
$this->assertInstanceOf('Clue\React\Redis\Client', $client);
160+
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
161161
}
162162
}

0 commit comments

Comments
 (0)