Skip to content

Commit a753f96

Browse files
committed
Consistently use $redis and fully qualified class names for Client
1 parent 92d3208 commit a753f96

File tree

11 files changed

+261
-269
lines changed

11 files changed

+261
-269
lines changed

README.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ local Redis server and send some requests:
6363

6464
```php
6565
$factory = new Clue\React\Redis\Factory();
66-
$client = $factory->createLazyClient('localhost:6379');
66+
$redis = $factory->createLazyClient('localhost:6379');
6767

68-
$client->set('greeting', 'Hello world');
69-
$client->append('greeting', '!');
68+
$redis->set('greeting', 'Hello world');
69+
$redis->append('greeting', '!');
7070

71-
$client->get('greeting')->then(function ($greeting) {
71+
$redis->get('greeting')->then(function ($greeting) {
7272
// Hello world!
7373
echo $greeting . PHP_EOL;
7474
});
7575

76-
$client->incr('invocation')->then(function ($n) {
76+
$redis->incr('invocation')->then(function ($n) {
7777
echo 'This is invocation #' . $n . PHP_EOL;
7878
});
7979

8080
// end connection once all pending requests have been resolved
81-
$client->end();
81+
$redis->end();
8282
```
8383

8484
See also the [examples](examples).
@@ -91,20 +91,20 @@ Most importantly, this project provides a [`Client`](#client) instance that
9191
can be used to invoke all [Redis commands](https://redis.io/commands) (such as `GET`, `SET`, etc.).
9292

9393
```php
94-
$client->get($key);
95-
$client->set($key, $value);
96-
$client->exists($key);
97-
$client->expire($key, $seconds);
98-
$client->mget($key1, $key2, $key3);
94+
$redis->get($key);
95+
$redis->set($key, $value);
96+
$redis->exists($key);
97+
$redis->expire($key, $seconds);
98+
$redis->mget($key1, $key2, $key3);
9999

100-
$client->multi();
101-
$client->exec();
100+
$redis->multi();
101+
$redis->exec();
102102

103-
$client->publish($channel, $payload);
104-
$client->subscribe($channel);
103+
$redis->publish($channel, $payload);
104+
$redis->subscribe($channel);
105105

106-
$client->ping();
107-
$client->select($database);
106+
$redis->ping();
107+
$redis->select($database);
108108

109109
// many more…
110110
```
@@ -161,17 +161,17 @@ send a message to all clients currently subscribed to a given channel:
161161
```php
162162
$channel = 'user';
163163
$message = json_encode(array('id' => 10));
164-
$client->publish($channel, $message);
164+
$redis->publish($channel, $message);
165165
```
166166

167167
The [`SUBSCRIBE` command](https://redis.io/commands/subscribe) can be used to
168168
subscribe to a channel and then receive incoming PubSub `message` events:
169169

170170
```php
171171
$channel = 'user';
172-
$client->subscribe($channel);
172+
$redis->subscribe($channel);
173173

174-
$client->on('message', function ($channel, $payload) {
174+
$redis->on('message', function ($channel, $payload) {
175175
// pubsub message received on given $channel
176176
var_dump($channel, json_decode($payload));
177177
});
@@ -181,9 +181,9 @@ Likewise, you can use the same client connection to subscribe to multiple
181181
channels by simply executing this command multiple times:
182182

183183
```php
184-
$client->subscribe('user.register');
185-
$client->subscribe('user.join');
186-
$client->subscribe('user.leave');
184+
$redis->subscribe('user.register');
185+
$redis->subscribe('user.join');
186+
$redis->subscribe('user.leave');
187187
```
188188

189189
Similarly, the [`PSUBSCRIBE` command](https://redis.io/commands/psubscribe) can
@@ -193,9 +193,9 @@ all incoming PubSub messages with the `pmessage` event:
193193

194194
```php
195195
$pattern = 'user.*';
196-
$client->psubscribe($pattern);
196+
$redis->psubscribe($pattern);
197197

198-
$client->on('pmessage', function ($pattern, $channel, $payload) {
198+
$redis->on('pmessage', function ($pattern, $channel, $payload) {
199199
// pubsub message received matching given $pattern
200200
var_dump($channel, json_decode($payload));
201201
});
@@ -213,10 +213,10 @@ receiving any further events for the given channel and pattern subscriptions
213213
respectively:
214214

215215
```php
216-
$client->subscribe('user');
216+
$redis->subscribe('user');
217217

218-
Loop::addTimer(60.0, function () use ($client) {
219-
$client->unsubscribe('user');
218+
Loop::addTimer(60.0, function () use ($redis) {
219+
$redis->unsubscribe('user');
220220
});
221221
```
222222

@@ -235,16 +235,16 @@ Additionally, can listen for the following PubSub events to get notifications
235235
about subscribed/unsubscribed channels and patterns:
236236

237237
```php
238-
$client->on('subscribe', function ($channel, $total) {
238+
$redis->on('subscribe', function ($channel, $total) {
239239
// subscribed to given $channel
240240
});
241-
$client->on('psubscribe', function ($pattern, $total) {
241+
$redis->on('psubscribe', function ($pattern, $total) {
242242
// subscribed to matching given $pattern
243243
});
244-
$client->on('unsubscribe', function ($channel, $total) {
244+
$redis->on('unsubscribe', function ($channel, $total) {
245245
// unsubscribed from given $channel
246246
});
247-
$client->on('punsubscribe', function ($pattern, $total) {
247+
$redis->on('punsubscribe', function ($pattern, $total) {
248248
// unsubscribed from matching given $pattern
249249
});
250250
```
@@ -299,7 +299,7 @@ and optionally authenticating (AUTH) and selecting the right database (SELECT).
299299

300300
```php
301301
$factory->createClient('localhost:6379')->then(
302-
function (Client $client) {
302+
function (Client $redis) {
303303
// client connected (and authenticated)
304304
},
305305
function (Exception $e) {
@@ -395,10 +395,10 @@ It helps with establishing a plain TCP/IP or secure TLS connection to Redis
395395
and optionally authenticating (AUTH) and selecting the right database (SELECT).
396396

397397
```php
398-
$client = $factory->createLazyClient('localhost:6379');
398+
$redis = $factory->createLazyClient('localhost:6379');
399399

400-
$client->incr('hello');
401-
$client->end();
400+
$redis->incr('hello');
401+
$redis->end();
402402
```
403403

404404
This method immediately returns a "virtual" connection implementing the
@@ -576,7 +576,7 @@ when the client connection is lost or is invalid.
576576
The event receives a single `Exception` argument for the error instance.
577577

578578
```php
579-
$client->on('error', function (Exception $e) {
579+
$redis->on('error', function (Exception $e) {
580580
echo 'Error: ' . $e->getMessage() . PHP_EOL;
581581
});
582582
```
@@ -590,7 +590,7 @@ errors caused by invalid commands.
590590
The `close` event will be emitted once the client connection closes (terminates).
591591

592592
```php
593-
$client->on('close', function () {
593+
$redis->on('close', function () {
594594
echo 'Connection closed' . PHP_EOL;
595595
});
596596
```

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/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();

0 commit comments

Comments
 (0)