MultiExecExecutor.php 3.7 KB

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