|
@@ -14,19 +14,17 @@ namespace Predis\Protocol\Text;
|
|
|
use Predis\CommunicationException;
|
|
|
use Predis\Connection\ComposableConnectionInterface;
|
|
|
use Predis\Protocol\ProtocolException;
|
|
|
-use Predis\Protocol\ResponseHandlerInterface;
|
|
|
use Predis\Protocol\ResponseReaderInterface;
|
|
|
|
|
|
/**
|
|
|
- * Implements a pluggable response reader using the standard wire protocol
|
|
|
- * defined by Redis.
|
|
|
+ * Response reader for the standard Redis wire protocol.
|
|
|
*
|
|
|
* @link http://redis.io/topics/protocol
|
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
|
*/
|
|
|
-class TextResponseReader implements ResponseReaderInterface
|
|
|
+class ResponseReader implements ResponseReaderInterface
|
|
|
{
|
|
|
- private $handlers;
|
|
|
+ protected $handlers;
|
|
|
|
|
|
/**
|
|
|
*
|
|
@@ -37,37 +35,36 @@ class TextResponseReader implements ResponseReaderInterface
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the default set of response handlers for all the type of replies
|
|
|
- * that can be returned by Redis.
|
|
|
+ * Returns the default handlers for the supported type of responses.
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
*/
|
|
|
- private function getDefaultHandlers()
|
|
|
+ protected function getDefaultHandlers()
|
|
|
{
|
|
|
return array(
|
|
|
- TextProtocol::PREFIX_STATUS => new ResponseStatusHandler(),
|
|
|
- TextProtocol::PREFIX_ERROR => new ResponseErrorHandler(),
|
|
|
- TextProtocol::PREFIX_INTEGER => new ResponseIntegerHandler(),
|
|
|
- TextProtocol::PREFIX_BULK => new ResponseBulkHandler(),
|
|
|
- TextProtocol::PREFIX_MULTI_BULK => new ResponseMultiBulkHandler(),
|
|
|
+ '+' => new Handler\StatusResponse(),
|
|
|
+ '-' => new Handler\ErrorResponse(),
|
|
|
+ ':' => new Handler\IntegerResponse(),
|
|
|
+ '$' => new Handler\BulkResponse(),
|
|
|
+ '*' => new Handler\MultiBulkResponse(),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets a response handler for a certain prefix that identifies a type of
|
|
|
- * reply that can be returned by Redis.
|
|
|
+ * Sets the handler for the specified prefix identifying the response type.
|
|
|
*
|
|
|
- * @param string $prefix Identifier for a type of reply.
|
|
|
- * @param ResponseHandlerInterface $handler Response handler for the reply.
|
|
|
+ * @param string $prefix Identifier of the type of response.
|
|
|
+ * @param Handler\ResponseHandlerInterface $handler Response handler.
|
|
|
*/
|
|
|
- public function setHandler($prefix, ResponseHandlerInterface $handler)
|
|
|
+ public function setHandler($prefix, Handler\ResponseHandlerInterface $handler)
|
|
|
{
|
|
|
$this->handlers[$prefix] = $handler;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the response handler associated to a certain type of reply that
|
|
|
- * can be returned by Redis.
|
|
|
+ * Returns the response handler associated to a certain type of response.
|
|
|
*
|
|
|
- * @param string $prefix Identifier for a type of reply.
|
|
|
+ * @param string $prefix Identifier of the type of response.
|
|
|
* @return ResponseHandlerInterface
|
|
|
*/
|
|
|
public function getHandler($prefix)
|
|
@@ -85,13 +82,13 @@ class TextResponseReader implements ResponseReaderInterface
|
|
|
$header = $connection->readLine();
|
|
|
|
|
|
if ($header === '') {
|
|
|
- $this->protocolError($connection, 'Unexpected empty header');
|
|
|
+ $this->onProtocolError($connection, 'Unexpected empty header');
|
|
|
}
|
|
|
|
|
|
$prefix = $header[0];
|
|
|
|
|
|
if (!isset($this->handlers[$prefix])) {
|
|
|
- $this->protocolError($connection, "Unknown prefix: '$prefix'");
|
|
|
+ $this->onProtocolError($connection, "Unknown prefix: '$prefix'");
|
|
|
}
|
|
|
|
|
|
$handler = $this->handlers[$prefix];
|
|
@@ -100,14 +97,16 @@ class TextResponseReader implements ResponseReaderInterface
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Helper method used to handle a protocol error generated while reading a
|
|
|
- * reply from a connection to Redis.
|
|
|
+ * Handles protocol errors generated while reading responses from the
|
|
|
+ * connection.
|
|
|
*
|
|
|
* @param ComposableConnectionInterface $connection Connection to Redis that generated the error.
|
|
|
* @param string $message Error message.
|
|
|
*/
|
|
|
- private function protocolError(ComposableConnectionInterface $connection, $message)
|
|
|
+ protected function onProtocolError(ComposableConnectionInterface $connection, $message)
|
|
|
{
|
|
|
- CommunicationException::handle(new ProtocolException($connection, $message));
|
|
|
+ CommunicationException::handle(
|
|
|
+ new ProtocolException($connection, $message)
|
|
|
+ );
|
|
|
}
|
|
|
}
|