ComposableTextProtocol.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Predis\Protocols;
  3. use Predis\ICommand;
  4. use Predis\Network\IConnectionComposable;
  5. class ComposableTextProtocol implements IProtocolProcessorExtended {
  6. private $_serializer, $_reader;
  7. public function __construct(Array $options = array()) {
  8. $this->setSerializer(new TextCommandSerializer());
  9. $this->setReader(new TextResponseReader());
  10. if (count($options) > 0) {
  11. $this->initializeOptions($options);
  12. }
  13. }
  14. private function initializeOptions(Array $options) {
  15. foreach ($options as $k => $v) {
  16. $this->setOption($k, $v);
  17. }
  18. }
  19. public function setOption($option, $value) {
  20. switch ($option) {
  21. case 'iterable_multibulk':
  22. $handler = $value ? new ResponseMultiBulkStreamHandler() : new ResponseMultiBulkHandler();
  23. $this->_reader->setHandler(TextProtocol::PREFIX_MULTI_BULK, $handler);
  24. break;
  25. case 'throw_errors':
  26. $handler = $value ? new ResponseErrorHandler() : new ResponseErrorSilentHandler();
  27. $this->_reader->setHandler(TextProtocol::PREFIX_ERROR, $handler);
  28. break;
  29. default:
  30. throw new \InvalidArgumentException(
  31. "The option $option is not supported by the current protocol"
  32. );
  33. }
  34. }
  35. public function serialize(ICommand $command) {
  36. return $this->_serializer->serialize($command);
  37. }
  38. public function write(IConnectionComposable $connection, ICommand $command) {
  39. $connection->writeBytes($this->_serializer->serialize($command));
  40. }
  41. public function read(IConnectionComposable $connection) {
  42. return $this->_reader->read($connection);
  43. }
  44. public function setSerializer(ICommandSerializer $serializer) {
  45. $this->_serializer = $serializer;
  46. }
  47. public function getSerializer() {
  48. return $this->_serializer;
  49. }
  50. public function setReader(IResponseReader $reader) {
  51. $this->_reader = $reader;
  52. }
  53. public function getReader() {
  54. return $this->_reader;
  55. }
  56. }