SafeExecutor.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Predis\Pipeline;
  3. use Predis\ServerException;
  4. use Predis\CommunicationException;
  5. use Predis\Network\IConnection;
  6. class SafeExecutor implements IPipelineExecutor
  7. {
  8. public function execute(IConnection $connection, &$commands)
  9. {
  10. $sizeofPipe = count($commands);
  11. $values = array();
  12. foreach ($commands as $command) {
  13. try {
  14. $connection->writeCommand($command);
  15. }
  16. catch (CommunicationException $exception) {
  17. return array_fill(0, $sizeofPipe, $exception);
  18. }
  19. }
  20. for ($i = 0; $i < $sizeofPipe; $i++) {
  21. $command = $commands[$i];
  22. unset($commands[$i]);
  23. try {
  24. $response = $connection->readResponse($command);
  25. $values[] = $response instanceof \Iterator ? iterator_to_array($response) : $response;
  26. }
  27. catch (ServerException $exception) {
  28. $values[] = $exception->toResponseError();
  29. }
  30. catch (CommunicationException $exception) {
  31. $toAdd = count($commands) - count($values);
  32. $values = array_merge($values, array_fill(0, $toAdd, $exception));
  33. break;
  34. }
  35. }
  36. return $values;
  37. }
  38. }