SafeClusterExecutor.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 SafeClusterExecutor implements IPipelineExecutor
  15. {
  16. public function execute(IConnection $connection, &$commands)
  17. {
  18. $connectionExceptions = array();
  19. $sizeofPipe = count($commands);
  20. $values = array();
  21. foreach ($commands as $command) {
  22. $cmdConnection = $connection->getConnection($command);
  23. if (isset($connectionExceptions[spl_object_hash($cmdConnection)])) {
  24. continue;
  25. }
  26. try {
  27. $cmdConnection->writeCommand($command);
  28. }
  29. catch (CommunicationException $exception) {
  30. $connectionExceptions[spl_object_hash($cmdConnection)] = $exception;
  31. }
  32. }
  33. for ($i = 0; $i < $sizeofPipe; $i++) {
  34. $command = $commands[$i];
  35. unset($commands[$i]);
  36. $cmdConnection = $connection->getConnection($command);
  37. $connectionObjectHash = spl_object_hash($cmdConnection);
  38. if (isset($connectionExceptions[$connectionObjectHash])) {
  39. $values[] = $connectionExceptions[$connectionObjectHash];
  40. continue;
  41. }
  42. try {
  43. $response = $cmdConnection->readResponse($command);
  44. $values[] = $response instanceof \Iterator ? iterator_to_array($response) : $response;
  45. }
  46. catch (ServerException $exception) {
  47. $values[] = $exception->toResponseError();
  48. }
  49. catch (CommunicationException $exception) {
  50. $values[] = $exception;
  51. $connectionExceptions[$connectionObjectHash] = $exception;
  52. }
  53. }
  54. return $values;
  55. }
  56. }