1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace Predis\Pipeline;
- use SplQueue;
- use Predis\ResponseErrorInterface;
- use Predis\ServerException;
- use Predis\Connection\ConnectionInterface;
- use Predis\Connection\ReplicationConnectionInterface;
- class StandardExecutor implements PipelineExecutorInterface
- {
- protected $exceptions;
-
- public function __construct($exceptions = true)
- {
- $this->exceptions = (bool) $exceptions;
- }
-
- protected function checkConnection(ConnectionInterface $connection)
- {
- if ($connection instanceof ReplicationConnectionInterface) {
- $connection->switchTo('master');
- }
- }
-
- protected function onResponseError(ConnectionInterface $connection, ResponseErrorInterface $response)
- {
-
- $connection->disconnect();
- $message = $response->getMessage();
- throw new ServerException($message);
- }
-
- public function execute(ConnectionInterface $connection, SplQueue $commands)
- {
- $size = count($commands);
- $values = array();
- $exceptions = $this->exceptions;
- $this->checkConnection($connection);
- foreach ($commands as $command) {
- $connection->writeCommand($command);
- }
- for ($i = 0; $i < $size; $i++) {
- $response = $connection->readResponse($commands->dequeue());
- if ($response instanceof ResponseErrorInterface && $exceptions === true) {
- $this->onResponseError($connection, $response);
- }
- $values[$i] = $response instanceof \Iterator ? iterator_to_array($response) : $response;
- }
- return $values;
- }
- }
|