ComposableProtocolProcessor.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 write(ComposableConnectionInterface $connection, CommandInterface $command)
  42. {
  43. $connection->writeBytes($this->serializer->serialize($command));
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function read(ComposableConnectionInterface $connection)
  49. {
  50. return $this->reader->read($connection);
  51. }
  52. /**
  53. * Sets the request serializer used by the protocol processor.
  54. *
  55. * @param RequestSerializerInterface $serializer Request serializer.
  56. */
  57. public function setRequestSerializer(RequestSerializerInterface $serializer)
  58. {
  59. $this->serializer = $serializer;
  60. }
  61. /**
  62. * Returns the request serializer used by the protocol processor.
  63. *
  64. * @return RequestSerializerInterface
  65. */
  66. public function getRequestSerializer()
  67. {
  68. return $this->serializer;
  69. }
  70. /**
  71. * Sets the response reader used by the protocol processor.
  72. *
  73. * @param ResponseReaderInterface $reader Response reader.
  74. */
  75. public function setResponseReader(ResponseReaderInterface $reader)
  76. {
  77. $this->reader = $reader;
  78. }
  79. /**
  80. * Returns the Response reader used by the protocol processor.
  81. *
  82. * @return ResponseReaderInterface
  83. */
  84. public function getResponseReader()
  85. {
  86. return $this->reader;
  87. }
  88. }