PipelineContext.php 5.0 KB

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