|
3 | 3 | namespace Clue\React\Redis; |
4 | 4 |
|
5 | 5 | use React\Socket\Server as ServerSocket; |
6 | | -use React\Promise\When; |
7 | 6 | use React\SocketClient\ConnectorInterface; |
8 | 7 | use React\Stream\Stream; |
9 | 8 | use Clue\React\Redis\Client; |
@@ -39,23 +38,39 @@ public function createClient($target = null) |
39 | 38 | $db = $this->getDatabaseFromTarget($target); |
40 | 39 | $protocol = $this->protocol; |
41 | 40 |
|
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()); |
58 | 43 | }); |
| 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; |
59 | 74 | } |
60 | 75 |
|
61 | 76 | private function parseUrl($target) |
@@ -89,7 +104,11 @@ private function connect($target) |
89 | 104 | $parts = $this->parseUrl($target); |
90 | 105 | } |
91 | 106 | 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 | + } |
93 | 112 | } |
94 | 113 |
|
95 | 114 | return $this->connector->create($parts['host'], $parts['port']); |
|
0 commit comments