PipelineContext.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. class PipelineContext
  16. {
  17. private $_client;
  18. private $_executor;
  19. private $_pipeline = array();
  20. private $_replies = array();
  21. private $_running = false;
  22. public function __construct(Client $client, Array $options = null)
  23. {
  24. $this->_client = $client;
  25. $this->_executor = $this->getExecutor($client, $options ?: array());
  26. }
  27. protected function getExecutor(Client $client, Array $options)
  28. {
  29. if (!$options) {
  30. return new StandardExecutor();
  31. }
  32. if (isset($options['executor'])) {
  33. $executor = $options['executor'];
  34. if (!$executor instanceof IPipelineExecutor) {
  35. throw new \InvalidArgumentException(
  36. 'The executor option accepts only instances ' .
  37. 'of Predis\Pipeline\IPipelineExecutor'
  38. );
  39. }
  40. return $executor;
  41. }
  42. if (isset($options['safe']) && $options['safe'] == true) {
  43. $isCluster = Helpers::isCluster($client->getConnection());
  44. return $isCluster ? new SafeClusterExecutor() : new SafeExecutor();
  45. }
  46. return new StandardExecutor();
  47. }
  48. public function __call($method, $arguments)
  49. {
  50. $command = $this->_client->createCommand($method, $arguments);
  51. $this->recordCommand($command);
  52. return $this;
  53. }
  54. protected function recordCommand(ICommand $command)
  55. {
  56. $this->_pipeline[] = $command;
  57. }
  58. public function executeCommand(ICommand $command)
  59. {
  60. $this->recordCommand($command);
  61. }
  62. public function flushPipeline()
  63. {
  64. if (count($this->_pipeline) > 0) {
  65. $connection = $this->_client->getConnection();
  66. $replies = $this->_executor->execute($connection, $this->_pipeline);
  67. $this->_replies = array_merge($this->_replies, $replies);
  68. $this->_pipeline = array();
  69. }
  70. return $this;
  71. }
  72. private function setRunning($bool)
  73. {
  74. if ($bool === true && $this->_running === true) {
  75. throw new ClientException("This pipeline is already opened");
  76. }
  77. $this->_running = $bool;
  78. }
  79. public function execute($block = null)
  80. {
  81. if ($block && !is_callable($block)) {
  82. throw new \InvalidArgumentException('Argument passed must be a callable object');
  83. }
  84. $this->setRunning(true);
  85. $pipelineBlockException = null;
  86. try {
  87. if ($block !== null) {
  88. $block($this);
  89. }
  90. $this->flushPipeline();
  91. }
  92. catch (\Exception $exception) {
  93. $pipelineBlockException = $exception;
  94. }
  95. $this->setRunning(false);
  96. if ($pipelineBlockException !== null) {
  97. throw $pipelineBlockException;
  98. }
  99. return $this->_replies;
  100. }
  101. }