StandardExecutor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Command\CommandInterface;
  13. use Predis\Connection\ConnectionInterface;
  14. use Predis\Connection\ReplicationConnectionInterface;
  15. use Predis\Response\ResponseErrorInterface;
  16. use Predis\Response\ResponseObjectInterface;
  17. use Predis\Response\ServerException;
  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 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. return $response;
  62. }
  63. /**
  64. * Handles -ERR responses returned by Redis.
  65. *
  66. * @param ConnectionInterface $connection The connection that returned the error.
  67. * @param ResponseErrorInterface $response The error response instance.
  68. */
  69. protected function onResponseError(ConnectionInterface $connection, ResponseErrorInterface $response)
  70. {
  71. if (!$this->exceptions) {
  72. return $response;
  73. }
  74. // Force disconnection to prevent protocol desynchronization.
  75. $connection->disconnect();
  76. $message = $response->getMessage();
  77. throw new ServerException($message);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function execute(ConnectionInterface $connection, SplQueue $commands)
  83. {
  84. $this->checkConnection($connection);
  85. foreach ($commands as $command) {
  86. $connection->writeCommand($command);
  87. }
  88. $values = array();
  89. while (!$commands->isEmpty()) {
  90. $command = $commands->dequeue();
  91. $response = $connection->readResponse($command);
  92. if ($response instanceof ResponseObjectInterface) {
  93. $values[] = $this->onResponseObject($connection, $command, $response);
  94. } else {
  95. $values[] = $command->parseResponse($response);
  96. }
  97. }
  98. return $values;
  99. }
  100. }