ComposableStreamConnection.php 2.8 KB

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