TextResponseReader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Helpers;
  12. use Predis\Protocol\IResponseReader;
  13. use Predis\Protocol\IResponseHandler;
  14. use Predis\Protocol\ProtocolException;
  15. use Predis\Network\IConnectionComposable;
  16. /**
  17. * Implements a pluggable response reader using the standard wire protocol
  18. * defined by Redis.
  19. *
  20. * @link http://redis.io/topics/protocol
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class TextResponseReader implements IResponseReader
  24. {
  25. private $handlers;
  26. /**
  27. *
  28. */
  29. public function __construct()
  30. {
  31. $this->handlers = $this->getDefaultHandlers();
  32. }
  33. /**
  34. * Returns the default set of response handlers for all the type of replies
  35. * that can be returned by Redis.
  36. */
  37. private 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. * reply that can be returned by Redis.
  50. *
  51. * @param string $prefix Identifier for a type of reply.
  52. * @param IResponseHandler $handler Response handler for the reply.
  53. */
  54. public function setHandler($prefix, IResponseHandler $handler)
  55. {
  56. $this->handlers[$prefix] = $handler;
  57. }
  58. /**
  59. * Returns the response handler associated to a certain type of reply that
  60. * can be returned by Redis.
  61. *
  62. * @param string $prefix Identifier for a type of reply.
  63. * @return IResponseHandler
  64. */
  65. public function getHandler($prefix)
  66. {
  67. if (isset($this->handlers[$prefix])) {
  68. return $this->handlers[$prefix];
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function read(IConnectionComposable $connection)
  75. {
  76. $header = $connection->readLine();
  77. if ($header === '') {
  78. $this->protocolError($connection, 'Unexpected empty header');
  79. }
  80. $prefix = $header[0];
  81. if (!isset($this->handlers[$prefix])) {
  82. $this->protocolError($connection, "Unknown prefix '$prefix'");
  83. }
  84. $handler = $this->handlers[$prefix];
  85. return $handler->handle($connection, substr($header, 1));
  86. }
  87. /**
  88. * Helper method used to handle a protocol error generated while reading a
  89. * reply from a connection to Redis.
  90. *
  91. * @param IConnectionComposable $connection Connection to Redis that generated the error.
  92. * @param string $message Error message.
  93. */
  94. private function protocolError(IConnectionComposable $connection, $message)
  95. {
  96. Helpers::onCommunicationException(new ProtocolException($connection, $message));
  97. }
  98. }