/** * Helper method to handle exceptions generated by a connection object. * * @param CommunicationException $exception Exception. * * @throws CommunicationException */ public static function handle(CommunicationException $exception) { if ($exception->shouldResetConnection()) { $connection = $exception->getConnection(); if ($connection->isConnected()) { $connection->disconnect(); } } throw $exception; }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { $length = (int) $payload; if ("{$length}" !== $payload) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid length of a multi-bulk response.")); } if ($length === -1) { return; } $list = array(); if ($length > 0) { $handlersCache = array(); $reader = $connection->getProtocol()->getResponseReader(); 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; }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { $length = (int) $payload; if ("{$length}" != $payload) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid length for a multi-bulk response.")); } return new MultiBulkIterator($connection, $length); }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { if (is_numeric($payload)) { return (int) $payload; } if ($payload !== 'nil') { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid numeric response.")); } return; }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { $length = (int) $payload; if ("{$length}" !== $payload) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid length for a bulk response.")); } if ($length >= 0) { return substr($connection->readBuffer($length + 2), 0, -2); } if ($length == -1) { return; } CommunicationException::handle(new ProtocolException($connection, "Value '{$payload}' is not a valid length for a bulk response.")); return; }
/** * {@inheritdoc} */ public function read(CompositeConnectionInterface $connection) { $chunk = $connection->readLine(); $prefix = $chunk[0]; $payload = substr($chunk, 1); switch ($prefix) { case '+': return new StatusResponse($payload); case '$': $size = (int) $payload; if ($size === -1) { return; } return substr($connection->readBuffer($size + 2), 0, -2); case '*': $count = (int) $payload; if ($count === -1) { return; } if ($this->mbiterable) { return new MultiBulkIterator($connection, $count); } $multibulk = array(); for ($i = 0; $i < $count; ++$i) { $multibulk[$i] = $this->read($connection); } return $multibulk; case ':': return (int) $payload; case '-': return new ErrorResponse($payload); default: CommunicationException::handle(new ProtocolException($connection, "Unknown response prefix: '{$prefix}'.")); return; } }
/** * Helper method for protocol errors encountered inside the transaction. * * @param string $message Error message. */ private function onProtocolError($message) { // Since a MULTI/EXEC block cannot be initialized when using aggregate // connections we can safely assume that Predis\Client::getConnection() // will return a Predis\Connection\NodeConnectionInterface instance. CommunicationException::handle(new ProtocolException($this->client->getConnection(), $message)); }
/** * Handles protocol errors generated while reading responses from a * connection. * * @param CompositeConnectionInterface $connection Redis connection that generated the error. * @param string $message Error message. */ protected function onProtocolError(CompositeConnectionInterface $connection, $message) { CommunicationException::handle(new ProtocolException($connection, $message)); }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { CommunicationException::handle(new ProtocolException($this, static::createExceptionMessage($message))); }