ComposableStreamConnection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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->protocol = $protocol;
  32. $this->parameters = $this->checkParameters($parameters);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function setProtocol(ProtocolInterface $protocol)
  38. {
  39. if ($protocol === null) {
  40. throw new \InvalidArgumentException("The protocol instance cannot be a null value");
  41. }
  42. $this->protocol = $protocol;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getProtocol()
  48. {
  49. return $this->protocol;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function writeBytes($buffer)
  55. {
  56. parent::writeBytes($buffer);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function readBytes($length)
  62. {
  63. if ($length <= 0) {
  64. throw new \InvalidArgumentException('Length parameter must be greater than 0');
  65. }
  66. $value = '';
  67. $socket = $this->getResource();
  68. do {
  69. $chunk = fread($socket, $length);
  70. if ($chunk === false || $chunk === '') {
  71. $this->onConnectionError('Error while reading bytes from the server');
  72. }
  73. $value .= $chunk;
  74. } while (($length -= strlen($chunk)) > 0);
  75. return $value;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function readLine()
  81. {
  82. $value = '';
  83. $socket = $this->getResource();
  84. do {
  85. $chunk = fgets($socket);
  86. if ($chunk === false || $chunk === '') {
  87. $this->onConnectionError('Error while reading line from the server');
  88. }
  89. $value .= $chunk;
  90. } while (substr($value, -2) !== "\r\n");
  91. return substr($value, 0, -2);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function writeCommand(CommandInterface $command)
  97. {
  98. $this->protocol->write($this, $command);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function read()
  104. {
  105. return $this->protocol->read($this);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function __sleep()
  111. {
  112. return array_merge(parent::__sleep(), array('protocol'));
  113. }
  114. }