ComposableStreamConnection.php 3.2 KB

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