MultiExecExecutor.php 3.8 KB

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