PipelineContext.php 5.6 KB

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