123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace Predis\Protocol\Text;
- use Predis\Helpers;
- use Predis\Protocol\IResponseReader;
- use Predis\Protocol\IResponseHandler;
- use Predis\Protocol\ProtocolException;
- use Predis\Network\IConnectionComposable;
- class TextResponseReader implements IResponseReader
- {
- private $handlers;
-
- public function __construct()
- {
- $this->handlers = $this->getDefaultHandlers();
- }
-
- private 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(),
- );
- }
-
- public function setHandler($prefix, IResponseHandler $handler)
- {
- $this->handlers[$prefix] = $handler;
- }
-
- public function getHandler($prefix)
- {
- if (isset($this->handlers[$prefix])) {
- return $this->handlers[$prefix];
- }
- }
-
- public function read(IConnectionComposable $connection)
- {
- $header = $connection->readLine();
- if ($header === '') {
- $this->protocolError($connection, 'Unexpected empty header');
- }
- $prefix = $header[0];
- if (!isset($this->handlers[$prefix])) {
- $this->protocolError($connection, "Unknown prefix '$prefix'");
- }
- $handler = $this->handlers[$prefix];
- return $handler->handle($connection, substr($header, 1));
- }
-
- private function protocolError(IConnectionComposable $connection, $message)
- {
- Helpers::onCommunicationException(new ProtocolException($connection, $message));
- }
- }
|