PipelineContext.php 5.6 KB

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