StandardExecutor.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Connection\ConnectionInterface;
  14. use Predis\Connection\ReplicationConnectionInterface;
  15. use Predis\ServerException;
  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. /**
  26. * @param bool $useServerExceptions Specifies if the executor should throw exceptions on server errors.
  27. */
  28. public function __construct($useServerExceptions = true)
  29. {
  30. $this->useServerExceptions = (bool) $useServerExceptions;
  31. }
  32. /**
  33. * Allows the pipeline executor to perform operations on the
  34. * connection before starting to execute the commands stored
  35. * in the pipeline.
  36. *
  37. * @param ConnectionInterface Connection instance.
  38. */
  39. protected function checkConnection(ConnectionInterface $connection)
  40. {
  41. if ($connection instanceof ReplicationConnectionInterface) {
  42. $connection->switchTo('master');
  43. }
  44. }
  45. /**
  46. * Handles -ERR responses returned by Redis.
  47. *
  48. * @param ConnectionInterface $connection The connection that returned the error.
  49. * @param ResponseErrorInterface $response The error response instance.
  50. */
  51. protected function onResponseError(ConnectionInterface $connection, ResponseErrorInterface $response)
  52. {
  53. // Force disconnection to prevent protocol desynchronization.
  54. $connection->disconnect();
  55. $message = $response->getMessage();
  56. throw new ServerException($message);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function execute(ConnectionInterface $connection, SplQueue $commands)
  62. {
  63. $size = count($commands);
  64. $values = array();
  65. $useServerExceptions = $this->useServerExceptions;
  66. $this->checkConnection($connection);
  67. foreach ($commands as $command) {
  68. $connection->writeCommand($command);
  69. }
  70. for ($i = 0; $i < $size; $i++) {
  71. $response = $connection->readResponse($commands->dequeue());
  72. if ($response instanceof ResponseErrorInterface && $useServerExceptions === true) {
  73. $this->onResponseError($connection, $response);
  74. }
  75. $values[$i] = $response instanceof \Iterator ? iterator_to_array($response) : $response;
  76. }
  77. return $values;
  78. }
  79. }