MultiExecContext.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Predis;
  3. class MultiExecContext {
  4. private $_initialized, $_discarded, $_insideBlock, $_checkAndSet;
  5. private $_client, $_options, $_commands, $_supportsWatch;
  6. public function __construct(Client $client, Array $options = null) {
  7. $this->checkCapabilities($client);
  8. $this->_options = $options ?: array();
  9. $this->_client = $client;
  10. $this->reset();
  11. }
  12. private function checkCapabilities(Client $client) {
  13. if (Utils::isCluster($client->getConnection())) {
  14. throw new ClientException(
  15. 'Cannot initialize a MULTI/EXEC context over a cluster of connections'
  16. );
  17. }
  18. $profile = $client->getProfile();
  19. if ($profile->supportsCommands(array('multi', 'exec', 'discard')) === false) {
  20. throw new ClientException(
  21. 'The current profile does not support MULTI, EXEC and DISCARD commands'
  22. );
  23. }
  24. $this->_supportsWatch = $profile->supportsCommands(array('watch', 'unwatch'));
  25. }
  26. private function isWatchSupported() {
  27. if ($this->_supportsWatch === false) {
  28. throw new ClientException(
  29. 'The current profile does not support WATCH and UNWATCH commands'
  30. );
  31. }
  32. }
  33. private function reset() {
  34. $this->_initialized = false;
  35. $this->_discarded = false;
  36. $this->_checkAndSet = false;
  37. $this->_insideBlock = false;
  38. $this->_commands = array();
  39. }
  40. private function initialize() {
  41. if ($this->_initialized === true) {
  42. return;
  43. }
  44. $options = $this->_options;
  45. $this->_checkAndSet = isset($options['cas']) && $options['cas'];
  46. if (isset($options['watch'])) {
  47. $this->watch($options['watch']);
  48. }
  49. if (!$this->_checkAndSet || ($this->_discarded && $this->_checkAndSet)) {
  50. $this->_client->multi();
  51. if ($this->_discarded) {
  52. $this->_checkAndSet = false;
  53. }
  54. }
  55. $this->_initialized = true;
  56. $this->_discarded = false;
  57. }
  58. public function __call($method, $arguments) {
  59. $this->initialize();
  60. $client = $this->_client;
  61. if ($this->_checkAndSet) {
  62. return call_user_func_array(array($client, $method), $arguments);
  63. }
  64. $command = $client->createCommand($method, $arguments);
  65. $response = $client->executeCommand($command);
  66. if (!$response instanceof ResponseQueued) {
  67. $this->malformedServerResponse(
  68. 'The server did not respond with a QUEUED status reply'
  69. );
  70. }
  71. $this->_commands[] = $command;
  72. return $this;
  73. }
  74. public function watch($keys) {
  75. $this->isWatchSupported();
  76. if ($this->_initialized && !$this->_checkAndSet) {
  77. throw new ClientException('WATCH inside MULTI is not allowed');
  78. }
  79. return $this->_client->watch($keys);
  80. }
  81. public function multi() {
  82. if ($this->_initialized && $this->_checkAndSet) {
  83. $this->_checkAndSet = false;
  84. $this->_client->multi();
  85. return $this;
  86. }
  87. $this->initialize();
  88. return $this;
  89. }
  90. public function unwatch() {
  91. $this->isWatchSupported();
  92. $this->_client->unwatch();
  93. return $this;
  94. }
  95. public function discard() {
  96. $this->_client->discard();
  97. $this->reset();
  98. $this->_discarded = true;
  99. return $this;
  100. }
  101. public function exec() {
  102. return $this->execute();
  103. }
  104. private function checkBeforeExecution($block) {
  105. if ($this->_insideBlock === true) {
  106. throw new ClientException(
  107. "Cannot invoke 'execute' or 'exec' inside an active client transaction block"
  108. );
  109. }
  110. if ($block) {
  111. if (!is_callable($block)) {
  112. throw new \InvalidArgumentException(
  113. 'Argument passed must be a callable object'
  114. );
  115. }
  116. if (count($this->_commands) > 0) {
  117. throw new ClientException(
  118. 'Cannot execute a transaction block after using fluent interface'
  119. );
  120. }
  121. }
  122. if (isset($this->_options['retry']) && !isset($block)) {
  123. $this->discard();
  124. throw new \InvalidArgumentException(
  125. 'Automatic retries can be used only when a transaction block is provided'
  126. );
  127. }
  128. }
  129. public function execute($block = null) {
  130. $this->checkBeforeExecution($block);
  131. $reply = null;
  132. $returnValues = array();
  133. $attemptsLeft = isset($this->_options['retry']) ? (int)$this->_options['retry'] : 0;
  134. do {
  135. $blockException = null;
  136. if ($block !== null) {
  137. $this->_insideBlock = true;
  138. try {
  139. $block($this);
  140. }
  141. catch (CommunicationException $exception) {
  142. $blockException = $exception;
  143. }
  144. catch (ServerException $exception) {
  145. $blockException = $exception;
  146. }
  147. catch (\Exception $exception) {
  148. $blockException = $exception;
  149. if ($this->_initialized === true) {
  150. $this->discard();
  151. }
  152. }
  153. $this->_insideBlock = false;
  154. if ($blockException !== null) {
  155. throw $blockException;
  156. }
  157. }
  158. if ($this->_initialized === false || count($this->_commands) == 0) {
  159. return;
  160. }
  161. $reply = $this->_client->exec();
  162. if ($reply === null) {
  163. if ($attemptsLeft === 0) {
  164. throw new AbortedMultiExec(
  165. 'The current transaction has been aborted by the server'
  166. );
  167. }
  168. $this->reset();
  169. if (isset($this->_options['on_retry']) && is_callable($this->_options['on_retry'])) {
  170. call_user_func($this->_options['on_retry'], $this, $attemptsLeft);
  171. }
  172. continue;
  173. }
  174. break;
  175. } while ($attemptsLeft-- > 0);
  176. $execReply = $reply instanceof \Iterator ? iterator_to_array($reply) : $reply;
  177. $sizeofReplies = count($execReply);
  178. $commands = &$this->_commands;
  179. if ($sizeofReplies !== count($commands)) {
  180. $this->malformedServerResponse(
  181. 'Unexpected number of responses for a MultiExecContext'
  182. );
  183. }
  184. for ($i = 0; $i < $sizeofReplies; $i++) {
  185. $returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof \Iterator
  186. ? iterator_to_array($execReply[$i])
  187. : $execReply[$i]
  188. );
  189. unset($commands[$i]);
  190. }
  191. return $returnValues;
  192. }
  193. private function malformedServerResponse($message) {
  194. // Since a MULTI/EXEC block cannot be initialized over a clustered
  195. // connection, we can safely assume that Predis\Client::getConnection()
  196. // will always return an instance of Predis\Connection.
  197. Utils::onCommunicationException(new MalformedServerResponse(
  198. $this->_client->getConnection(), $message
  199. ));
  200. }
  201. }