ComposableStreamConnection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $protocol = $protocol ?: new TextProtocol();
  31. $protocol->setOption('iterable_multibulk', $parameters->iterable_multibulk);
  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. }
  76. while (($length -= strlen($chunk)) > 0);
  77. return $value;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function readLine()
  83. {
  84. $value = '';
  85. $socket = $this->getResource();
  86. do {
  87. $chunk = fgets($socket);
  88. if ($chunk === false || $chunk === '') {
  89. $this->onConnectionError('Error while reading line from the server');
  90. }
  91. $value .= $chunk;
  92. }
  93. while (substr($value, -2) !== "\r\n");
  94. return substr($value, 0, -2);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function writeCommand(CommandInterface $command)
  100. {
  101. $this->protocol->write($this, $command);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function read()
  107. {
  108. return $this->protocol->read($this);
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function __sleep()
  114. {
  115. return array_merge(parent::__sleep(), array('protocol'));
  116. }
  117. }