StreamableMultiBulkResponse.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * Please note that streamable multibulk replies are not globally supported
  20. * by the abstractions built-in into Predis such as for transactions or
  21. * pipelines. Use them with care!
  22. *
  23. * @link http://redis.io/topics/protocol
  24. * @author Daniele Alessandri <suppakilla@gmail.com>
  25. */
  26. class StreamableMultiBulkResponse implements ResponseHandlerInterface
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function handle(ComposableConnectionInterface $connection, $payload)
  32. {
  33. $length = (int) $payload;
  34. if ("$length" != $payload) {
  35. CommunicationException::handle(new ProtocolException(
  36. $connection, "Cannot parse '$payload' as the length of the multibulk response"
  37. ));
  38. }
  39. return new MultiBulk($connection, $length);
  40. }
  41. }