PipelineContext.php 5.8 KB

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