protected function read_reply($name = '')
{
$reply = fgets($this->redis);
if ($reply === FALSE) {
$info = stream_get_meta_data($this->redis);
if ($info['timed_out']) {
throw new CredisException('Read operation timed out.', CredisException::CODE_TIMED_OUT);
} else {
$this->connected = FALSE;
throw new CredisException('Lost connection to Redis server.', CredisException::CODE_DISCONNECTED);
}
}
$reply = rtrim($reply, CRLF);
#echo "> $name: $reply\n";
$replyType = substr($reply, 0, 1);
switch ($replyType) {
/* Error reply */
case '-':
if ($this->isMulti || $this->usePipeline) {
$response = FALSE;
} else {
if ($name == 'evalsha' && substr($reply, 0, 9) == '-NOSCRIPT') {
$response = NULL;
} else {
throw new CredisException(substr($reply, 0, 4) == '-ERR' ? substr($reply, 5) : substr($reply, 1));
}
}
break;
/* Inline reply */
/* Inline reply */
case '+':
$response = substr($reply, 1);
if ($response == 'OK' || $response == 'QUEUED') {
return TRUE;
}
break;
/* Bulk reply */
/* Bulk reply */
case '$':
if ($reply == '$-1') {
return FALSE;
}
$size = (int) substr($reply, 1);
$response = stream_get_contents($this->redis, $size + 2);
if (!$response) {
$this->connected = FALSE;
throw new CredisException('Error reading reply.');
}
$response = substr($response, 0, $size);
break;
/* Multi-bulk reply */
/* Multi-bulk reply */
case '*':
$count = substr($reply, 1);
if ($count == '-1') {
return FALSE;
}
$response = array();
for ($i = 0; $i < $count; $i++) {
$response[] = $this->read_reply();
}
break;
/* Integer reply */
/* Integer reply */
case ':':
$response = intval(substr($reply, 1));
break;
default:
throw new CredisException('Invalid response: ' . print_r($reply, TRUE));
break;
}
// Smooth over differences between phpredis and standalone response
switch ($name) {
case '':
// Minor optimization for multi-bulk replies
break;
case 'config':
case 'hgetall':
$keys = $values = array();
while ($response) {
$keys[] = array_shift($response);
$values[] = array_shift($response);
}
$response = count($keys) ? array_combine($keys, $values) : array();
break;
case 'info':
$lines = explode(CRLF, trim($response, CRLF));
$response = array();
foreach ($lines as $line) {
if (!$line || substr($line, 0, 1) == '#') {
continue;
}
list($key, $value) = explode(':', $line, 2);
$response[$key] = $value;
}
break;
case 'ttl':
if ($response === -1) {
$response = FALSE;
}
break;
}
return $response;
}