MultiExecExecutor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ResponseQueued) {
  67. if ($response instanceof ResponseErrorInterface) {
  68. $cmd = $this->profile->createCommand('discard');
  69. $connection->executeCommand($cmd);
  70. throw new ServerException($response->getMessage());
  71. }
  72. }
  73. }
  74. $cmd = $this->profile->createCommand('exec');
  75. $responses = $connection->executeCommand($cmd);
  76. if (!isset($responses)) {
  77. throw new ClientException('The underlying transaction has been aborted by the server');
  78. }
  79. if (count($responses) !== $size) {
  80. throw new ClientException("Invalid number of replies [expected: $size - actual: ".count($responses)."]");
  81. }
  82. for ($i = 0; $i < $size; $i++) {
  83. if ($response = $responses[$i] instanceof \Iterator) {
  84. $response = iterator_to_array($response);
  85. }
  86. $values[$i] = $commands->dequeue()->parseResponse($responses[$i]);
  87. unset($responses[$i]);
  88. }
  89. return $values;
  90. }
  91. /**
  92. * @param ServerProfileInterface $profile Server profile.
  93. */
  94. public function setProfile(ServerProfileInterface $profile)
  95. {
  96. if (!$profile->supportsCommands(array('multi', 'exec', 'discard'))) {
  97. throw new ClientException('The specified server profile must support MULTI, EXEC and DISCARD.');
  98. }
  99. $this->profile = $profile;
  100. }
  101. }