@@ -5,4 +5,5 @@ |
||
| 5 | 5 | use Clue\Redis\Protocol\Model\ModelInterface as ClueModelInterface; |
| 6 | 6 | |
| 7 | 7 | interface RequestInterface extends ClueModelInterface |
| 8 | -{} |
|
| 8 | +{ |
|
| 9 | +} |
|
@@ -49,10 +49,12 @@ discard block |
||
| 49 | 49 | $args = [$key]; |
| 50 | 50 | |
| 51 | 51 | return $this->dispatch(Builder::build($command, $args))->then(function ($value) { |
| 52 | - if (!empty($value)) { |
|
| 52 | + if (!empty($value)) |
|
| 53 | + { |
|
| 53 | 54 | $tmp = []; |
| 54 | 55 | $size = count($value); |
| 55 | - for ($i=0; $i<$size; $i+=2) { |
|
| 56 | + for ($i=0; $i<$size; $i+=2) |
|
| 57 | + { |
|
| 56 | 58 | $field = $value[$i]; |
| 57 | 59 | $val = $value[$i+1]; |
| 58 | 60 | $tmp[$field] = $val; |
@@ -133,8 +135,10 @@ discard block |
||
| 133 | 135 | { |
| 134 | 136 | $command = Enum::HMSET; |
| 135 | 137 | $args = [$key]; |
| 136 | - if (!empty($fvMap)) { |
|
| 137 | - foreach ($fvMap as $field => $value) { |
|
| 138 | + if (!empty($fvMap)) |
|
| 139 | + { |
|
| 140 | + foreach ($fvMap as $field => $value) |
|
| 141 | + { |
|
| 138 | 142 | $tmp[] = $field; |
| 139 | 143 | $tmp[] = $value; |
| 140 | 144 | } |
@@ -104,18 +104,22 @@ |
||
| 104 | 104 | $command = Enum::INFO; |
| 105 | 105 | |
| 106 | 106 | return $this->dispatch(Builder::build($command, $section))->then(function ($value) { |
| 107 | - if ($value) { |
|
| 107 | + if ($value) |
|
| 108 | + { |
|
| 108 | 109 | $ret = explode(PHP_EOL, $value); |
| 109 | 110 | $handled = []; |
| 110 | 111 | $lastKey = ''; |
| 111 | - foreach ($ret as $_ => $v) { |
|
| 112 | - if (($pos = strpos($v, '#')) !== false) { |
|
| 112 | + foreach ($ret as $_ => $v) |
|
| 113 | + { |
|
| 114 | + if (($pos = strpos($v, '#')) !== false) |
|
| 115 | + { |
|
| 113 | 116 | $lastKey = strtolower(substr($v,$pos+2)); |
| 114 | 117 | $handled[$lastKey] = []; |
| 115 | 118 | continue; |
| 116 | 119 | } |
| 117 | 120 | $statMap = explode(':', $v); |
| 118 | - if ($statMap[0]) { |
|
| 121 | + if ($statMap[0]) |
|
| 122 | + { |
|
| 119 | 123 | list($name, $stat) = explode(':', $v); |
| 120 | 124 | $handled[$lastKey][$name] = $stat; |
| 121 | 125 | } |
@@ -45,7 +45,8 @@ |
||
| 45 | 45 | public function bitField($key, $subCommand = null, ...$param) |
| 46 | 46 | { |
| 47 | 47 | $command = Enum::BITFIELD; |
| 48 | - switch ($subCommand = strtoupper($subCommand)) { |
|
| 48 | + switch ($subCommand = strtoupper($subCommand)) |
|
| 49 | + { |
|
| 49 | 50 | case 'GET' : { |
| 50 | 51 | list ($type, $offset) = $param; |
| 51 | 52 | $args = [$subCommand, $type, $offset]; |
@@ -26,7 +26,8 @@ discard block |
||
| 26 | 26 | $args = $keys; |
| 27 | 27 | $promise = $this->dispatch(Builder::build($command, $args)); |
| 28 | 28 | $promise = $promise->then(function ($value) { |
| 29 | - if (is_array($value)) { |
|
| 29 | + if (is_array($value)) |
|
| 30 | + { |
|
| 30 | 31 | list($k,$v) = $value; |
| 31 | 32 | |
| 32 | 33 | return [ |
@@ -53,7 +54,8 @@ discard block |
||
| 53 | 54 | $args = $keys; |
| 54 | 55 | $promise = $this->dispatch(Builder::build($command, $args)); |
| 55 | 56 | $promise = $promise->then(function ($value) { |
| 56 | - if (is_array($value)) { |
|
| 57 | + if (is_array($value)) |
|
| 58 | + { |
|
| 57 | 59 | list($k,$v) = $value; |
| 58 | 60 | |
| 59 | 61 | return [ |
@@ -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 | |
@@ -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; |
@@ -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'); |