ComposableStreamConnection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Connection;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Protocol\ProtocolInterface;
  13. use Predis\Protocol\Text\TextProtocol;
  14. /**
  15. * Connection abstraction to Redis servers based on PHP's stream that uses an
  16. * external protocol processor defining the protocol used for the communication.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class ComposableStreamConnection extends StreamConnection implements ComposableConnectionInterface
  21. {
  22. private $protocol;
  23. /**
  24. * @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
  25. * @param ProtocolInterface $protocol A protocol processor.
  26. */
  27. public function __construct(ConnectionParametersInterface $parameters, ProtocolInterface $protocol = null)
  28. {
  29. $protocol = $protocol ?: new TextProtocol();
  30. $protocol->setOption('iterable_multibulk', $parameters->iterable_multibulk);
  31. $this->mbiterable = null;
  32. $this->protocol = $protocol;
  33. $this->parameters = $this->checkParameters($parameters);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function setProtocol(ProtocolInterface $protocol)
  39. {
  40. if ($protocol === null) {
  41. throw new \InvalidArgumentException("The protocol instance cannot be a null value");
  42. }
  43. $this->protocol = $protocol;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getProtocol()
  49. {
  50. return $this->protocol;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function writeBytes($buffer)
  56. {
  57. parent::writeBytes($buffer);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function readBytes($length)
  63. {
  64. if ($length <= 0) {
  65. throw new \InvalidArgumentException('Length parameter must be greater than 0');
  66. }
  67. $value = '';
  68. $socket = $this->getResource();
  69. do {
  70. $chunk = fread($socket, $length);
  71. if ($chunk === false || $chunk === '') {
  72. $this->onConnectionError('Error while reading bytes from the server');
  73. }
  74. $value .= $chunk;
  75. } while (($length -= strlen($chunk)) > 0);
  76. return $value;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function readLine()
  82. {
  83. $value = '';
  84. $socket = $this->getResource();
  85. do {
  86. $chunk = fgets($socket);
  87. if ($chunk === false || $chunk === '') {
  88. $this->onConnectionError('Error while reading line from the server');
  89. }
  90. $value .= $chunk;
  91. } while (substr($value, -2) !== "\r\n");
  92. return substr($value, 0, -2);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function writeCommand(CommandInterface $command)
  98. {
  99. $this->protocol->write($this, $command);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function read()
  105. {
  106. return $this->protocol->read($this);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function __sleep()
  112. {
  113. return array_merge(parent::__sleep(), array('protocol'));
  114. }
  115. }