ComposableTextProtocol.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Command\CommandInterface;
  12. use Predis\Connection\ComposableConnectionInterface;
  13. use Predis\Protocol\ResponseReaderInterface;
  14. use Predis\Protocol\CommandSerializerInterface;
  15. use Predis\Protocol\ComposableProtocolInterface;
  16. /**
  17. * Implements a customizable protocol processor that uses the standard Redis
  18. * wire protocol to serialize Redis commands and parse replies returned by
  19. * the server using a pluggable set of classes.
  20. *
  21. * @link http://redis.io/topics/protocol
  22. * @author Daniele Alessandri <suppakilla@gmail.com>
  23. */
  24. class ComposableTextProtocol implements ComposableProtocolInterface
  25. {
  26. private $serializer;
  27. private $reader;
  28. /**
  29. * @param array $options Set of options used to initialize the protocol processor.
  30. */
  31. public function __construct(Array $options = array())
  32. {
  33. $this->setSerializer(new TextCommandSerializer());
  34. $this->setReader(new TextResponseReader());
  35. if (count($options) > 0) {
  36. $this->initializeOptions($options);
  37. }
  38. }
  39. /**
  40. * Initializes the protocol processor using a set of options.
  41. *
  42. * @param array $options Set of options.
  43. */
  44. private function initializeOptions(Array $options)
  45. {
  46. foreach ($options as $k => $v) {
  47. $this->setOption($k, $v);
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function setOption($option, $value)
  54. {
  55. switch ($option) {
  56. case 'iterable_multibulk':
  57. $handler = $value ? new ResponseMultiBulkStreamHandler() : new ResponseMultiBulkHandler();
  58. $this->reader->setHandler(TextProtocol::PREFIX_MULTI_BULK, $handler);
  59. break;
  60. default:
  61. throw new \InvalidArgumentException("The option $option is not supported by the current protocol");
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function serialize(CommandInterface $command)
  68. {
  69. return $this->serializer->serialize($command);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function write(ComposableConnectionInterface $connection, CommandInterface $command)
  75. {
  76. $connection->writeBytes($this->serializer->serialize($command));
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function read(ComposableConnectionInterface $connection)
  82. {
  83. return $this->reader->read($connection);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setSerializer(CommandSerializerInterface $serializer)
  89. {
  90. $this->serializer = $serializer;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getSerializer()
  96. {
  97. return $this->serializer;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setReader(ResponseReaderInterface $reader)
  103. {
  104. $this->reader = $reader;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getReader()
  110. {
  111. return $this->reader;
  112. }
  113. }