ProtocolProcessor.php 3.2 KB

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