ComposableProtocolProcessor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\ProtocolProcessorInterface;
  14. use Predis\Protocol\RequestSerializerInterface;
  15. use Predis\Protocol\ResponseReaderInterface;
  16. /**
  17. * Composable protocol processor for the standard Redis wire protocol using
  18. * pluggable handlers to serialize requests and deserialize responses.
  19. *
  20. * @link http://redis.io/topics/protocol
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class ComposableProtocolProcessor implements ProtocolProcessorInterface
  24. {
  25. protected $serializer;
  26. protected $reader;
  27. /**
  28. * @param RequestSerializerInterface $serializer Request serializer.
  29. * @param ResponseReaderInterface $reader Response reader.
  30. */
  31. public function __construct(
  32. RequestSerializerInterface $serializer = null,
  33. ResponseReaderInterface $reader = null
  34. ) {
  35. $this->setRequestSerializer($serializer ?: new RequestSerializer());
  36. $this->setResponseReader($reader ?: new ResponseReader());
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function serialize(CommandInterface $command)
  42. {
  43. return $this->serializer->serialize($command);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function write(ComposableConnectionInterface $connection, CommandInterface $command)
  49. {
  50. $connection->writeBytes($this->serializer->serialize($command));
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function read(ComposableConnectionInterface $connection)
  56. {
  57. return $this->reader->read($connection);
  58. }
  59. /**
  60. * Sets the request serializer used by the protocol processor.
  61. *
  62. * @param RequestSerializerInterface $serializer Request serializer.
  63. */
  64. public function setRequestSerializer(RequestSerializerInterface $serializer)
  65. {
  66. $this->serializer = $serializer;
  67. }
  68. /**
  69. * Returns the request serializer used by the protocol processor.
  70. *
  71. * @return RequestSerializerInterface
  72. */
  73. public function getRequestSerializer()
  74. {
  75. return $this->serializer;
  76. }
  77. /**
  78. * Sets the response reader used by the protocol processor.
  79. *
  80. * @param ResponseReaderInterface $reader Response reader.
  81. */
  82. public function setResponseReader(ResponseReaderInterface $reader)
  83. {
  84. $this->reader = $reader;
  85. }
  86. /**
  87. * Returns the Response reader used by the protocol processor.
  88. *
  89. * @return ResponseReaderInterface
  90. */
  91. public function getResponseReader()
  92. {
  93. return $this->reader;
  94. }
  95. }