StreamableMultiBulkResponse.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\Protocol\Text\Handler;
  11. use Predis\CommunicationException;
  12. use Predis\Connection\ComposableConnectionInterface;
  13. use Predis\Response\Iterator\MultiBulk;
  14. use Predis\Protocol\ProtocolException;
  15. /**
  16. * Handler for the multibulk response type in the standard Redis wire protocol.
  17. * It returns multibulk responses as iterators that can stream bulk elements.
  18. *
  19. * Streamable multibulk responses are not globally supported by the abstractions
  20. * built-in into Predis, such as transactions or pipelines. Use them with care!
  21. *
  22. * @link http://redis.io/topics/protocol
  23. * @author Daniele Alessandri <suppakilla@gmail.com>
  24. */
  25. class StreamableMultiBulkResponse implements ResponseHandlerInterface
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function handle(ComposableConnectionInterface $connection, $payload)
  31. {
  32. $length = (int) $payload;
  33. if ("$length" != $payload) {
  34. CommunicationException::handle(new ProtocolException(
  35. $connection, "Cannot parse '$payload' as the length of the multibulk response"
  36. ));
  37. }
  38. return new MultiBulk($connection, $length);
  39. }
  40. }