ComposableStreamConnection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Network;
  11. use Predis\IConnectionParameters;
  12. use Predis\Commands\ICommand;
  13. use Predis\Protocol\IProtocolProcessor;
  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 IConnectionComposable
  22. {
  23. private $protocol;
  24. /**
  25. * @param IConnectionParameters $parameters Parameters used to initialize the connection.
  26. * @param IProtocolProcessor $protocol A protocol processor.
  27. */
  28. public function __construct(IConnectionParameters $parameters, IProtocolProcessor $protocol = null)
  29. {
  30. $this->setProtocol($protocol ?: new TextProtocol());
  31. parent::__construct($parameters);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function initializeProtocol(IConnectionParameters $parameters)
  37. {
  38. $this->protocol->setOption('throw_errors', $parameters->throw_errors);
  39. $this->protocol->setOption('iterable_multibulk', $parameters->iterable_multibulk);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function setProtocol(IProtocolProcessor $protocol)
  45. {
  46. if ($protocol === null) {
  47. throw new \InvalidArgumentException("The protocol instance cannot be a null value");
  48. }
  49. $this->protocol = $protocol;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getProtocol()
  55. {
  56. return $this->protocol;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function writeBytes($buffer)
  62. {
  63. parent::writeBytes($buffer);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function readBytes($length)
  69. {
  70. if ($length <= 0) {
  71. throw new \InvalidArgumentException('Length parameter must be greater than 0');
  72. }
  73. $value = '';
  74. $socket = $this->getResource();
  75. do {
  76. $chunk = fread($socket, $length);
  77. if ($chunk === false || $chunk === '') {
  78. $this->onConnectionError('Error while reading bytes from the server');
  79. }
  80. $value .= $chunk;
  81. }
  82. while (($length -= strlen($chunk)) > 0);
  83. return $value;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function readLine()
  89. {
  90. $value = '';
  91. $socket = $this->getResource();
  92. do {
  93. $chunk = fgets($socket);
  94. if ($chunk === false || $chunk === '') {
  95. $this->onConnectionError('Error while reading line from the server');
  96. }
  97. $value .= $chunk;
  98. }
  99. while (substr($value, -2) !== "\r\n");
  100. return substr($value, 0, -2);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function writeCommand(ICommand $command)
  106. {
  107. $this->protocol->write($this, $command);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function read()
  113. {
  114. return $this->protocol->read($this);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function __sleep()
  120. {
  121. return array_merge(parent::__sleep(), array('protocol'));
  122. }
  123. }