TextProtocol.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\ResponseError;
  13. use Predis\ResponseQueued;
  14. use Predis\ServerException;
  15. use Predis\Command\CommandInterface;
  16. use Predis\Connection\ComposableConnectionInterface;
  17. use Predis\Iterator\MultiBulkResponseSimple;
  18. use Predis\Protocol\ProtocolException;
  19. use Predis\Protocol\ProtocolInterface;
  20. /**
  21. * Implements a protocol processor for the standard wire protocol defined by Redis.
  22. *
  23. * @link http://redis.io/topics/protocol
  24. * @author Daniele Alessandri <suppakilla@gmail.com>
  25. */
  26. class TextProtocol implements ProtocolInterface
  27. {
  28. const NEWLINE = "\r\n";
  29. const OK = 'OK';
  30. const ERROR = 'ERR';
  31. const QUEUED = 'QUEUED';
  32. const NULL = 'nil';
  33. const PREFIX_STATUS = '+';
  34. const PREFIX_ERROR = '-';
  35. const PREFIX_INTEGER = ':';
  36. const PREFIX_BULK = '$';
  37. const PREFIX_MULTI_BULK = '*';
  38. const BUFFER_SIZE = 4096;
  39. private $mbiterable;
  40. private $serializer;
  41. /**
  42. *
  43. */
  44. public function __construct()
  45. {
  46. $this->mbiterable = false;
  47. $this->serializer = new TextCommandSerializer();
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function write(ComposableConnectionInterface $connection, CommandInterface $command)
  53. {
  54. $connection->writeBytes($this->serializer->serialize($command));
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function read(ComposableConnectionInterface $connection)
  60. {
  61. $chunk = $connection->readLine();
  62. $prefix = $chunk[0];
  63. $payload = substr($chunk, 1);
  64. switch ($prefix) {
  65. case '+': // inline
  66. switch ($payload) {
  67. case 'OK':
  68. return true;
  69. case 'QUEUED':
  70. return new ResponseQueued();
  71. default:
  72. return $payload;
  73. }
  74. case '$': // bulk
  75. $size = (int) $payload;
  76. if ($size === -1) {
  77. return null;
  78. }
  79. return substr($connection->readBytes($size + 2), 0, -2);
  80. case '*': // multi bulk
  81. $count = (int) $payload;
  82. if ($count === -1) {
  83. return null;
  84. }
  85. if ($this->mbiterable == true) {
  86. return new MultiBulkResponseSimple($connection, $count);
  87. }
  88. $multibulk = array();
  89. for ($i = 0; $i < $count; $i++) {
  90. $multibulk[$i] = $this->read($connection);
  91. }
  92. return $multibulk;
  93. case ':': // integer
  94. return (int) $payload;
  95. case '-': // error
  96. return new ResponseError($payload);
  97. default:
  98. Helpers::onCommunicationException(new ProtocolException(
  99. $connection, "Unknown prefix: '$prefix'"
  100. ));
  101. }
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setOption($option, $value)
  107. {
  108. switch ($option) {
  109. case 'iterable_multibulk':
  110. $this->mbiterable = (bool) $value;
  111. break;
  112. }
  113. }
  114. }