PipelineContext.php 5.1 KB

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