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. $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. }
  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. }
  92. while (substr($value, -2) !== "\r\n");
  93. return substr($value, 0, -2);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function writeCommand(CommandInterface $command)
  99. {
  100. $this->protocol->write($this, $command);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function read()
  106. {
  107. return $this->protocol->read($this);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function __sleep()
  113. {
  114. return array_merge(parent::__sleep(), array('protocol'));
  115. }
  116. }