123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace Predis\Protocol\Text;
- use Predis\Command\CommandInterface;
- use Predis\Connection\ComposableConnectionInterface;
- use Predis\Protocol\ResponseReaderInterface;
- use Predis\Protocol\CommandSerializerInterface;
- use Predis\Protocol\ComposableProtocolInterface;
- class ComposableTextProtocol implements ComposableProtocolInterface
- {
- private $serializer;
- private $reader;
-
- public function __construct(Array $options = array())
- {
- $this->setSerializer(new TextCommandSerializer());
- $this->setReader(new TextResponseReader());
- if (count($options) > 0) {
- $this->initializeOptions($options);
- }
- }
-
- private function initializeOptions(Array $options)
- {
- foreach ($options as $k => $v) {
- $this->setOption($k, $v);
- }
- }
-
- public function setOption($option, $value)
- {
- switch ($option) {
- case 'iterable_multibulk':
- $handler = $value ? new ResponseMultiBulkStreamHandler() : new ResponseMultiBulkHandler();
- $this->reader->setHandler(TextProtocol::PREFIX_MULTI_BULK, $handler);
- break;
- default:
- throw new \InvalidArgumentException("The option $option is not supported by the current protocol");
- }
- }
-
- public function serialize(CommandInterface $command)
- {
- return $this->serializer->serialize($command);
- }
-
- public function write(ComposableConnectionInterface $connection, CommandInterface $command)
- {
- $connection->writeBytes($this->serializer->serialize($command));
- }
-
- public function read(ComposableConnectionInterface $connection)
- {
- return $this->reader->read($connection);
- }
-
- public function setSerializer(CommandSerializerInterface $serializer)
- {
- $this->serializer = $serializer;
- }
-
- public function getSerializer()
- {
- return $this->serializer;
- }
-
- public function setReader(ResponseReaderInterface $reader)
- {
- $this->reader = $reader;
- }
-
- public function getReader()
- {
- return $this->reader;
- }
- }
|