StandardExecutor.php 3.5 KB

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