PipelineContext.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 (!$options) {
  50. return new StandardExecutor();
  51. }
  52. if (isset($options['executor'])) {
  53. $executor = $options['executor'];
  54. if (!$executor instanceof PipelineExecutorInterface) {
  55. throw new \InvalidArgumentException(
  56. 'The executor option accepts only instances ' .
  57. 'of Predis\Pipeline\PipelineExecutorInterface'
  58. );
  59. }
  60. return $executor;
  61. }
  62. if (isset($options['safe']) && $options['safe'] == true) {
  63. $isCluster = Helpers::isCluster($client->getConnection());
  64. return $isCluster ? new SafeClusterExecutor() : new SafeExecutor();
  65. }
  66. return new StandardExecutor();
  67. }
  68. /**
  69. * Queues a command into the pipeline buffer.
  70. *
  71. * @param string $method Command ID.
  72. * @param array $arguments Arguments for the command.
  73. * @return PipelineContext
  74. */
  75. public function __call($method, $arguments)
  76. {
  77. $command = $this->client->createCommand($method, $arguments);
  78. $this->recordCommand($command);
  79. return $this;
  80. }
  81. /**
  82. * Queues a command instance into the pipeline buffer.
  83. *
  84. * @param CommandInterface $command Command to queue in the buffer.
  85. */
  86. protected function recordCommand(CommandInterface $command)
  87. {
  88. $this->pipeline[] = $command;
  89. }
  90. /**
  91. * Queues a command instance into the pipeline buffer.
  92. *
  93. * @param CommandInterface $command Command to queue in the buffer.
  94. */
  95. public function executeCommand(CommandInterface $command)
  96. {
  97. $this->recordCommand($command);
  98. }
  99. /**
  100. * Flushes the buffer that holds the queued commands.
  101. *
  102. * @param Boolean $send Specifies if the commands in the buffer should be sent to Redis.
  103. * @return PipelineContext
  104. */
  105. public function flushPipeline($send = true)
  106. {
  107. if (count($this->pipeline) > 0) {
  108. if ($send) {
  109. $connection = $this->client->getConnection();
  110. $replies = $this->executor->execute($connection, $this->pipeline);
  111. $this->replies = array_merge($this->replies, $replies);
  112. }
  113. $this->pipeline = array();
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Marks the running status of the pipeline.
  119. *
  120. * @param Boolean $bool True if the pipeline is running.
  121. * False if the pipeline is not running.
  122. */
  123. private function setRunning($bool)
  124. {
  125. if ($bool === true && $this->running === true) {
  126. throw new ClientException("This pipeline is already opened");
  127. }
  128. $this->running = $bool;
  129. }
  130. /**
  131. * Handles the actual execution of the whole pipeline.
  132. *
  133. * @param mixed $callable Optional callback for execution.
  134. * @return array
  135. */
  136. public function execute($callable = null)
  137. {
  138. if ($callable && !is_callable($callable)) {
  139. throw new \InvalidArgumentException('Argument passed must be a callable object');
  140. }
  141. $this->setRunning(true);
  142. $pipelineBlockException = null;
  143. try {
  144. if ($callable !== null) {
  145. call_user_func($callable, $this);
  146. }
  147. $this->flushPipeline();
  148. }
  149. catch (\Exception $exception) {
  150. $pipelineBlockException = $exception;
  151. }
  152. $this->setRunning(false);
  153. if ($pipelineBlockException !== null) {
  154. throw $pipelineBlockException;
  155. }
  156. return $this->replies;
  157. }
  158. /**
  159. * Returns the underlying client instance used by the pipeline object.
  160. *
  161. * @return ClientInterface
  162. */
  163. public function getClient()
  164. {
  165. return $this->client;
  166. }
  167. /**
  168. * Returns the underlying pipeline executor used by the pipeline object.
  169. *
  170. * @return PipelineExecutorInterface
  171. */
  172. public function getExecutor()
  173. {
  174. return $this->executor;
  175. }
  176. }