@@ -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'); |
@@ -103,11 +103,11 @@ discard block |
||
| 103 | 103 | $args = [$key, $star, $stop]; |
| 104 | 104 | if ($withScores) { |
| 105 | 105 | $args[] = 'WITHSCORES'; |
| 106 | - return $this->dispatch(Builder::build($command, $args))->then(function ($value) { |
|
| 106 | + return $this->dispatch(Builder::build($command, $args))->then(function($value) { |
|
| 107 | 107 | $len = count($value); |
| 108 | 108 | $ret = []; |
| 109 | - for ($i=0; $i<$len; $i+=2) { |
|
| 110 | - $ret[$value[$i]] = $value[$i+1]; |
|
| 109 | + for ($i = 0; $i < $len; $i += 2) { |
|
| 110 | + $ret[$value[$i]] = $value[$i + 1]; |
|
| 111 | 111 | } |
| 112 | 112 | return $ret; |
| 113 | 113 | }); |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | { |
| 125 | 125 | $command = Enum::ZRANGEBYLEX; |
| 126 | 126 | $args = [$key, $min, $max]; |
| 127 | - $args = array_merge($args,$options); |
|
| 127 | + $args = array_merge($args, $options); |
|
| 128 | 128 | |
| 129 | 129 | return $this->dispatch(Builder::build($command, $args)); |
| 130 | 130 | } |
@@ -136,8 +136,8 @@ discard block |
||
| 136 | 136 | public function zRevRangeByLex($key, $max, $min, array $options = []) |
| 137 | 137 | { |
| 138 | 138 | $command = Enum::ZREVRANGEBYLEX; |
| 139 | - $args = [$key, $max,$min]; |
|
| 140 | - $args = array_merge($args,$options); |
|
| 139 | + $args = [$key, $max, $min]; |
|
| 140 | + $args = array_merge($args, $options); |
|
| 141 | 141 | |
| 142 | 142 | return $this->dispatch(Builder::build($command, $args)); |
| 143 | 143 | } |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | public function zRangeByScore($key, $min, $max, array $options = []) |
| 150 | 150 | { |
| 151 | 151 | $command = Enum::ZRANGEBYSCORE; |
| 152 | - $args = [$key, $min,$max]; |
|
| 152 | + $args = [$key, $min, $max]; |
|
| 153 | 153 | $args = array_merge($args, $options); |
| 154 | 154 | |
| 155 | 155 | return $this->dispatch(Builder::build($command, $args)); |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | public function zRank($key, $member) |
| 163 | 163 | { |
| 164 | 164 | $command = Enum::ZRANK; |
| 165 | - $args = [$key,$member]; |
|
| 165 | + $args = [$key, $member]; |
|
| 166 | 166 | |
| 167 | 167 | return $this->dispatch(Builder::build($command, $args)); |
| 168 | 168 | } |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | public function zRemRangeByRank($key, $start, $stop) |
| 201 | 201 | { |
| 202 | 202 | $command = Enum::ZREMRANGEBYRANK; |
| 203 | - $args = [$key, $start,$stop]; |
|
| 203 | + $args = [$key, $start, $stop]; |
|
| 204 | 204 | |
| 205 | 205 | return $this->dispatch(Builder::build($command, $args)); |
| 206 | 206 | } |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | public function zRevRangeByScore($key, $max, $min, array $options = []) |
| 239 | 239 | { |
| 240 | 240 | $command = Enum::ZREVRANGEBYSCORE; |
| 241 | - $args = [$key,$max,$min]; |
|
| 241 | + $args = [$key, $max, $min]; |
|
| 242 | 242 | $args = array_merge($args, $options); |
| 243 | 243 | |
| 244 | 244 | return $this->dispatch(Builder::build($command, $args)); |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | public function zRevRank($key, $member) |
| 252 | 252 | { |
| 253 | 253 | $command = Enum::ZREVRANK; |
| 254 | - $args = [$key,$member]; |
|
| 254 | + $args = [$key, $member]; |
|
| 255 | 255 | |
| 256 | 256 | return $this->dispatch(Builder::build($command, $args)); |
| 257 | 257 | } |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | public function zScore($key, $member) |
| 264 | 264 | { |
| 265 | 265 | $command = Enum::ZSCORE; |
| 266 | - $args = [$key,$member]; |
|
| 266 | + $args = [$key, $member]; |
|
| 267 | 267 | |
| 268 | 268 | return $this->dispatch(Builder::build($command, $args)); |
| 269 | 269 | } |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | { |
| 277 | 277 | // TODO: Implement zScan() method. |
| 278 | 278 | $command = Enum::ZSCAN; |
| 279 | - $args = [$key , $cursor]; |
|
| 279 | + $args = [$key, $cursor]; |
|
| 280 | 280 | $args = array_merge($args, $options); |
| 281 | 281 | |
| 282 | 282 | return $this->dispatch(Builder::build($command, $args)); |
@@ -22,8 +22,10 @@ discard block |
||
| 22 | 22 | { |
| 23 | 23 | $command = Enum::ZADD; |
| 24 | 24 | $args = array_merge([$key], $options); |
| 25 | - if (!empty($scoreMembers)) { |
|
| 26 | - foreach ($scoreMembers as $score => $member) { |
|
| 25 | + if (!empty($scoreMembers)) |
|
| 26 | + { |
|
| 27 | + foreach ($scoreMembers as $score => $member) |
|
| 28 | + { |
|
| 27 | 29 | $args[] = (float) $score; |
| 28 | 30 | $args[] = $member; |
| 29 | 31 | } |
@@ -101,12 +103,14 @@ discard block |
||
| 101 | 103 | { |
| 102 | 104 | $command = Enum::ZRANGE; |
| 103 | 105 | $args = [$key, $star, $stop]; |
| 104 | - if ($withScores) { |
|
| 106 | + if ($withScores) |
|
| 107 | + { |
|
| 105 | 108 | $args[] = 'WITHSCORES'; |
| 106 | 109 | return $this->dispatch(Builder::build($command, $args))->then(function ($value) { |
| 107 | 110 | $len = count($value); |
| 108 | 111 | $ret = []; |
| 109 | - for ($i=0; $i<$len; $i+=2) { |
|
| 112 | + for ($i=0; $i<$len; $i+=2) |
|
| 113 | + { |
|
| 110 | 114 | $ret[$value[$i]] = $value[$i+1]; |
| 111 | 115 | } |
| 112 | 116 | return $ret; |