MultiBulkResponseSimple.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Predis\Iterators;
  3. use Predis\Network\IConnection;
  4. use Predis\Network\IConnectionSingle;
  5. class MultiBulkResponseSimple extends MultiBulkResponse {
  6. private $_connection;
  7. public function __construct(IConnectionSingle $connection, $size) {
  8. $this->_connection = $connection;
  9. $this->_position = 0;
  10. $this->_current = $size > 0 ? $this->getValue() : null;
  11. $this->_replySize = $size;
  12. }
  13. public function __destruct() {
  14. // When the iterator is garbage-collected (e.g. it goes out of the
  15. // scope of a foreach) but it has not reached its end, we must sync
  16. // the client with the queued elements that have not been read from
  17. // the connection with the server.
  18. $this->sync();
  19. }
  20. public function sync($drop = false) {
  21. if ($drop == true) {
  22. if ($this->valid()) {
  23. $this->_position = $this->_replySize;
  24. $this->_connection->disconnect();
  25. }
  26. }
  27. else {
  28. while ($this->valid()) {
  29. $this->next();
  30. }
  31. }
  32. }
  33. protected function getValue() {
  34. return $this->_connection->read();
  35. }
  36. }