TextResponseReader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Protocol\Text;
  11. use Predis\CommunicationException;
  12. use Predis\Connection\ComposableConnectionInterface;
  13. use Predis\Protocol\ProtocolException;
  14. use Predis\Protocol\ResponseHandlerInterface;
  15. use Predis\Protocol\ResponseReaderInterface;
  16. /**
  17. * Response reader for the standard Redis wire protocol.
  18. *
  19. * @link http://redis.io/topics/protocol
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class TextResponseReader implements ResponseReaderInterface
  23. {
  24. protected $handlers;
  25. /**
  26. *
  27. */
  28. public function __construct()
  29. {
  30. $this->handlers = $this->getDefaultHandlers();
  31. }
  32. /**
  33. * Returns the default handlers for the supported type of responses.
  34. *
  35. * @return array
  36. */
  37. protected function getDefaultHandlers()
  38. {
  39. return array(
  40. TextProtocol::PREFIX_STATUS => new ResponseStatusHandler(),
  41. TextProtocol::PREFIX_ERROR => new ResponseErrorHandler(),
  42. TextProtocol::PREFIX_INTEGER => new ResponseIntegerHandler(),
  43. TextProtocol::PREFIX_BULK => new ResponseBulkHandler(),
  44. TextProtocol::PREFIX_MULTI_BULK => new ResponseMultiBulkHandler(),
  45. );
  46. }
  47. /**
  48. * Sets a response handler for a certain prefix that identifies a type of
  49. * response that can be returned by Redis.
  50. *
  51. * @param string $prefix Identifier of the type of response.
  52. * @param ResponseHandlerInterface $handler Response handler.
  53. */
  54. public function setHandler($prefix, ResponseHandlerInterface $handler)
  55. {
  56. $this->handlers[$prefix] = $handler;
  57. }
  58. /**
  59. * Returns the response handler associated to a certain type of response.
  60. *
  61. * @param string $prefix Identifier of the type of response.
  62. * @return ResponseHandlerInterface
  63. */
  64. public function getHandler($prefix)
  65. {
  66. if (isset($this->handlers[$prefix])) {
  67. return $this->handlers[$prefix];
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function read(ComposableConnectionInterface $connection)
  74. {
  75. $header = $connection->readLine();
  76. if ($header === '') {
  77. $this->onProtocolError($connection, 'Unexpected empty header');
  78. }
  79. $prefix = $header[0];
  80. if (!isset($this->handlers[$prefix])) {
  81. $this->onProtocolError($connection, "Unknown prefix: '$prefix'");
  82. }
  83. $handler = $this->handlers[$prefix];
  84. return $handler->handle($connection, substr($header, 1));
  85. }
  86. /**
  87. * Handles protocol errors generated while reading responses from the
  88. * connection.
  89. *
  90. * @param ComposableConnectionInterface $connection Connection to Redis that generated the error.
  91. * @param string $message Error message.
  92. */
  93. protected function onProtocolError(ComposableConnectionInterface $connection, $message)
  94. {
  95. CommunicationException::handle(
  96. new ProtocolException($connection, $message)
  97. );
  98. }
  99. }