ResponseMultiBulkStreamHandler.php 1.4 KB

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