SafeClusterExecutor.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Predis\Pipeline;
  3. use Predis\ServerException;
  4. use Predis\CommunicationException;
  5. use Predis\Network\IConnection;
  6. class SafeClusterExecutor implements IPipelineExecutor {
  7. public function execute(IConnection $connection, &$commands) {
  8. $connectionExceptions = array();
  9. $sizeofPipe = count($commands);
  10. $values = array();
  11. foreach ($commands as $command) {
  12. $cmdConnection = $connection->getConnection($command);
  13. if (isset($connectionExceptions[spl_object_hash($cmdConnection)])) {
  14. continue;
  15. }
  16. try {
  17. $cmdConnection->writeCommand($command);
  18. }
  19. catch (CommunicationException $exception) {
  20. $connectionExceptions[spl_object_hash($cmdConnection)] = $exception;
  21. }
  22. }
  23. for ($i = 0; $i < $sizeofPipe; $i++) {
  24. $command = $commands[$i];
  25. unset($commands[$i]);
  26. $cmdConnection = $connection->getConnection($command);
  27. $connectionObjectHash = spl_object_hash($cmdConnection);
  28. if (isset($connectionExceptions[$connectionObjectHash])) {
  29. $values[] = $connectionExceptions[$connectionObjectHash];
  30. continue;
  31. }
  32. try {
  33. $response = $cmdConnection->readResponse($command);
  34. $values[] = ($response instanceof \Iterator
  35. ? iterator_to_array($response)
  36. : $response
  37. );
  38. }
  39. catch (ServerException $exception) {
  40. $values[] = $exception->toResponseError();
  41. }
  42. catch (CommunicationException $exception) {
  43. $values[] = $exception;
  44. $connectionExceptions[$connectionObjectHash] = $exception;
  45. }
  46. }
  47. return $values;
  48. }
  49. }