MultiBulkResponseSimple.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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->_protocol = $connection->getProtocol();
  10. $this->_position = 0;
  11. $this->_current = $size > 0 ? $this->getValue() : null;
  12. $this->_replySize = $size;
  13. }
  14. public function __destruct() {
  15. // When the iterator is garbage-collected (e.g. it goes out of the
  16. // scope of a foreach) but it has not reached its end, we must sync
  17. // the client with the queued elements that have not been read from
  18. // the connection with the server.
  19. $this->sync();
  20. }
  21. public function sync($drop = false) {
  22. if ($drop == true) {
  23. if ($this->valid()) {
  24. $this->_position = $this->_replySize;
  25. $this->_connection->disconnect();
  26. }
  27. }
  28. else {
  29. while ($this->valid()) {
  30. $this->next();
  31. }
  32. }
  33. }
  34. protected function getValue() {
  35. return $this->_protocol->read($this->_connection);
  36. }
  37. }