/** * Returns the handler used by the protocol reader for inline responses. * * @return \Closure */ protected function getStatusHandler() { return function ($payload) { return StatusResponse::get($payload); }; }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { return Status::get($payload); }
/** * {@inheritdoc} */ public function read() { $socket = $this->getResource(); $chunk = fgets($socket); if ($chunk === false || $chunk === '') { $this->onConnectionError('Error while reading line from the server.'); } $prefix = $chunk[0]; $payload = substr($chunk, 1, -2); switch ($prefix) { case '+': return StatusResponse::get($payload); case '$': $size = (int) $payload; if ($size === -1) { return; } $bulkData = ''; $bytesLeft = $size += 2; do { $chunk = fread($socket, min($bytesLeft, 4096)); if ($chunk === false || $chunk === '') { $this->onConnectionError('Error while reading bytes from the server.'); } $bulkData .= $chunk; $bytesLeft = $size - strlen($bulkData); } while ($bytesLeft > 0); return substr($bulkData, 0, -2); case '*': $count = (int) $payload; if ($count === -1) { return; } $multibulk = array(); for ($i = 0; $i < $count; ++$i) { $multibulk[$i] = $this->read(); } return $multibulk; case ':': return (int) $payload; case '-': return new ErrorResponse($payload); default: $this->onProtocolError("Unknown response prefix: '{$prefix}'."); return; } }