SafeExecutor.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\CommunicationException;
  13. use Predis\Network\IConnection;
  14. class SafeExecutor implements IPipelineExecutor
  15. {
  16. public function execute(IConnection $connection, &$commands)
  17. {
  18. $sizeofPipe = count($commands);
  19. $values = array();
  20. foreach ($commands as $command) {
  21. try {
  22. $connection->writeCommand($command);
  23. }
  24. catch (CommunicationException $exception) {
  25. return array_fill(0, $sizeofPipe, $exception);
  26. }
  27. }
  28. for ($i = 0; $i < $sizeofPipe; $i++) {
  29. $command = $commands[$i];
  30. unset($commands[$i]);
  31. try {
  32. $response = $connection->readResponse($command);
  33. $values[] = $response instanceof \Iterator ? iterator_to_array($response) : $response;
  34. }
  35. catch (ServerException $exception) {
  36. $values[] = $exception->toResponseError();
  37. }
  38. catch (CommunicationException $exception) {
  39. $toAdd = count($commands) - count($values);
  40. $values = array_merge($values, array_fill(0, $toAdd, $exception));
  41. break;
  42. }
  43. }
  44. return $values;
  45. }
  46. }