forked from bashkarev/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.php
157 lines (142 loc) · 3.58 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @copyright Copyright (c) 2017 Dmitry Bashkarev
* @license https://github.com/bashkarev/clickhouse/blob/master/LICENSE
* @link https://github.com/bashkarev/clickhouse#readme
*/
namespace bashkarev\clickhouse;
use yii\db\Exception;
/**
* @author Dmitry Bashkarev <[email protected]>
*/
class Parser
{
const POS_HEADER = 0x01;
const POS_LENGTH = 0x02;
const POS_CONTENT = 0x03;
const POS_END = 0x04;
const CRLF = "\r\n";
/**
* @var int
*/
protected $position = 0x01;
/**
* @var int
*/
protected $httpCode;
/**
* @var int
*/
protected $length;
/**
* @var string
*/
protected $last;
/**
* @param resource $socket
* @return \Generator
*/
public function run($socket)
{
while (true) {
if ($this->position === self::POS_HEADER) {
$line = fgets($socket, 1024);
if ($line === false) {
continue;
}
$this->parseHeader($line);
}
if ($this->position === self::POS_LENGTH) {
$line = fgets($socket, 11);
if ($line === false || $line === self::CRLF) {
continue;
}
$this->parseLength($line);
}
if ($this->position === self::POS_CONTENT) {
yield from $this->parseContent(fread($socket, $this->length));
}
if ($this->position === self::POS_END) {
fseek($socket, 2, SEEK_CUR); // \r\n end
if (($last = $this->getLastContent()) !== null) {
yield $last;
}
break 1;
}
}
}
/**
* @param $buffer
* @return \Generator
* @throws Exception
*/
protected function parseContent($buffer)
{
if ($this->httpCode !== 200) {
throw new Exception($buffer);
}
$lines = explode("\n", $buffer);
$count = count($lines) - 1;
for ($i = 0; ; $i++) {
if ($i === $count) {
$this->last = $lines[$i];
break 1;
}
$line = $lines[$i];
if ($i === 0 && $this->last !== null) {
$line = $this->last . $line;
$this->last = null;
}
$value = $this->parseContentLine($line);
if ($value !== null) {
yield $value;
}
}
$this->position = self::POS_LENGTH;
}
/**
* @param $value
* @return array|null
*/
protected function parseContentLine($value)
{
return json_decode($value, true);
}
/**
* @return mixed
*/
protected function getLastContent()
{
if ($this->last === null || $this->last === '') {
return null;
}
return $this->parseContentLine($this->last);
}
/**
* @param $line
*/
protected function parseHeader($line)
{
if ($this->httpCode === null) {
$this->parseCode($line);
}
if ($line === self::CRLF || $line === PHP_EOL) {
$this->position = self::POS_LENGTH;
}
}
/**
* @param $line
*/
protected function parseLength($line)
{
$this->length = hexdec($line);
$this->position = ($this->length === 0) ? self::POS_END : self::POS_CONTENT;
}
/**
* @param string $line
*/
protected function parseCode($line)
{
$this->httpCode = (int)substr($line, 9, 3);
}
}