PipelineContext.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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->getExecutor($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 getExecutor(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. protected function recordCommand(ICommand $command)
  83. {
  84. $this->pipeline[] = $command;
  85. }
  86. /**
  87. * Queues a command instance into the pipeline buffer.
  88. */
  89. public function executeCommand(ICommand $command)
  90. {
  91. $this->recordCommand($command);
  92. }
  93. /**
  94. * Flushes the queued commands by writing the buffer to Redis and reading
  95. * all the replies into the reply buffer.
  96. *
  97. * @return PipelineContext
  98. */
  99. public function flushPipeline()
  100. {
  101. if (count($this->pipeline) > 0) {
  102. $connection = $this->client->getConnection();
  103. $replies = $this->executor->execute($connection, $this->pipeline);
  104. $this->replies = array_merge($this->replies, $replies);
  105. $this->pipeline = array();
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Marks the running status of the pipeline.
  111. *
  112. * @param Boolean $bool True if the pipeline is running.
  113. * False if the pipeline is not running.
  114. */
  115. private function setRunning($bool)
  116. {
  117. if ($bool === true && $this->running === true) {
  118. throw new ClientException("This pipeline is already opened");
  119. }
  120. $this->running = $bool;
  121. }
  122. /**
  123. * Handles the actual execution of the whole pipeline.
  124. *
  125. * @param mixed $callable Callback for execution.
  126. * @return array
  127. */
  128. public function execute($callable = null)
  129. {
  130. if ($callable && !is_callable($callable)) {
  131. throw new \InvalidArgumentException('Argument passed must be a callable object');
  132. }
  133. $this->setRunning(true);
  134. $pipelineBlockException = null;
  135. try {
  136. if ($callable !== null) {
  137. $callable($this);
  138. }
  139. $this->flushPipeline();
  140. }
  141. catch (\Exception $exception) {
  142. $pipelineBlockException = $exception;
  143. }
  144. $this->setRunning(false);
  145. if ($pipelineBlockException !== null) {
  146. throw $pipelineBlockException;
  147. }
  148. return $this->replies;
  149. }
  150. }