ProtocolProcessor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Command\CommandInterface;
  13. use Predis\Connection\ComposableConnectionInterface;
  14. use Predis\Protocol\ProtocolException;
  15. use Predis\Protocol\ProtocolProcessorInterface;
  16. use Predis\Response\Status as StatusResponse;
  17. use Predis\Response\Error as ErrorResponse;
  18. use Predis\Response\Iterator\MultiBulk as MultiBulkIterator;
  19. /**
  20. * Protocol processor for the standard Redis wire protocol.
  21. *
  22. * @link http://redis.io/topics/protocol
  23. * @author Daniele Alessandri <suppakilla@gmail.com>
  24. */
  25. class ProtocolProcessor implements ProtocolProcessorInterface
  26. {
  27. protected $mbiterable;
  28. protected $serializer;
  29. /**
  30. *
  31. */
  32. public function __construct()
  33. {
  34. $this->mbiterable = false;
  35. $this->serializer = new RequestSerializer();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function write(ComposableConnectionInterface $connection, CommandInterface $command)
  41. {
  42. $request = $this->serializer->serialize($command);
  43. $connection->writeBuffer($request);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function read(ComposableConnectionInterface $connection)
  49. {
  50. $chunk = $connection->readLine();
  51. $prefix = $chunk[0];
  52. $payload = substr($chunk, 1);
  53. switch ($prefix) {
  54. case '+': // inline
  55. return new StatusResponse($payload);
  56. case '$': // bulk
  57. $size = (int) $payload;
  58. if ($size === -1) {
  59. return null;
  60. }
  61. return substr($connection->readBuffer($size + 2), 0, -2);
  62. case '*': // multi bulk
  63. $count = (int) $payload;
  64. if ($count === -1) {
  65. return null;
  66. }
  67. if ($this->mbiterable) {
  68. return new MultiBulkIterator($connection, $count);
  69. }
  70. $multibulk = array();
  71. for ($i = 0; $i < $count; $i++) {
  72. $multibulk[$i] = $this->read($connection);
  73. }
  74. return $multibulk;
  75. case ':': // integer
  76. return (int) $payload;
  77. case '-': // error
  78. return new ErrorResponse($payload);
  79. default:
  80. CommunicationException::handle(new ProtocolException(
  81. $connection, "Unknown response prefix: '$prefix'."
  82. ));
  83. }
  84. }
  85. /**
  86. * Enables or disables returning multibulk responses as specialized PHP
  87. * iterators used to stream bulk elements of a multibulk response instead
  88. * returning a plain array.
  89. *
  90. * Streamable multibulk responses are not globally supported by the
  91. * abstractions built-in into Predis, such as transactions or pipelines.
  92. * Use them with care!
  93. *
  94. * @param bool $value Enable or disable streamable multibulk responses.
  95. */
  96. public function useIterableMultibulk($value)
  97. {
  98. $this->mbiterable = (bool) $value;
  99. }
  100. }