SafeExecutor.php 1.3 KB

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