Debian Codegraph @

vendor/clue/redis-protocol/src/Parser/ResponseParser.php

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