public function read(Predis_Connection $connection) { $chunk = $connection->readLine(); $prefix = $chunk[0]; $payload = substr($chunk, 1); switch ($prefix) { case '+': // inline switch ($payload) { case 'OK': return true; case 'QUEUED': return new Predis_ResponseQueued(); default: return $payload; } case '$': // bulk $size = (int) $payload; if ($size === -1) { return null; } return substr($connection->readBytes($size + 2), 0, -2); case '*': // multi bulk $count = (int) $payload; if ($count === -1) { return null; } if ($this->_iterableMultibulk) { return new Predis_Shared_MultiBulkResponseIterator($connection, $count); } $multibulk = array(); for ($i = 0; $i < $count; $i++) { $multibulk[$i] = $this->read($connection); } return $multibulk; case ':': // integer return (int) $payload; case '-': // error $errorMessage = substr($payload, 4); if ($this->_throwErrors) { throw new Predis_ServerException($errorMessage); } return new Predis_ResponseError($errorMessage); default: throw new Predis_CommunicationException($connection, "Unknown prefix: '{$prefix}'"); } }
private static function discardNewLine(Predis_Connection $connection) { if ($connection->readBytes(2) !== Predis_Protocol::NEWLINE) { Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse($connection, 'Did not receive a new-line at the end of a bulk response')); } }
public function handle(Predis_Connection $connection, $lengthString) { $length = (int) $lengthString; if ($length != $lengthString) { Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse($connection, "Cannot parse '{$length}' as data length")); } if ($length >= 0) { return substr($connection->readBytes($length + 2), 0, -2); } if ($length == -1) { return null; } }