Skip to content

Commit 910a486

Browse files
committed
Support React PHP v0.4 and react/promise v2 (fixes clue#6)
This does not break BC and keeps compatibility with React v0.3
1 parent d8ff573 commit 910a486

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
}
1212
],
1313
"require": {
14-
"react/promise": "~1",
15-
"react/socket-client": "0.3.*",
16-
"react/event-loop": "0.3.*",
14+
"react/promise": "1.*|2.*",
15+
"react/socket-client": "0.3.*|0.4.*",
16+
"react/event-loop": "0.3.*|0.4.*",
1717
"clue/redis-protocol": "0.3.*"
1818
},
1919
"autoload": {

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Clue\Redis\Protocol\Serializer\SerializerInterface;
1111
use Clue\Redis\Protocol\Factory as ProtocolFactory;
1212
use Clue\React\Redis\Request;
13-
use React\Promise\When;
1413
use UnderflowException;
1514
use RuntimeException;
1615

@@ -69,7 +68,13 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
6968
public function __call($name, $args)
7069
{
7170
if ($this->ending) {
72-
return When::reject(new RuntimeException('Connection closed'));
71+
$e = new RuntimeException('Connection closed');
72+
73+
if (class_exists('React\Promise\When')) {
74+
return \React\Promise\When::reject($e);
75+
} else {
76+
return \React\Promise\reject($e);
77+
}
7378
}
7479

7580
$this->stream->write($this->serializer->getRequestMessage($name, $args));

src/Factory.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Clue\React\Redis;
44

55
use React\Socket\Server as ServerSocket;
6-
use React\Promise\When;
76
use React\SocketClient\ConnectorInterface;
87
use React\Stream\Stream;
98
use Clue\React\Redis\Client;
@@ -39,23 +38,39 @@ public function createClient($target = null)
3938
$db = $this->getDatabaseFromTarget($target);
4039
$protocol = $this->protocol;
4140

42-
return $this->connect($target)->then(function (Stream $stream) use ($auth, $db, $protocol) {
43-
$client = new Client($stream, $protocol->createResponseParser(), $protocol->createSerializer());
44-
45-
return When::all(
46-
array(
47-
($auth !== null ? $client->auth($auth) : null),
48-
($db !== null ? $client->select($db) : null)
49-
),
50-
function() use ($client) {
51-
return $client;
52-
},
53-
function($error) use ($client) {
54-
$client->close();
55-
throw $error;
56-
}
57-
);
41+
$promise = $this->connect($target)->then(function (Stream $stream) use ($protocol) {
42+
return new Client($stream, $protocol->createResponseParser(), $protocol->createSerializer());
5843
});
44+
45+
if ($auth !== null) {
46+
$promise = $promise->then(function (Client $client) use ($auth) {
47+
return $client->auth($auth)->then(
48+
function () use ($client) {
49+
return $client;
50+
},
51+
function ($error) use ($client) {
52+
$client->close();
53+
throw $error;
54+
}
55+
);
56+
});
57+
}
58+
59+
if ($db !== null) {
60+
$promise = $promise->then(function (Client $client) use ($db) {
61+
return $client->select($db)->then(
62+
function () use ($client) {
63+
return $client;
64+
},
65+
function ($error) use ($client) {
66+
$client->close();
67+
throw $error;
68+
}
69+
);
70+
});
71+
}
72+
73+
return $promise;
5974
}
6075

6176
private function parseUrl($target)
@@ -89,7 +104,11 @@ private function connect($target)
89104
$parts = $this->parseUrl($target);
90105
}
91106
catch (Exception $e) {
92-
return When::reject($e);
107+
if (class_exists('React\Promise\When')) {
108+
return \React\Promise\When::reject($e);
109+
} else {
110+
return \React\Promise\reject($e);
111+
}
93112
}
94113

95114
return $this->connector->create($parts['host'], $parts['port']);

0 commit comments

Comments
 (0)