PipelineContext.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\Connection\ReplicationConnectionInterface;
  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 = array();
  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. }
  40. /**
  41. * Returns a pipeline executor depending on the kind of the underlying
  42. * connection and the passed options.
  43. *
  44. * @param ClientInterface Client instance used by the context.
  45. * @param array Options for the context initialization.
  46. * @return PipelineExecutorInterface
  47. */
  48. protected function createExecutor(ClientInterface $client, Array $options)
  49. {
  50. if (!$options) {
  51. return new StandardExecutor();
  52. }
  53. if (isset($options['executor'])) {
  54. $executor = $options['executor'];
  55. if (!$executor instanceof PipelineExecutorInterface) {
  56. throw new \InvalidArgumentException(
  57. 'The executor option accepts only instances ' .
  58. 'of Predis\Pipeline\PipelineExecutorInterface'
  59. );
  60. }
  61. return $executor;
  62. }
  63. if (isset($options['safe']) && $options['safe'] == true) {
  64. $isCluster = Helpers::isCluster($client->getConnection());
  65. return $isCluster ? new SafeClusterExecutor() : new SafeExecutor();
  66. }
  67. return new StandardExecutor();
  68. }
  69. /**
  70. * Queues a command into the pipeline buffer.
  71. *
  72. * @param string $method Command ID.
  73. * @param array $arguments Arguments for the command.
  74. * @return PipelineContext
  75. */
  76. public function __call($method, $arguments)
  77. {
  78. $command = $this->client->createCommand($method, $arguments);
  79. $this->recordCommand($command);
  80. return $this;
  81. }
  82. /**
  83. * Queues a command instance into the pipeline buffer.
  84. *
  85. * @param CommandInterface $command Command to queue in the buffer.
  86. */
  87. protected function recordCommand(CommandInterface $command)
  88. {
  89. $this->pipeline[] = $command;
  90. }
  91. /**
  92. * Queues a command instance into the pipeline buffer.
  93. *
  94. * @param CommandInterface $command Command to queue in the buffer.
  95. */
  96. public function executeCommand(CommandInterface $command)
  97. {
  98. $this->recordCommand($command);
  99. }
  100. /**
  101. * Flushes the buffer that holds the queued commands.
  102. *
  103. * @param Boolean $send Specifies if the commands in the buffer should be sent to Redis.
  104. * @return PipelineContext
  105. */
  106. public function flushPipeline($send = true)
  107. {
  108. if (count($this->pipeline) > 0) {
  109. if ($send) {
  110. $connection = $this->client->getConnection();
  111. // TODO: it would be better to use a dedicated pipeline executor
  112. // for classes implementing master/slave replication.
  113. if ($connection instanceof ReplicationConnectionInterface) {
  114. $connection->switchTo('master');
  115. }
  116. $replies = $this->executor->execute($connection, $this->pipeline);
  117. $this->replies = array_merge($this->replies, $replies);
  118. }
  119. $this->pipeline = array();
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Marks the running status of the pipeline.
  125. *
  126. * @param Boolean $bool True if the pipeline is running.
  127. * False if the pipeline is not running.
  128. */
  129. private function setRunning($bool)
  130. {
  131. if ($bool === true && $this->running === true) {
  132. throw new ClientException("This pipeline is already opened");
  133. }
  134. $this->running = $bool;
  135. }
  136. /**
  137. * Handles the actual execution of the whole pipeline.
  138. *
  139. * @param mixed $callable Optional callback for execution.
  140. * @return array
  141. */
  142. public function execute($callable = null)
  143. {
  144. if ($callable && !is_callable($callable)) {
  145. throw new \InvalidArgumentException('Argument passed must be a callable object');
  146. }
  147. $this->setRunning(true);
  148. $pipelineBlockException = null;
  149. try {
  150. if ($callable !== null) {
  151. call_user_func($callable, $this);
  152. }
  153. $this->flushPipeline();
  154. }
  155. catch (\Exception $exception) {
  156. $pipelineBlockException = $exception;
  157. }
  158. $this->setRunning(false);
  159. if ($pipelineBlockException !== null) {
  160. throw $pipelineBlockException;
  161. }
  162. return $this->replies;
  163. }
  164. /**
  165. * Returns the underlying client instance used by the pipeline object.
  166. *
  167. * @return ClientInterface
  168. */
  169. public function getClient()
  170. {
  171. return $this->client;
  172. }
  173. /**
  174. * Returns the underlying pipeline executor used by the pipeline object.
  175. *
  176. * @return PipelineExecutorInterface
  177. */
  178. public function getExecutor()
  179. {
  180. return $this->executor;
  181. }
  182. }