123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- namespace Predis\Pipeline;
- use Exception;
- use InvalidArgumentException;
- use SplQueue;
- use Predis\BasicClientInterface;
- use Predis\ClientException;
- use Predis\ClientInterface;
- use Predis\ExecutableContextInterface;
- use Predis\Command\CommandInterface;
- use Predis\Connection\ConnectionInterface;
- use Predis\Connection\ReplicationConnectionInterface;
- use Predis\Response;
- class Pipeline implements BasicClientInterface, ExecutableContextInterface
- {
- private $client;
- private $pipeline;
- private $responses = array();
- private $running = false;
-
- public function __construct(ClientInterface $client)
- {
- $this->client = $client;
- $this->pipeline = new SplQueue();
- }
-
- public function __call($method, $arguments)
- {
- $command = $this->client->createCommand($method, $arguments);
- $this->recordCommand($command);
- return $this;
- }
-
- protected function recordCommand(CommandInterface $command)
- {
- $this->pipeline->enqueue($command);
- }
-
- public function executeCommand(CommandInterface $command)
- {
- $this->recordCommand($command);
- }
-
- protected function exception(ConnectionInterface $connection, Response\ErrorInterface $response)
- {
- $connection->disconnect();
- $message = $response->getMessage();
- throw new Response\ServerException($message);
- }
-
- protected function getConnection()
- {
- $connection = $this->getClient()->getConnection();
- if ($connection instanceof ReplicationConnectionInterface) {
- $connection->switchTo('master');
- }
- return $connection;
- }
-
- protected function executePipeline(ConnectionInterface $connection, SplQueue $commands)
- {
- foreach ($commands as $command) {
- $connection->writeRequest($command);
- }
- $responses = array();
- $exceptions = $this->throwServerExceptions();
- while (!$commands->isEmpty()) {
- $command = $commands->dequeue();
- $response = $connection->readResponse($command);
- if (!$response instanceof Response\ResponseInterface) {
- $responses[] = $command->parseResponse($response);
- } else if ($response instanceof Response\ErrorInterface && $exceptions) {
- $this->exception($connection, $response);
- } else {
- $responses[] = $response;
- }
- }
- return $responses;
- }
-
- public function flushPipeline($send = true)
- {
- if ($send && !$this->pipeline->isEmpty()) {
- $responses = $this->executePipeline($this->getConnection(), $this->pipeline);
- $this->responses = array_merge($this->responses, $responses);
- } else {
- $this->pipeline = new SplQueue();
- }
- return $this;
- }
-
- private function setRunning($bool)
- {
- if ($bool && $this->running) {
- throw new ClientException('This pipeline is already opened');
- }
- $this->running = $bool;
- }
-
- public function execute($callable = null)
- {
- if ($callable && !is_callable($callable)) {
- throw new InvalidArgumentException('Argument passed must be a callable object');
- }
- $exception = null;
- $this->setRunning(true);
- try {
- if ($callable) {
- call_user_func($callable, $this);
- }
- $this->flushPipeline();
- } catch (Exception $exception) {
-
- }
- $this->setRunning(false);
- if ($exception) {
- throw $exception;
- }
- return $this->responses;
- }
-
- protected function throwServerExceptions()
- {
- return (bool) $this->client->getOptions()->exceptions;
- }
-
- public function getClient()
- {
- return $this->client;
- }
- }
|