MultiBulkResponseSimple.php 1.4 KB

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