| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\Redis\Protocol\Parser; |
| 4 |
|
| 5 |
use Clue\Redis\Protocol\Parser\ParserInterface; |
| 6 |
use Clue\Redis\Protocol\Model\ModelInterface; |
| 7 |
use Clue\Redis\Protocol\Model\BulkReply; |
| 8 |
use Clue\Redis\Protocol\Model\ErrorReply; |
| 9 |
use Clue\Redis\Protocol\Model\IntegerReply; |
| 10 |
use Clue\Redis\Protocol\Model\MultiBulkReply; |
| 11 |
use Clue\Redis\Protocol\Model\StatusReply; |
| 12 |
use Clue\Redis\Protocol\Parser\ParserException; |
| 13 |
|
| 14 |
/** |
| 15 |
* Simple recursive redis wire protocol parser |
| 16 |
* |
| 17 |
* Heavily influenced by blocking parser implementation from jpd/redisent. |
| 18 |
* |
| 19 |
* @link https://github.com/jdp/redisent |
| 20 |
* @link http://redis.io/topics/protocol |
| 21 |
*/ |
| 22 |
class ResponseParser implements ParserInterface |
| 23 |
{ |
| 24 |
const CRLF = "\r\n"; |
| 25 |
|
| 26 |
private $incomingBuffer = ''; |
| 27 |
private $incomingOffset = 0; |
| 28 |
|
| 29 |
public function pushIncoming($dataChunk) |
| 30 |
{ |
| 31 |
$this->incomingBuffer .= $dataChunk; |
| 32 |
|
| 33 |
return $this->tryParsingIncomingMessages(); |
| 34 |
} |
| 35 |
|
| 36 |
private function tryParsingIncomingMessages() |
| 37 |
{ |
| 38 |
$messages = array(); |
| 39 |
|
| 40 |
do { |
| 41 |
$message = $this->readResponse(); |
| 42 |
if ($message === null) { |
| 43 |
// restore previous position for next parsing attempt |
| 44 |
$this->incomingOffset = 0; |
| 45 |
break; |
| 46 |
} |
| 47 |
|
| 48 |
$messages []= $message; |
| 49 |
|
| 50 |
$this->incomingBuffer = (string)substr($this->incomingBuffer, $this->incomingOffset); |
| 51 |
$this->incomingOffset = 0; |
| 52 |
} while($this->incomingBuffer !== ''); |
| 53 |
|
| 54 |
return $messages; |
| 55 |
} |
| 56 |
|
| 57 |
private function readLine() |
| 58 |
{ |
| 59 |
$pos = strpos($this->incomingBuffer, "\r\n", $this->incomingOffset); |
| 60 |
|
| 61 |
if ($pos === false) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
$ret = (string)substr($this->incomingBuffer, $this->incomingOffset, $pos - $this->incomingOffset); |
| 66 |
$this->incomingOffset = $pos + 2; |
| 67 |
|
| 68 |
return $ret; |
| 69 |
} |
| 70 |
|
| 71 |
private function readLength($len) |
| 72 |
{ |
| 73 |
$ret = substr($this->incomingBuffer, $this->incomingOffset, $len); |
| 74 |
if (strlen($ret) !== $len) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
$this->incomingOffset += $len; |
| 79 |
|
| 80 |
return $ret; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* try to parse response from incoming buffer |
| 85 |
* |
| 86 |
* ripped from jdp/redisent, with some minor modifications to read from |
| 87 |
* the incoming buffer instead of issuing a blocking fread on a stream |
| 88 |
* |
| 89 |
* @throws ParserException if the incoming buffer is invalid |
| 90 |
* @return ModelInterface|null |
| 91 |
* @link https://github.com/jdp/redisent |
| 92 |
*/ |
| 93 |
private function readResponse() |
| 94 |
{ |
| 95 |
/* Parse the response based on the reply identifier */ |
| 96 |
$reply = $this->readLine(); |
| 97 |
if ($reply === null) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
switch (substr($reply, 0, 1)) { |
| 101 |
/* Error reply */ |
| 102 |
case '-': |
| 103 |
$response = new ErrorReply(substr($reply, 1)); |
| 104 |
break; |
| 105 |
/* Inline reply */ |
| 106 |
case '+': |
| 107 |
$response = new StatusReply(substr($reply, 1)); |
| 108 |
break; |
| 109 |
/* Bulk reply */ |
| 110 |
case '$': |
| 111 |
$size = (int)substr($reply, 1); |
| 112 |
if ($size === -1) { |
| 113 |
return new BulkReply(null); |
| 114 |
} |
| 115 |
$data = $this->readLength($size); |
| 116 |
if ($data === null) { |
| 117 |
return null; |
| 118 |
} |
| 119 |
if ($this->readLength(2) === null) { /* discard crlf */ |
| 120 |
return null; |
| 121 |
} |
| 122 |
$response = new BulkReply($data); |
| 123 |
break; |
| 124 |
/* Multi-bulk reply */ |
| 125 |
case '*': |
| 126 |
$count = (int)substr($reply, 1); |
| 127 |
if ($count === -1) { |
| 128 |
return new MultiBulkReply(null); |
| 129 |
} |
| 130 |
$response = array(); |
| 131 |
for ($i = 0; $i < $count; $i++) { |
| 132 |
$sub = $this->readResponse(); |
| 133 |
if ($sub === null) { |
| 134 |
return null; |
| 135 |
} |
| 136 |
$response []= $sub; |
| 137 |
} |
| 138 |
$response = new MultiBulkReply($response); |
| 139 |
break; |
| 140 |
/* Integer reply */ |
| 141 |
case ':': |
| 142 |
$response = new IntegerReply(substr($reply, 1)); |
| 143 |
break; |
| 144 |
default: |
| 145 |
throw new ParserException('Invalid message can not be parsed: "' . $reply . '"'); |
| 146 |
break; |
| 147 |
} |
| 148 |
/* Party on */ |
| 149 |
return $response; |
| 150 |
} |
| 151 |
} |