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