MultiExecExecutor.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 SplQueue;
  12. use Predis\ResponseErrorInterface;
  13. use Predis\Profile\ServerProfileInterface;
  14. use Predis\Connection\ConnectionInterface;
  15. use Predis\Connection\SingleConnectionInterface;
  16. use Predis\ResponseQueued;
  17. use Predis\ClientException;
  18. use Predis\ServerException;
  19. use Predis\Profile\ServerProfile;
  20. /**
  21. * Implements a pipeline executor that wraps the whole pipeline
  22. * in a MULTI / EXEC context to make sure that it is executed
  23. * correctly.
  24. *
  25. * @author Daniele Alessandri <suppakilla@gmail.com>
  26. */
  27. class MultiExecExecutor implements PipelineExecutorInterface
  28. {
  29. protected $profile;
  30. /**
  31. *
  32. */
  33. public function __construct()
  34. {
  35. $this->setProfile(ServerProfile::getDefault());
  36. }
  37. /**
  38. * Allows the pipeline executor to perform operations on the
  39. * connection before starting to execute the commands stored
  40. * in the pipeline.
  41. *
  42. * @param ConnectionInterface Connection instance.
  43. */
  44. protected function checkConnection(ConnectionInterface $connection)
  45. {
  46. if (!$connection instanceof SingleConnectionInterface) {
  47. $class = __CLASS__;
  48. throw new ClientException("$class can be used only with single connections");
  49. }
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function execute(ConnectionInterface $connection, SplQueue $commands)
  55. {
  56. $size = count($commands);
  57. $values = array();
  58. $this->checkConnection($connection);
  59. $cmd = $this->profile->createCommand('multi');
  60. $connection->executeCommand($cmd);
  61. foreach ($commands as $command) {
  62. $connection->writeCommand($command);
  63. }
  64. foreach ($commands as $command) {
  65. $response = $connection->readResponse($command);
  66. if ($response instanceof ResponseErrorInterface) {
  67. $cmd = $this->profile->createCommand('discard');
  68. $connection->executeCommand($cmd);
  69. throw new ServerException($response->getMessage());
  70. }
  71. }
  72. $cmd = $this->profile->createCommand('exec');
  73. $responses = $connection->executeCommand($cmd);
  74. if (!isset($responses)) {
  75. throw new ClientException('The underlying transaction has been aborted by the server');
  76. }
  77. if (count($responses) !== $size) {
  78. throw new ClientException("Invalid number of replies [expected: $size - actual: ".count($responses)."]");
  79. }
  80. for ($i = 0; $i < $size; $i++) {
  81. if ($response = $responses[$i] instanceof \Iterator) {
  82. $response = iterator_to_array($response);
  83. }
  84. $values[$i] = $commands->dequeue()->parseResponse($responses[$i]);
  85. unset($responses[$i]);
  86. }
  87. return $values;
  88. }
  89. /**
  90. * @param ServerProfileInterface $profile Server profile.
  91. */
  92. public function setProfile(ServerProfileInterface $profile)
  93. {
  94. if (!$profile->supportsCommands(array('multi', 'exec', 'discard'))) {
  95. throw new ClientException('The specified server profile must support MULTI, EXEC and DISCARD.');
  96. }
  97. $this->profile = $profile;
  98. }
  99. }