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