PipelineContext.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Predis\ClientInterface;
  12. use Predis\BasicClientInterface;
  13. use Predis\ExecutableContextInterface;
  14. use Predis\Command\CommandInterface;
  15. use Predis\Helpers;
  16. use Predis\ClientException;
  17. /**
  18. * Abstraction of a pipeline context where write and read operations
  19. * of commands and their replies over the network are pipelined.
  20. *
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class PipelineContext implements BasicClientInterface, ExecutableContextInterface
  24. {
  25. private $client;
  26. private $executor;
  27. private $pipeline = array();
  28. private $replies = array();
  29. private $running = false;
  30. /**
  31. * @param ClientInterface Client instance used by the context.
  32. * @param array Options for the context initialization.
  33. */
  34. public function __construct(ClientInterface $client, Array $options = null)
  35. {
  36. $this->client = $client;
  37. $this->executor = $this->createExecutor($client, $options ?: array());
  38. }
  39. /**
  40. * Returns a pipeline executor depending on the kind of the underlying
  41. * connection and the passed options.
  42. *
  43. * @param ClientInterface Client instance used by the context.
  44. * @param array Options for the context initialization.
  45. * @return PipelineExecutorInterface
  46. */
  47. protected function createExecutor(ClientInterface $client, Array $options)
  48. {
  49. if (isset($options['executor'])) {
  50. $executor = $options['executor'];
  51. if (is_callable($executor)) {
  52. $executor = call_user_func($executor, $client, $options);
  53. }
  54. if (!$executor instanceof PipelineExecutorInterface) {
  55. $message = 'The executor option accepts only instances of Predis\Pipeline\PipelineExecutorInterface';
  56. throw new \InvalidArgumentException($message);
  57. }
  58. return $executor;
  59. }
  60. if (isset($options['safe']) && $options['safe'] == true) {
  61. $isCluster = Helpers::isCluster($client->getConnection());
  62. return $isCluster ? new SafeClusterExecutor() : new SafeExecutor();
  63. }
  64. $clientOpts = $client->getOptions();
  65. $useExceptions = isset($clientOpts->exceptions) ? $clientOpts->exceptions : true;
  66. $executor = new StandardExecutor($useExceptions);
  67. return $executor;
  68. }
  69. /**
  70. * Queues a command into the pipeline buffer.
  71. *
  72. * @param string $method Command ID.
  73. * @param array $arguments Arguments for the command.
  74. * @return PipelineContext
  75. */
  76. public function __call($method, $arguments)
  77. {
  78. $command = $this->client->createCommand($method, $arguments);
  79. $this->recordCommand($command);
  80. return $this;
  81. }
  82. /**
  83. * Queues a command instance into the pipeline buffer.
  84. *
  85. * @param CommandInterface $command Command to queue in the buffer.
  86. */
  87. protected function recordCommand(CommandInterface $command)
  88. {
  89. $this->pipeline[] = $command;
  90. }
  91. /**
  92. * Queues a command instance into the pipeline buffer.
  93. *
  94. * @param CommandInterface $command Command to queue in the buffer.
  95. */
  96. public function executeCommand(CommandInterface $command)
  97. {
  98. $this->recordCommand($command);
  99. }
  100. /**
  101. * Flushes the buffer that holds the queued commands.
  102. *
  103. * @param Boolean $send Specifies if the commands in the buffer should be sent to Redis.
  104. * @return PipelineContext
  105. */
  106. public function flushPipeline($send = true)
  107. {
  108. if (count($this->pipeline) > 0) {
  109. if ($send) {
  110. $connection = $this->client->getConnection();
  111. $replies = $this->executor->execute($connection, $this->pipeline);
  112. $this->replies = array_merge($this->replies, $replies);
  113. }
  114. $this->pipeline = array();
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Marks the running status of the pipeline.
  120. *
  121. * @param Boolean $bool True if the pipeline is running.
  122. * False if the pipeline is not running.
  123. */
  124. private function setRunning($bool)
  125. {
  126. if ($bool === true && $this->running === true) {
  127. throw new ClientException("This pipeline is already opened");
  128. }
  129. $this->running = $bool;
  130. }
  131. /**
  132. * Handles the actual execution of the whole pipeline.
  133. *
  134. * @param mixed $callable Optional callback for execution.
  135. * @return array
  136. */
  137. public function execute($callable = null)
  138. {
  139. if ($callable && !is_callable($callable)) {
  140. throw new \InvalidArgumentException('Argument passed must be a callable object');
  141. }
  142. $this->setRunning(true);
  143. $pipelineBlockException = null;
  144. try {
  145. if ($callable !== null) {
  146. call_user_func($callable, $this);
  147. }
  148. $this->flushPipeline();
  149. }
  150. catch (\Exception $exception) {
  151. $pipelineBlockException = $exception;
  152. }
  153. $this->setRunning(false);
  154. if ($pipelineBlockException !== null) {
  155. throw $pipelineBlockException;
  156. }
  157. return $this->replies;
  158. }
  159. /**
  160. * Returns the underlying client instance used by the pipeline object.
  161. *
  162. * @return ClientInterface
  163. */
  164. public function getClient()
  165. {
  166. return $this->client;
  167. }
  168. /**
  169. * Returns the underlying pipeline executor used by the pipeline object.
  170. *
  171. * @return PipelineExecutorInterface
  172. */
  173. public function getExecutor()
  174. {
  175. return $this->executor;
  176. }
  177. }