/** * Send request and retrieve response to the connected disque node. * * @param array $args * @return array|int|null|string * * @throws CommandException * @throws StreamException */ protected function send(array $args = []) { $this->log->debug('send()ing command', $args); $response = RespUtils::deserialize($this->stream->write(RespUtils::serialize($args))); $this->log->debug('response', [$response]); return $response; }
/** * @param StreamInterface $stream * * @return array|int|null|string * * @throws CommandException * @throws \RuntimeException */ public static function deserialize(StreamInterface $stream) { $rsp = $stream->readLine(); list($type, $result) = [$rsp[0], trim(substr($rsp, 1, strlen($rsp)))]; switch ($type) { case '-': // ERRORS throw new CommandException($result); case '+': // SIMPLE STRINGS return $result; case ':': // INTEGERS return (int) $result; case '$': // BULK STRINGS $result = (int) $result; if ($result == -1) { return null; } return trim($stream->readBytes($result + 2)); case '*': // ARRAYS $cnt = (int) $result; $out = []; for ($i = 0; $i < $cnt; $i++) { $out[] = static::deserialize($stream); } return $out; default: throw new \RuntimeException('unhandled protocol response: ' . $rsp); } }
/** * return the internal stream url. * * @return string */ public function getNodeUrl() { return $this->connected->getNodeUrl(); }
protected function tearDown() { if ($this->stream) { $this->stream->close(); } }