mirror of
https://github.com/predis/predis.git
synced 2026-10-11 14:27:44 +00:00
fb2d8a37c1
Supporting this feature has been problematic and leaded to some ugly code to make abstractions such as pipelines and transactions aware of these kind of response objects. Furthermore, it was not possible to add them to all the connection classes due to implementation limits. For such reasons Predis do not support them globally anymore, but the actual classes are still shipped within the library so that they can be used to build custom stuff at a level lower than client (that is, unless we decide to remove them for good before going stable).
118 lines
3.3 KiB
PHP
118 lines
3.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Pipeline;
|
|
|
|
use SplQueue;
|
|
use Predis\ResponseErrorInterface;
|
|
use Predis\ResponseObjectInterface;
|
|
use Predis\ServerException;
|
|
use Predis\Command\CommandInterface;
|
|
use Predis\Connection\ConnectionInterface;
|
|
use Predis\Connection\ReplicationConnectionInterface;
|
|
|
|
/**
|
|
* Implements the standard pipeline executor strategy used
|
|
* to write a list of commands and read their replies over
|
|
* a connection to Redis.
|
|
*
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
class StandardExecutor implements PipelineExecutorInterface
|
|
{
|
|
protected $exceptions;
|
|
|
|
/**
|
|
* @param bool $exceptions Specifies if the executor should throw exceptions on server errors.
|
|
*/
|
|
public function __construct($exceptions = true)
|
|
{
|
|
$this->exceptions = (bool) $exceptions;
|
|
}
|
|
|
|
/**
|
|
* Allows the pipeline executor to perform operations on the
|
|
* connection before starting to execute the commands stored
|
|
* in the pipeline.
|
|
*
|
|
* @param ConnectionInterface $connection Connection instance.
|
|
*/
|
|
protected function checkConnection(ConnectionInterface $connection)
|
|
{
|
|
if ($connection instanceof ReplicationConnectionInterface) {
|
|
$connection->switchTo('master');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles a response object.
|
|
*
|
|
* @param ConnectionInterface $connection
|
|
* @param CommandInterface $command
|
|
* @param ResponseObjectInterface $response
|
|
* @return mixed
|
|
*/
|
|
protected function onResponseObject(ConnectionInterface $connection, CommandInterface $command, ResponseObjectInterface $response)
|
|
{
|
|
if ($response instanceof ResponseErrorInterface) {
|
|
return $this->onResponseError($connection, $response);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Handles -ERR responses returned by Redis.
|
|
*
|
|
* @param ConnectionInterface $connection The connection that returned the error.
|
|
* @param ResponseErrorInterface $response The error response instance.
|
|
*/
|
|
protected function onResponseError(ConnectionInterface $connection, ResponseErrorInterface $response)
|
|
{
|
|
if (!$this->exceptions) {
|
|
return $response;
|
|
}
|
|
|
|
// Force disconnection to prevent protocol desynchronization.
|
|
$connection->disconnect();
|
|
$message = $response->getMessage();
|
|
|
|
throw new ServerException($message);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function execute(ConnectionInterface $connection, SplQueue $commands)
|
|
{
|
|
$this->checkConnection($connection);
|
|
|
|
foreach ($commands as $command) {
|
|
$connection->writeCommand($command);
|
|
}
|
|
|
|
$values = array();
|
|
|
|
while (!$commands->isEmpty()) {
|
|
$command = $commands->dequeue();
|
|
$response = $connection->readResponse($command);
|
|
|
|
if ($response instanceof ResponseObjectInterface) {
|
|
$values[] = $this->onResponseObject($connection, $command, $response);
|
|
} else {
|
|
$values[] = $command->parseResponse($response);
|
|
}
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
}
|