StandardExecutor.php 3.3 KB

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