MultiBulkResponseSimple.php 1.2 KB

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