PipelineContext.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Client;
  12. use Predis\Helpers;
  13. use Predis\ClientException;
  14. use Predis\Commands\ICommand;
  15. /**
  16. * Abstraction of a pipeline context where write and read operations
  17. * of commands and their replies over the network are pipelined.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class PipelineContext
  22. {
  23. private $client;
  24. private $executor;
  25. private $pipeline = array();
  26. private $replies = array();
  27. private $running = false;
  28. /**
  29. * @param Client Client instance used by the context.
  30. * @param array Options for the context initialization.
  31. */
  32. public function __construct(Client $client, Array $options = null)
  33. {
  34. $this->client = $client;
  35. $this->executor = $this->createExecutor($client, $options ?: array());
  36. }
  37. /**
  38. * Returns a pipeline executor depending on the kind of the underlying
  39. * connection and the passed options.
  40. *
  41. * @param Client Client instance used by the context.
  42. * @param array Options for the context initialization.
  43. * @return IPipelineExecutor
  44. */
  45. protected function createExecutor(Client $client, Array $options)
  46. {
  47. if (!$options) {
  48. return new StandardExecutor();
  49. }
  50. if (isset($options['executor'])) {
  51. $executor = $options['executor'];
  52. if (!$executor instanceof IPipelineExecutor) {
  53. throw new \InvalidArgumentException(
  54. 'The executor option accepts only instances ' .
  55. 'of Predis\Pipeline\IPipelineExecutor'
  56. );
  57. }
  58. return $executor;
  59. }
  60. if (isset($options['safe']) && $options['safe'] == true) {
  61. $isCluster = Helpers::isCluster($client->getConnection());
  62. return $isCluster ? new SafeClusterExecutor() : new SafeExecutor();
  63. }
  64. return new StandardExecutor();
  65. }
  66. /**
  67. * Queues a command into the pipeline buffer.
  68. *
  69. * @param string $method Command ID.
  70. * @param array $arguments Arguments for the command.
  71. * @return PipelineContext
  72. */
  73. public function __call($method, $arguments)
  74. {
  75. $command = $this->client->createCommand($method, $arguments);
  76. $this->recordCommand($command);
  77. return $this;
  78. }
  79. /**
  80. * Queues a command instance into the pipeline buffer.
  81. *
  82. * @param ICommand $command Command to queue in the buffer.
  83. */
  84. protected function recordCommand(ICommand $command)
  85. {
  86. $this->pipeline[] = $command;
  87. }
  88. /**
  89. * Queues a command instance into the pipeline buffer.
  90. *
  91. * @param ICommand $command Command to queue in the buffer.
  92. */
  93. public function executeCommand(ICommand $command)
  94. {
  95. $this->recordCommand($command);
  96. }
  97. /**
  98. * Flushes the buffer that holds the queued commands.
  99. *
  100. * @param Boolean $send Specifies if the commands in the buffer should be sent to Redis.
  101. * @return PipelineContext
  102. */
  103. public function flushPipeline($send = true)
  104. {
  105. if (count($this->pipeline) > 0) {
  106. if ($send) {
  107. $connection = $this->client->getConnection();
  108. $replies = $this->executor->execute($connection, $this->pipeline);
  109. $this->replies = array_merge($this->replies, $replies);
  110. }
  111. $this->pipeline = array();
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Marks the running status of the pipeline.
  117. *
  118. * @param Boolean $bool True if the pipeline is running.
  119. * False if the pipeline is not running.
  120. */
  121. private function setRunning($bool)
  122. {
  123. if ($bool === true && $this->running === true) {
  124. throw new ClientException("This pipeline is already opened");
  125. }
  126. $this->running = $bool;
  127. }
  128. /**
  129. * Handles the actual execution of the whole pipeline.
  130. *
  131. * @param mixed $callable Callback for execution.
  132. * @return array
  133. */
  134. public function execute($callable = null)
  135. {
  136. if ($callable && !is_callable($callable)) {
  137. throw new \InvalidArgumentException('Argument passed must be a callable object');
  138. }
  139. $this->setRunning(true);
  140. $pipelineBlockException = null;
  141. try {
  142. if ($callable !== null) {
  143. call_user_func($callable, $this);
  144. }
  145. $this->flushPipeline();
  146. }
  147. catch (\Exception $exception) {
  148. $pipelineBlockException = $exception;
  149. }
  150. $this->setRunning(false);
  151. if ($pipelineBlockException !== null) {
  152. throw $pipelineBlockException;
  153. }
  154. return $this->replies;
  155. }
  156. /**
  157. * Returns the underlying client instance used by the pipeline object.
  158. *
  159. * @return Client
  160. */
  161. public function getClient()
  162. {
  163. return $this->client;
  164. }
  165. /**
  166. * Returns the underlying pipeline executor used by the pipeline object.
  167. *
  168. * @return IPipelineExecutor
  169. */
  170. public function getExecutor()
  171. {
  172. return $this->executor;
  173. }
  174. }