StandardExecutor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 SplQueue;
  12. use Predis\ResponseErrorInterface;
  13. use Predis\ResponseObjectInterface;
  14. use Predis\ServerException;
  15. use Predis\Command\CommandInterface;
  16. use Predis\Connection\ConnectionInterface;
  17. use Predis\Connection\ReplicationConnectionInterface;
  18. /**
  19. * Implements the standard pipeline executor strategy used
  20. * to write a list of commands and read their replies over
  21. * a connection to Redis.
  22. *
  23. * @author Daniele Alessandri <suppakilla@gmail.com>
  24. */
  25. class StandardExecutor implements PipelineExecutorInterface
  26. {
  27. protected $exceptions;
  28. /**
  29. * @param bool $exceptions Specifies if the executor should throw exceptions on server errors.
  30. */
  31. public function __construct($exceptions = true)
  32. {
  33. $this->exceptions = (bool) $exceptions;
  34. }
  35. /**
  36. * Allows the pipeline executor to perform operations on the
  37. * connection before starting to execute the commands stored
  38. * in the pipeline.
  39. *
  40. * @param ConnectionInterface Connection instance.
  41. */
  42. protected function checkConnection(ConnectionInterface $connection)
  43. {
  44. if ($connection instanceof ReplicationConnectionInterface) {
  45. $connection->switchTo('master');
  46. }
  47. }
  48. /**
  49. * Handles a response object.
  50. *
  51. * @param ConnectionInterface $connection
  52. * @param CommandInterface $command
  53. * @param ResponseObjectInterface $response
  54. * @return mixed
  55. */
  56. protected function onResponseObject(ConnectionInterface $connection, CommandInterface $command, ResponseObjectInterface $response)
  57. {
  58. if ($response instanceof ResponseErrorInterface) {
  59. return $this->onResponseError($connection, $response);
  60. }
  61. if ($response instanceof \Iterator) {
  62. return $command->parseResponse(iterator_to_array($response));
  63. }
  64. return $response;
  65. }
  66. /**
  67. * Handles -ERR responses returned by Redis.
  68. *
  69. * @param ConnectionInterface $connection The connection that returned the error.
  70. * @param ResponseErrorInterface $response The error response instance.
  71. */
  72. protected function onResponseError(ConnectionInterface $connection, ResponseErrorInterface $response)
  73. {
  74. if (!$this->exceptions) {
  75. return $response;
  76. }
  77. // Force disconnection to prevent protocol desynchronization.
  78. $connection->disconnect();
  79. $message = $response->getMessage();
  80. throw new ServerException($message);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function execute(ConnectionInterface $connection, SplQueue $commands)
  86. {
  87. $size = count($commands);
  88. $values = array();
  89. $this->checkConnection($connection);
  90. foreach ($commands as $command) {
  91. $connection->writeCommand($command);
  92. }
  93. for ($i = 0; $i < $size; $i++) {
  94. $command = $commands->dequeue();
  95. $response = $connection->readResponse($command);
  96. if ($response instanceof ResponseObjectInterface) {
  97. $values[$i] = $this->onResponseObject($connection, $command, $response);
  98. } else {
  99. $values[$i] = $command->parseResponse($response);
  100. }
  101. }
  102. return $values;
  103. }
  104. }