123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace Predis\Protocol\Text;
- use Predis\CommunicationException;
- use Predis\Connection\ComposableConnectionInterface;
- use Predis\Protocol\ProtocolException;
- use Predis\Protocol\ResponseReaderInterface;
- class ResponseReader implements ResponseReaderInterface
- {
- protected $handlers;
-
- public function __construct()
- {
- $this->handlers = $this->getDefaultHandlers();
- }
-
- protected function getDefaultHandlers()
- {
- return array(
- '+' => new Handler\StatusResponse(),
- '-' => new Handler\ErrorResponse(),
- ':' => new Handler\IntegerResponse(),
- '$' => new Handler\BulkResponse(),
- '*' => new Handler\MultiBulkResponse(),
- );
- }
-
- public function setHandler($prefix, Handler\ResponseHandlerInterface $handler)
- {
- $this->handlers[$prefix] = $handler;
- }
-
- public function getHandler($prefix)
- {
- if (isset($this->handlers[$prefix])) {
- return $this->handlers[$prefix];
- }
- }
-
- public function read(ComposableConnectionInterface $connection)
- {
- $header = $connection->readLine();
- if ($header === '') {
- $this->onProtocolError($connection, 'Unexpected empty header');
- }
- $prefix = $header[0];
- if (!isset($this->handlers[$prefix])) {
- $this->onProtocolError($connection, "Unknown prefix: '$prefix'");
- }
- $handler = $this->handlers[$prefix];
- return $handler->handle($connection, substr($header, 1));
- }
-
- protected function onProtocolError(ComposableConnectionInterface $connection, $message)
- {
- CommunicationException::handle(
- new ProtocolException($connection, $message)
- );
- }
- }
|