MultiExecExecutor.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\ClientException;
  13. use Predis\ResponseErrorInterface;
  14. use Predis\ResponseObjectInterface;
  15. use Predis\ServerException;
  16. use Predis\Connection\ConnectionInterface;
  17. use Predis\Connection\SingleConnectionInterface;
  18. use Predis\Profile\ServerProfile;
  19. use Predis\Profile\ServerProfileInterface;
  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(ServerProfileInterface $profile = null)
  34. {
  35. $this->setProfile($profile ?: 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 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. $this->checkConnection($connection);
  57. $cmd = $this->profile->createCommand('multi');
  58. $connection->executeCommand($cmd);
  59. foreach ($commands as $command) {
  60. $connection->writeCommand($command);
  61. }
  62. foreach ($commands as $command) {
  63. $response = $connection->readResponse($command);
  64. if ($response instanceof ResponseErrorInterface) {
  65. $cmd = $this->profile->createCommand('discard');
  66. $connection->executeCommand($cmd);
  67. throw new ServerException($response->getMessage());
  68. }
  69. }
  70. $cmd = $this->profile->createCommand('exec');
  71. $responses = $connection->executeCommand($cmd);
  72. if (!isset($responses)) {
  73. throw new ClientException('The underlying transaction has been aborted by the server');
  74. }
  75. if (count($responses) !== count($commands)) {
  76. throw new ClientException("Invalid number of replies [expected: ".count($commands)." - actual: ".count($responses)."]");
  77. }
  78. return $this->consumeArrayResponse($commands, $responses);
  79. }
  80. /**
  81. * Consumes an array response returned by EXEC.
  82. *
  83. * @param SplQueue $commands Pipelined commands
  84. * @param Array $responses Responses returned by EXEC.
  85. * @return array
  86. */
  87. protected function consumeArrayResponse(SplQueue $commands, Array &$responses)
  88. {
  89. $size = count($commands);
  90. $values = array();
  91. for ($i = 0; $i < $size; $i++) {
  92. $command = $commands->dequeue();
  93. $response = $responses[$i];
  94. if ($response instanceof ResponseObjectInterface) {
  95. $values[$i] = $response;
  96. } else {
  97. $values[$i] = $command->parseResponse($response);
  98. }
  99. unset($responses[$i]);
  100. }
  101. return $values;
  102. }
  103. /**
  104. * @param ServerProfileInterface $profile Server profile.
  105. */
  106. public function setProfile(ServerProfileInterface $profile)
  107. {
  108. if (!$profile->supportsCommands(array('multi', 'exec', 'discard'))) {
  109. throw new ClientException('The specified server profile must support MULTI, EXEC and DISCARD.');
  110. }
  111. $this->profile = $profile;
  112. }
  113. }