ComposableStreamConnection.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 InvalidArgumentException;
  12. use Predis\Command\CommandInterface;
  13. use Predis\Protocol\ProtocolProcessorInterface;
  14. use Predis\Protocol\Text\ProtocolProcessor as TextProtocolProcessor;
  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. protected $protocol;
  24. /**
  25. * @param ParametersInterface $parameters Initialization parameters for the connection.
  26. * @param ProtocolProcessorInterface $protocol Protocol processor.
  27. */
  28. public function __construct(
  29. ParametersInterface $parameters,
  30. ProtocolProcessorInterface $protocol = null
  31. ) {
  32. $this->parameters = $this->assertParameters($parameters);
  33. $this->protocol = $protocol ?: new TextProtocolProcessor();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getProtocol()
  39. {
  40. return $this->protocol;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function writeBuffer($buffer)
  46. {
  47. $this->write($buffer);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function readBuffer($length)
  53. {
  54. if ($length <= 0) {
  55. throw new InvalidArgumentException('Length parameter must be greater than 0.');
  56. }
  57. $value = '';
  58. $socket = $this->getResource();
  59. do {
  60. $chunk = fread($socket, $length);
  61. if ($chunk === false || $chunk === '') {
  62. $this->onConnectionError('Error while reading bytes from the server.');
  63. }
  64. $value .= $chunk;
  65. } while (($length -= strlen($chunk)) > 0);
  66. return $value;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function readLine()
  72. {
  73. $value = '';
  74. $socket = $this->getResource();
  75. do {
  76. $chunk = fgets($socket);
  77. if ($chunk === false || $chunk === '') {
  78. $this->onConnectionError('Error while reading line from the server.');
  79. }
  80. $value .= $chunk;
  81. } while (substr($value, -2) !== "\r\n");
  82. return substr($value, 0, -2);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function writeRequest(CommandInterface $command)
  88. {
  89. $this->protocol->write($this, $command);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function read()
  95. {
  96. return $this->protocol->read($this);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function __sleep()
  102. {
  103. return array_merge(parent::__sleep(), array('protocol'));
  104. }
  105. }