StandardExecutor.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Pipeline;
  11. use Predis\ServerException;
  12. use Predis\Network\IConnection;
  13. /**
  14. * Implements the standard pipeline executor strategy used
  15. * to write a list of commands and read their replies over
  16. * a connection to Redis.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class StandardExecutor implements IPipelineExecutor
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function execute(IConnection $connection, &$commands)
  26. {
  27. $sizeofPipe = count($commands);
  28. $values = array();
  29. foreach ($commands as $command) {
  30. $connection->writeCommand($command);
  31. }
  32. try {
  33. for ($i = 0; $i < $sizeofPipe; $i++) {
  34. $response = $connection->readResponse($commands[$i]);
  35. $values[] = $response instanceof \Iterator
  36. ? iterator_to_array($response)
  37. : $response;
  38. unset($commands[$i]);
  39. }
  40. }
  41. catch (ServerException $exception) {
  42. // Force disconnection to prevent protocol desynchronization.
  43. $connection->disconnect();
  44. throw $exception;
  45. }
  46. return $values;
  47. }
  48. }