Ejemplo n.º 1
0
 /**
  * Handles a multi-bulk reply returned by Redis.
  *
  * @param IConnectionComposable $connection Connection to Redis.
  * @param string $lengthString Number of items in the multi-bulk reply.
  * @return array
  */
 public function handle(IConnectionComposable $connection, $lengthString)
 {
     $length = (int) $lengthString;
     if ($length != $lengthString) {
         Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$length}' as data length"));
     }
     if ($length === -1) {
         return null;
     }
     $list = array();
     if ($length > 0) {
         $handlersCache = array();
         $reader = $connection->getProtocol()->getReader();
         for ($i = 0; $i < $length; $i++) {
             $header = $connection->readLine();
             $prefix = $header[0];
             if (isset($handlersCache[$prefix])) {
                 $handler = $handlersCache[$prefix];
             } else {
                 $handler = $reader->getHandler($prefix);
                 $handlersCache[$prefix] = $handler;
             }
             $list[$i] = $handler->handle($connection, substr($header, 1));
         }
     }
     return $list;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function read(IConnectionComposable $connection)
 {
     $header = $connection->readLine();
     if ($header === '') {
         $this->protocolError($connection, 'Unexpected empty header');
     }
     $prefix = $header[0];
     if (!isset($this->handlers[$prefix])) {
         $this->protocolError($connection, "Unknown prefix '{$prefix}'");
     }
     $handler = $this->handlers[$prefix];
     return $handler->handle($connection, substr($header, 1));
 }
Ejemplo n.º 3
0
 /**
  * Handles a bulk reply returned by Redis.
  *
  * @param IConnectionComposable $connection Connection to Redis.
  * @param string $lengthString Bytes size of the bulk reply.
  * @return string
  */
 public function handle(IConnectionComposable $connection, $lengthString)
 {
     $length = (int) $lengthString;
     if ($length != $lengthString) {
         Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$length}' as data length"));
     }
     if ($length >= 0) {
         return substr($connection->readBytes($length + 2), 0, -2);
     }
     if ($length == -1) {
         return null;
     }
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function read(IConnectionComposable $connection)
 {
     $chunk = $connection->readLine();
     $prefix = $chunk[0];
     $payload = substr($chunk, 1);
     switch ($prefix) {
         case '+':
             // inline
             switch ($payload) {
                 case 'OK':
                     return true;
                 case 'QUEUED':
                     return new ResponseQueued();
                 default:
                     return $payload;
             }
         case '$':
             // bulk
             $size = (int) $payload;
             if ($size === -1) {
                 return null;
             }
             return substr($connection->readBytes($size + 2), 0, -2);
         case '*':
             // multi bulk
             $count = (int) $payload;
             if ($count === -1) {
                 return null;
             }
             if ($this->mbiterable == true) {
                 return new MultiBulkResponseSimple($connection, $count);
             }
             $multibulk = array();
             for ($i = 0; $i < $count; $i++) {
                 $multibulk[$i] = $this->read($connection);
             }
             return $multibulk;
         case ':':
             // integer
             return (int) $payload;
         case '-':
             // error
             if ($this->throwErrors) {
                 throw new ServerException($payload);
             }
             return new ResponseError($payload);
         default:
             Helpers::onCommunicationException(new ProtocolException($connection, "Unknown prefix: '{$prefix}'"));
     }
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function write(IConnectionComposable $connection, ICommand $command)
 {
     $connection->writeBytes($this->_serializer->serialize($command));
 }