ComposableStreamConnection.php 3.4 KB

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