/** * Handles a multi-bulk reply returned by Redis. * * @param IConnectionComposable $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return array */ public function handle(IConnectionComposable $connection, $lengthString) { $length = (int) $lengthString; if ($length != $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$length}' as data length")); } if ($length === -1) { return null; } $list = array(); if ($length > 0) { $handlersCache = array(); $reader = $connection->getProtocol()->getReader(); for ($i = 0; $i < $length; $i++) { $header = $connection->readLine(); $prefix = $header[0]; if (isset($handlersCache[$prefix])) { $handler = $handlersCache[$prefix]; } else { $handler = $reader->getHandler($prefix); $handlersCache[$prefix] = $handler; } $list[$i] = $handler->handle($connection, substr($header, 1)); } } return $list; }
/** * Handles a multi-bulk reply returned by Redis in a streamable fashion. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return MultiBulkResponseSimple */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" != $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$lengthString}' as multi-bulk length")); } return new MultiBulkResponseSimple($connection, $length); }
/** * Handles a multi-bulk reply returned by Redis in a streamable fashion. * * @param IConnectionComposable $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return MultiBulkResponseSimple */ public function handle(IConnectionComposable $connection, $lengthString) { $length = (int) $lengthString; if ($length != $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$length}' as data length")); } return new MultiBulkResponseSimple($connection, $length); }
/** * @group disconnected */ public function testOnCommunicationException() { $this->setExpectedException('Predis\\CommunicationException'); $connection = $this->getMock('Predis\\Connection\\SingleConnectionInterface'); $connection->expects($this->once())->method('isConnected')->will($this->returnValue(true)); $connection->expects($this->once())->method('disconnect'); $exception = $this->getMockForAbstractClass('Predis\\CommunicationException', array($connection)); Helpers::onCommunicationException($exception); }
/** * Handles an integer reply returned by Redis. * * @param IConnectionComposable $connection Connection to Redis. * @param string $number String representation of an integer. * @return int */ public function handle(IConnectionComposable $connection, $number) { if (is_numeric($number)) { return (int) $number; } if ($number !== 'nil') { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$number}' as numeric response")); } return null; }
/** * Handles a bulk reply returned by Redis. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Bytes size of the bulk reply. * @return string */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" !== $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$lengthString}' as bulk length")); } if ($length >= 0) { return substr($connection->readBytes($length + 2), 0, -2); } if ($length == -1) { return null; } }
/** * Helper method that handles protocol errors encountered inside a transaction. * * @param string $message Error message. */ private function onProtocolError($message) { // Since a MULTI/EXEC block cannot be initialized over a clustered // connection, we can safely assume that Predis\Client::getConnection() // will always return an instance of Predis\Network\IConnectionSingle. Helpers::onCommunicationException(new ProtocolException($this->client->getConnection(), $message)); }
/** * Helper method used to handle a protocol error generated while reading a * reply from a connection to Redis. * * @param ComposableConnectionInterface $connection Connection to Redis that generated the error. * @param string $message Error message. */ private function protocolError(ComposableConnectionInterface $connection, $message) { Helpers::onCommunicationException(new ProtocolException($connection, $message)); }
/** * {@inheritdoc} */ public function read(ComposableConnectionInterface $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 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->mbiterable == true) { return new MultiBulkResponseSimple($connection, $count); } $multibulk = array(); for ($i = 0; $i < $count; $i++) { $multibulk[$i] = $this->read($connection); } return $multibulk; case ':': // integer return (int) $payload; case '-': // error return new ResponseError($payload); default: Helpers::onCommunicationException(new ProtocolException($connection, "Unknown prefix: '{$prefix}'")); } }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { Helpers::onCommunicationException(new ProtocolException($this, $message)); }
/** * Helper method that handles protocol errors encountered inside a transaction. * * @param string $message Error message. */ private function onProtocolError($message) { // Since a MULTI/EXEC block cannot be initialized when using aggregated // connections, we can safely assume that Predis\Client::getConnection() // will always return an instance of Predis\Connection\SingleConnectionInterface. Helpers::onCommunicationException(new ProtocolException($this->client->getConnection(), $message)); }