MultiExecContext.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if ($this->_initialized) {
  97. $this->_client->discard();
  98. $this->reset();
  99. $this->_discarded = true;
  100. }
  101. return $this;
  102. }
  103. public function exec() {
  104. return $this->execute();
  105. }
  106. private function checkBeforeExecution($block) {
  107. if ($this->_insideBlock === true) {
  108. throw new ClientException(
  109. "Cannot invoke 'execute' or 'exec' inside an active client transaction block"
  110. );
  111. }
  112. if ($block) {
  113. if (!is_callable($block)) {
  114. throw new \InvalidArgumentException(
  115. 'Argument passed must be a callable object'
  116. );
  117. }
  118. if (count($this->_commands) > 0) {
  119. $this->discard();
  120. throw new ClientException(
  121. 'Cannot execute a transaction block after using fluent interface'
  122. );
  123. }
  124. }
  125. if (isset($this->_options['retry']) && !isset($block)) {
  126. $this->discard();
  127. throw new \InvalidArgumentException(
  128. 'Automatic retries can be used only when a transaction block is provided'
  129. );
  130. }
  131. }
  132. public function execute($block = null) {
  133. $this->checkBeforeExecution($block);
  134. $reply = null;
  135. $returnValues = array();
  136. $attemptsLeft = isset($this->_options['retry']) ? (int)$this->_options['retry'] : 0;
  137. do {
  138. $blockException = null;
  139. if ($block !== null) {
  140. $this->_insideBlock = true;
  141. try {
  142. $block($this);
  143. }
  144. catch (CommunicationException $exception) {
  145. $blockException = $exception;
  146. }
  147. catch (ServerException $exception) {
  148. $blockException = $exception;
  149. }
  150. catch (\Exception $exception) {
  151. $blockException = $exception;
  152. if ($this->_initialized === true) {
  153. $this->discard();
  154. }
  155. }
  156. $this->_insideBlock = false;
  157. if ($blockException !== null) {
  158. throw $blockException;
  159. }
  160. }
  161. if ($this->_initialized === false || count($this->_commands) == 0) {
  162. return;
  163. }
  164. $reply = $this->_client->exec();
  165. if ($reply === null) {
  166. if ($attemptsLeft === 0) {
  167. throw new AbortedMultiExec(
  168. 'The current transaction has been aborted by the server'
  169. );
  170. }
  171. $this->reset();
  172. if (isset($this->_options['on_retry']) && is_callable($this->_options['on_retry'])) {
  173. call_user_func($this->_options['on_retry'], $this, $attemptsLeft);
  174. }
  175. continue;
  176. }
  177. break;
  178. } while ($attemptsLeft-- > 0);
  179. $execReply = $reply instanceof \Iterator ? iterator_to_array($reply) : $reply;
  180. $sizeofReplies = count($execReply);
  181. $commands = &$this->_commands;
  182. if ($sizeofReplies !== count($commands)) {
  183. $this->malformedServerResponse(
  184. 'Unexpected number of responses for a MultiExecContext'
  185. );
  186. }
  187. for ($i = 0; $i < $sizeofReplies; $i++) {
  188. $returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof \Iterator
  189. ? iterator_to_array($execReply[$i])
  190. : $execReply[$i]
  191. );
  192. unset($commands[$i]);
  193. }
  194. return $returnValues;
  195. }
  196. private function malformedServerResponse($message) {
  197. // Since a MULTI/EXEC block cannot be initialized over a clustered
  198. // connection, we can safely assume that Predis\Client::getConnection()
  199. // will always return an instance of Predis\Network\IConnectionSingle.
  200. Utils::onCommunicationException(new MalformedServerResponse(
  201. $this->_client->getConnection(), $message
  202. ));
  203. }
  204. }