@@ -54,7 +54,7 @@ |
||
| 54 | 54 | $this->host = $parts['host']; |
| 55 | 55 | $this->port = $parts['port']; |
| 56 | 56 | $this->auth = $auth; |
| 57 | - $this->db = $parts['db']; |
|
| 57 | + $this->db = $parts['db']; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | public function getHost() |
@@ -8,44 +8,53 @@ |
||
| 8 | 8 | { |
| 9 | 9 | public function __construct($target) |
| 10 | 10 | { |
| 11 | - if ($target === null) { |
|
| 11 | + if ($target === null) |
|
| 12 | + { |
|
| 12 | 13 | $target = 'tcp://127.0.0.1'; |
| 13 | 14 | } |
| 14 | 15 | |
| 15 | - if (strpos($target, '://') === false) { |
|
| 16 | + if (strpos($target, '://') === false) |
|
| 17 | + { |
|
| 16 | 18 | $target = 'tcp://' . $target; |
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | $parts = parse_url($target); |
| 20 | - if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') { |
|
| 22 | + if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') |
|
| 23 | + { |
|
| 21 | 24 | throw new InvalidArgumentException('Given URL can not be parsed'); |
| 22 | 25 | } |
| 23 | 26 | |
| 24 | - if (!isset($parts['port'])) { |
|
| 27 | + if (!isset($parts['port'])) |
|
| 28 | + { |
|
| 25 | 29 | $parts['port'] = 6379; |
| 26 | 30 | $this->port = 6379; |
| 27 | 31 | } |
| 28 | 32 | |
| 29 | - if ($parts['host'] === 'localhost') { |
|
| 33 | + if ($parts['host'] === 'localhost') |
|
| 34 | + { |
|
| 30 | 35 | $parts['host'] = '127.0.0.1'; |
| 31 | 36 | $this->host = '127.0.0.1'; |
| 32 | 37 | } |
| 33 | 38 | |
| 34 | 39 | $auth = null; |
| 35 | - if (isset($parts['user'])) { |
|
| 40 | + if (isset($parts['user'])) |
|
| 41 | + { |
|
| 36 | 42 | $auth = $parts['user']; |
| 37 | 43 | $this->auth = $auth; |
| 38 | 44 | } |
| 39 | 45 | |
| 40 | - if (isset($parts['pass'])) { |
|
| 46 | + if (isset($parts['pass'])) |
|
| 47 | + { |
|
| 41 | 48 | $auth .= ':' . $parts['pass']; |
| 42 | 49 | } |
| 43 | 50 | |
| 44 | - if ($auth !== null) { |
|
| 51 | + if ($auth !== null) |
|
| 52 | + { |
|
| 45 | 53 | $parts['auth'] = $auth; |
| 46 | 54 | } |
| 47 | 55 | |
| 48 | - if (isset($parts['path']) && $parts['path'] !== '') { |
|
| 56 | + if (isset($parts['path']) && $parts['path'] !== '') |
|
| 57 | + { |
|
| 49 | 58 | $parts['db'] = substr($parts['path'], 1); |
| 50 | 59 | } |
| 51 | 60 | |
@@ -71,7 +71,7 @@ |
||
| 71 | 71 | { |
| 72 | 72 | $payload = $this->driver->commands($command); |
| 73 | 73 | |
| 74 | - $this->dispatcher->on('request', function () use ($payload) { |
|
| 74 | + $this->dispatcher->on('request', function() use ($payload) { |
|
| 75 | 75 | $this->dispatcher->handleRequest($payload); |
| 76 | 76 | }); |
| 77 | 77 | |
@@ -66,8 +66,8 @@ |
||
| 66 | 66 | if ($this->dispatcher->isEnding()) |
| 67 | 67 | { |
| 68 | 68 | $request->reject(new RuntimeException('Connection closed')); |
| 69 | - } |
|
| 70 | - else |
|
| 69 | + } |
|
| 70 | + else |
|
| 71 | 71 | { |
| 72 | 72 | $payload = $this->driver->commands($command); |
| 73 | 73 | |
@@ -82,13 +82,13 @@ |
||
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Create socket client with connection to Redis database. |
|
| 87 | - * |
|
| 88 | - * @param string $endpoint |
|
| 89 | - * @return SocketInterface |
|
| 90 | - * @throws ExecutionException |
|
| 91 | - */ |
|
| 85 | + /** |
|
| 86 | + * Create socket client with connection to Redis database. |
|
| 87 | + * |
|
| 88 | + * @param string $endpoint |
|
| 89 | + * @return SocketInterface |
|
| 90 | + * @throws ExecutionException |
|
| 91 | + */ |
|
| 92 | 92 | protected function createClient($endpoint) |
| 93 | 93 | { |
| 94 | 94 | $ex = null; |
@@ -62,8 +62,8 @@ discard block |
||
| 62 | 62 | $this->closed = false; |
| 63 | 63 | $this->protocol = new Driver(); |
| 64 | 64 | $this->on('connect', [$this, 'handleConnect']); |
| 65 | - $this->on('response',[$this, 'handleResponse']); |
|
| 66 | - $this->on('disconnect',[$this, 'handleDisconnect']); |
|
| 65 | + $this->on('response', [$this, 'handleResponse']); |
|
| 66 | + $this->on('disconnect', [$this, 'handleDisconnect']); |
|
| 67 | 67 | $this->on('close', [$this, 'handleClose']); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public function isEnding() |
| 122 | 122 | { |
| 123 | - return $this->ending? true: false; |
|
| 123 | + return $this->ending ? true : false; |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | { |
| 228 | 228 | $this->ending = true; |
| 229 | 229 | // reject all remaining requests in the queue |
| 230 | - while($this->reqs) { |
|
| 230 | + while ($this->reqs) { |
|
| 231 | 231 | $req = array_shift($this->reqs); |
| 232 | 232 | /* @var $req Deferred */ |
| 233 | 233 | $req->reject(new RuntimeException('Connection closing')); |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | { |
| 77 | 77 | $this->handleDisconnect(); |
| 78 | 78 | } |
| 79 | - else |
|
| 79 | + else |
|
| 80 | 80 | { |
| 81 | 81 | $this->handleClose(); |
| 82 | 82 | } |
@@ -98,9 +98,11 @@ discard block |
||
| 98 | 98 | return new Socket($endpoint, $this->loop); |
| 99 | 99 | } |
| 100 | 100 | catch (Error $ex) |
| 101 | - {} |
|
| 101 | + { |
|
| 102 | +} |
|
| 102 | 103 | catch (Exception $ex) |
| 103 | - {} |
|
| 104 | + { |
|
| 105 | +} |
|
| 104 | 106 | |
| 105 | 107 | throw new ExecutionException('Redis connection socket could not be created!', 0, $ex); |
| 106 | 108 | } |
@@ -138,17 +140,22 @@ discard block |
||
| 138 | 140 | */ |
| 139 | 141 | public function watch($endpoint) |
| 140 | 142 | { |
| 141 | - if ($this->stream !== null) { |
|
| 143 | + if ($this->stream !== null) |
|
| 144 | + { |
|
| 142 | 145 | return; |
| 143 | 146 | } |
| 144 | 147 | |
| 145 | - try { |
|
| 148 | + try |
|
| 149 | + { |
|
| 146 | 150 | $this->stream = $this->createClient($endpoint); |
| 147 | - } catch (\Exception $e) { |
|
| 151 | + } |
|
| 152 | + catch (\Exception $e) |
|
| 153 | + { |
|
| 148 | 154 | $this->emit('error', [$e]); |
| 149 | 155 | } |
| 150 | 156 | |
| 151 | - if ($this->stream->isOpen()) { |
|
| 157 | + if ($this->stream->isOpen()) |
|
| 158 | + { |
|
| 152 | 159 | $this->emit('connect', [$this]); |
| 153 | 160 | } |
| 154 | 161 | |
@@ -170,18 +177,25 @@ discard block |
||
| 170 | 177 | */ |
| 171 | 178 | public function handleData($_, $data) |
| 172 | 179 | { |
| 173 | - try { |
|
| 180 | + try |
|
| 181 | + { |
|
| 174 | 182 | $models = $this->protocol->parseResponse($data); |
| 175 | - } catch (ParserException $error) { |
|
| 183 | + } |
|
| 184 | + catch (ParserException $error) |
|
| 185 | + { |
|
| 176 | 186 | $this->emit('error', [$error]); |
| 177 | 187 | |
| 178 | 188 | return; |
| 179 | 189 | } |
| 180 | 190 | |
| 181 | - foreach ($models as $data) { |
|
| 182 | - try { |
|
| 191 | + foreach ($models as $data) |
|
| 192 | + { |
|
| 193 | + try |
|
| 194 | + { |
|
| 183 | 195 | $this->emit('response', [$data]); |
| 184 | - } catch (UnderflowException $error) { |
|
| 196 | + } |
|
| 197 | + catch (UnderflowException $error) |
|
| 198 | + { |
|
| 185 | 199 | $this->emit('error', [$error]); |
| 186 | 200 | |
| 187 | 201 | break; |
@@ -203,19 +217,24 @@ discard block |
||
| 203 | 217 | */ |
| 204 | 218 | public function handleResponse(ModelInterface $message) |
| 205 | 219 | { |
| 206 | - if (!$this->reqs) { |
|
| 220 | + if (!$this->reqs) |
|
| 221 | + { |
|
| 207 | 222 | throw new UnderflowException('Unexpected reply received, no matching request found'); |
| 208 | 223 | } |
| 209 | 224 | /* @var Deferred $req */ |
| 210 | 225 | $req = array_shift($this->reqs); |
| 211 | 226 | |
| 212 | - if ($message instanceof ErrorReply) { |
|
| 227 | + if ($message instanceof ErrorReply) |
|
| 228 | + { |
|
| 213 | 229 | $req->reject($message); |
| 214 | - } else { |
|
| 230 | + } |
|
| 231 | + else |
|
| 232 | + { |
|
| 215 | 233 | $req->resolve($message->getValueNative()); |
| 216 | 234 | } |
| 217 | 235 | |
| 218 | - if (count($this->reqs) <= 0) { |
|
| 236 | + if (count($this->reqs) <= 0) |
|
| 237 | + { |
|
| 219 | 238 | $this->emit('disconnect'); |
| 220 | 239 | } |
| 221 | 240 | } |
@@ -227,7 +246,8 @@ discard block |
||
| 227 | 246 | { |
| 228 | 247 | $this->ending = true; |
| 229 | 248 | // reject all remaining requests in the queue |
| 230 | - while($this->reqs) { |
|
| 249 | + while($this->reqs) |
|
| 250 | + { |
|
| 231 | 251 | $req = array_shift($this->reqs); |
| 232 | 252 | /* @var $req Deferred */ |
| 233 | 253 | $req->reject(new RuntimeException('Connection closing')); |
@@ -241,7 +261,8 @@ discard block |
||
| 241 | 261 | */ |
| 242 | 262 | public function handleClose() |
| 243 | 263 | { |
| 244 | - if ($this->closed) { |
|
| 264 | + if ($this->closed) |
|
| 265 | + { |
|
| 245 | 266 | return; |
| 246 | 267 | } |
| 247 | 268 | $this->removeListeners('connect'); |