Skip to content

Improve documentation structure and simplify example code #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
398 changes: 227 additions & 171 deletions README.md

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions examples/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
// $ php examples/cli.php
// $ REDIS_URI=localhost:6379 php examples/cli.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();
$factory = new Clue\React\Redis\Factory();

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

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

Loop::addReadStream(STDIN, function () use ($client) {
Loop::addReadStream(STDIN, function () use ($redis) {
$line = fgets(STDIN);
if ($line === false || $line === '') {
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
Loop::removeReadStream(STDIN);
return $client->end();
return $redis->end();
}

$line = rtrim($line);
Expand All @@ -32,10 +29,10 @@

$params = explode(' ', $line);
$method = array_shift($params);
$promise = call_user_func_array(array($client, $method), $params);
$promise = call_user_func_array(array($redis, $method), $params);

// special method such as end() / close() called
if (!$promise instanceof PromiseInterface) {
if (!$promise instanceof React\Promise\PromiseInterface) {
return;
}

Expand All @@ -46,7 +43,7 @@
});
});

$client->on('close', function() {
$redis->on('close', function() {
echo '## DISCONNECTED' . PHP_EOL;

Loop::removeReadStream(STDIN);
Expand Down
12 changes: 5 additions & 7 deletions examples/incr.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
// $ php examples/incr.php
// $ REDIS_URI=localhost:6379 php examples/incr.php

use Clue\React\Redis\Factory;

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

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

$client->incr('test');
$redis->incr('test');

$client->get('test')->then(function ($result) {
$redis->get('test')->then(function ($result) {
var_dump($result);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
exit(1);
});

//$client->end();
//$redis->end();
10 changes: 4 additions & 6 deletions examples/publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
// $ php examples/publish.php
// $ REDIS_URI=localhost:6379 php examples/publish.php channel message

use Clue\React\Redis\Factory;

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

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

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

$client->publish($channel, $message)->then(function ($received) {
$redis->publish($channel, $message)->then(function ($received) {
echo 'Successfully published. Received by ' . $received . PHP_EOL;
}, function (Exception $e) {
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;
exit(1);
});

$client->end();
$redis->end();
19 changes: 9 additions & 10 deletions examples/subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
// $ php examples/subscribe.php
// $ REDIS_URI=localhost:6379 php examples/subscribe.php channel

use Clue\React\Redis\Factory;
use React\EventLoop\Loop;

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

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

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

$client->subscribe($channel)->then(function () {
$redis->subscribe($channel)->then(function () {
echo 'Now subscribed to channel ' . PHP_EOL;
}, function (Exception $e) use ($client) {
$client->close();
}, function (Exception $e) use ($redis) {
$redis->close();
echo 'Unable to subscribe: ' . $e->getMessage() . PHP_EOL;
});

$client->on('message', function ($channel, $message) {
$redis->on('message', function ($channel, $message) {
echo 'Message on ' . $channel . ': ' . $message . PHP_EOL;
});

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

Loop::addPeriodicTimer(2.0, function ($timer) use ($client, $channel){
$client->subscribe($channel)->then(function () use ($timer) {
Loop::addPeriodicTimer(2.0, function ($timer) use ($redis, $channel){
$redis->subscribe($channel)->then(function () use ($timer) {
echo 'Now subscribed again' . PHP_EOL;
Loop::cancelTimer($timer);
}, function (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
interface Client extends EventEmitterInterface
{
/**
* Invoke the given command and return a Promise that will be resolved when the request has been replied to
* Invoke the given command and return a Promise that will be fulfilled when the request has been replied to
*
* This is a magic method that will be invoked when calling any redis
* command on this instance.
Expand Down
24 changes: 12 additions & 12 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public function createClient($uri)
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null);
if (isset($args['password']) || isset($parts['pass'])) {
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']);
$promise = $promise->then(function (StreamingClient $client) use ($pass, $uri) {
return $client->auth($pass)->then(
function () use ($client) {
return $client;
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) {
return $redis->auth($pass)->then(
function () use ($redis) {
return $redis;
},
function (\Exception $e) use ($client, $uri) {
$client->close();
function (\Exception $e) use ($redis, $uri) {
$redis->close();

$const = '';
$errno = $e->getCode();
Expand All @@ -131,13 +131,13 @@ function (\Exception $e) use ($client, $uri) {
// use `?db=1` query or `/1` path (skip first slash)
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1);
$promise = $promise->then(function (StreamingClient $client) use ($db, $uri) {
return $client->select($db)->then(
function () use ($client) {
return $client;
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) {
return $redis->select($db)->then(
function () use ($redis) {
return $redis;
},
function (\Exception $e) use ($client, $uri) {
$client->close();
function (\Exception $e) use ($redis, $uri) {
$redis->close();

$const = '';
$errno = $e->getCode();
Expand Down
34 changes: 17 additions & 17 deletions src/LazyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ private function client()
$subscribed =& $this->subscribed;
$psubscribed =& $this->psubscribed;
$loop = $this->loop;
return $pending = $this->factory->createClient($this->target)->then(function (Client $client) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
return $pending = $this->factory->createClient($this->target)->then(function (Client $redis) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
// connection completed => remember only until closed
$client->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
$redis->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
$pending = null;

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

// keep track of all channels and patterns this connection is subscribed to
$client->on('subscribe', function ($channel) use (&$subscribed) {
$redis->on('subscribe', function ($channel) use (&$subscribed) {
$subscribed[$channel] = true;
});
$client->on('psubscribe', function ($pattern) use (&$psubscribed) {
$redis->on('psubscribe', function ($pattern) use (&$psubscribed) {
$psubscribed[$pattern] = true;
});
$client->on('unsubscribe', function ($channel) use (&$subscribed) {
$redis->on('unsubscribe', function ($channel) use (&$subscribed) {
unset($subscribed[$channel]);
});
$client->on('punsubscribe', function ($pattern) use (&$psubscribed) {
$redis->on('punsubscribe', function ($pattern) use (&$psubscribed) {
unset($psubscribed[$pattern]);
});

Util::forwardEvents(
$client,
$redis,
$self,
array(
'message',
Expand All @@ -103,7 +103,7 @@ private function client()
)
);

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

$that = $this;
return $this->client()->then(function (Client $client) use ($name, $args, $that) {
return $this->client()->then(function (Client $redis) use ($name, $args, $that) {
$that->awake();
return \call_user_func_array(array($client, $name), $args)->then(
return \call_user_func_array(array($redis, $name), $args)->then(
function ($result) use ($that) {
$that->idle();
return $result;
Expand All @@ -148,11 +148,11 @@ public function end()
}

$that = $this;
return $this->client()->then(function (Client $client) use ($that) {
$client->on('close', function () use ($that) {
return $this->client()->then(function (Client $redis) use ($that) {
$redis->on('close', function () use ($that) {
$that->close();
});
$client->end();
$redis->end();
});
}

Expand All @@ -166,8 +166,8 @@ public function close()

// either close active connection or cancel pending connection attempt
if ($this->promise !== null) {
$this->promise->then(function (Client $client) {
$client->close();
$this->promise->then(function (Client $redis) {
$redis->close();
});
if ($this->promise !== null) {
$this->promise->cancel();
Expand Down Expand Up @@ -208,8 +208,8 @@ public function idle()
$idleTimer =& $this->idleTimer;
$promise =& $this->promise;
$idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) {
$promise->then(function (Client $client) {
$client->close();
$promise->then(function (Client $redis) {
$redis->close();
});
$promise = null;
$idleTimer = null;
Expand Down
12 changes: 6 additions & 6 deletions tests/FactoryLazyClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function testWillResolveIfConnectorResolves()
$stream->expects($this->never())->method('write');

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

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

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

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

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

$this->assertInstanceOf('Clue\React\Redis\Client', $client);
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
}
}
Loading