ComposableTextProtocol.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Commands\ICommand;
  12. use Predis\Protocol\IResponseReader;
  13. use Predis\Protocol\ICommandSerializer;
  14. use Predis\Protocol\IComposableProtocolProcessor;
  15. use Predis\Network\IConnectionComposable;
  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 IComposableProtocolProcessor
  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. case 'throw_errors':
  61. $handler = $value ? new ResponseErrorHandler() : new ResponseErrorSilentHandler();
  62. $this->reader->setHandler(TextProtocol::PREFIX_ERROR, $handler);
  63. break;
  64. default:
  65. throw new \InvalidArgumentException("The option $option is not supported by the current protocol");
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function serialize(ICommand $command)
  72. {
  73. return $this->serializer->serialize($command);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function write(IConnectionComposable $connection, ICommand $command)
  79. {
  80. $connection->writeBytes($this->serializer->serialize($command));
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function read(IConnectionComposable $connection)
  86. {
  87. return $this->reader->read($connection);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function setSerializer(ICommandSerializer $serializer)
  93. {
  94. $this->serializer = $serializer;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getSerializer()
  100. {
  101. return $this->serializer;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setReader(IResponseReader $reader)
  107. {
  108. $this->reader = $reader;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getReader()
  114. {
  115. return $this->reader;
  116. }
  117. }