ResponseReader.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\CompositeConnectionInterface;
  13. use Predis\Protocol\ProtocolException;
  14. use Predis\Protocol\ResponseReaderInterface;
  15. /**
  16. * Response reader for the standard Redis wire protocol.
  17. *
  18. * @link http://redis.io/topics/protocol
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class ResponseReader 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. '+' => new Handler\StatusResponse(),
  41. '-' => new Handler\ErrorResponse(),
  42. ':' => new Handler\IntegerResponse(),
  43. '$' => new Handler\BulkResponse(),
  44. '*' => new Handler\MultiBulkResponse(),
  45. );
  46. }
  47. /**
  48. * Sets the handler for the specified prefix identifying the response type.
  49. *
  50. * @param string $prefix Identifier of the type of response.
  51. * @param Handler\ResponseHandlerInterface $handler Response handler.
  52. */
  53. public function setHandler($prefix, Handler\ResponseHandlerInterface $handler)
  54. {
  55. $this->handlers[$prefix] = $handler;
  56. }
  57. /**
  58. * Returns the response handler associated to a certain type of response.
  59. *
  60. * @param string $prefix Identifier of the type of response.
  61. *
  62. * @return Handler\ResponseHandlerInterface
  63. */
  64. public function getHandler($prefix)
  65. {
  66. if (isset($this->handlers[$prefix])) {
  67. return $this->handlers[$prefix];
  68. }
  69. return;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function read(CompositeConnectionInterface $connection)
  75. {
  76. $header = $connection->readLine();
  77. if ($header === '') {
  78. $this->onProtocolError($connection, 'Unexpected empty reponse header');
  79. }
  80. $prefix = $header[0];
  81. if (!isset($this->handlers[$prefix])) {
  82. $this->onProtocolError($connection, "Unknown response prefix: '$prefix'");
  83. }
  84. $payload = $this->handlers[$prefix]->handle($connection, substr($header, 1));
  85. return $payload;
  86. }
  87. /**
  88. * Handles protocol errors generated while reading responses from a
  89. * connection.
  90. *
  91. * @param CompositeConnectionInterface $connection Redis connection that generated the error.
  92. * @param string $message Error message.
  93. */
  94. protected function onProtocolError(CompositeConnectionInterface $connection, $message)
  95. {
  96. CommunicationException::handle(
  97. new ProtocolException($connection, "$message [{$connection->getParameters()}]")
  98. );
  99. }
  100. }