|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter\Components; |
|
4
|
|
|
|
|
5
|
|
|
use mpyw\Co\Co; |
|
6
|
|
|
use mpyw\Cowitter\Response; |
|
7
|
|
|
use mpyw\Cowitter\Helpers\ResponseBodyDecoder; |
|
8
|
|
|
use mpyw\Cowitter\Helpers\RequestParamValidator; |
|
9
|
|
|
|
|
10
|
|
|
class StreamHandler |
|
11
|
|
|
{ |
|
12
|
|
|
protected $headerResponse; |
|
13
|
|
|
protected $headerResponseBuffer = ''; |
|
14
|
|
|
protected $headerResponseHandler; |
|
15
|
|
|
protected $eventBuffer = ''; |
|
16
|
|
|
protected $eventHandler; |
|
17
|
|
|
protected $haltedByUser = false; |
|
18
|
|
|
|
|
19
|
15 |
|
public function __construct(callable $header_response_handler = null, callable $event_handler = null) |
|
20
|
15 |
|
{ |
|
21
|
15 |
|
$this->headerResponseHandler = $header_response_handler; |
|
22
|
15 |
|
$this->eventHandler = $event_handler; |
|
23
|
15 |
|
} |
|
24
|
|
|
|
|
25
|
13 |
|
public function headerFunction($ch, $str) |
|
26
|
13 |
|
{ |
|
27
|
13 |
|
$handle = $this->headerResponseHandler; |
|
28
|
13 |
|
$this->headerResponseBuffer .= $str; |
|
29
|
13 |
|
if (substr($this->headerResponseBuffer, -4) === "\r\n\r\n") { |
|
30
|
13 |
|
$this->headerResponse = new Response($this->headerResponseBuffer, $ch); |
|
31
|
13 |
|
if ($handle) { |
|
32
|
3 |
|
RequestParamValidator::isGenerator($handle) |
|
33
|
1 |
|
? Co::async($handle($this->headerResponse)) |
|
34
|
2 |
|
: $handle($this->headerResponse); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
13 |
|
return strlen($str); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
11 |
|
protected function processLine($line) |
|
41
|
11 |
|
{ |
|
42
|
11 |
|
$handle = $this->eventHandler; |
|
43
|
11 |
|
if ('' === $line = rtrim($line)) { |
|
44
|
8 |
|
return; |
|
45
|
|
|
} |
|
46
|
11 |
|
$event = ResponseBodyDecoder::getDecodedResponse($this->headerResponse, $line); |
|
47
|
11 |
|
if ($handle) { |
|
48
|
11 |
|
if (RequestParamValidator::isGenerator($handle)) { |
|
49
|
|
|
Co::async(function () use ($handle, $event) { |
|
50
|
1 |
|
if (false === (yield $handle($event->getContent()))) { |
|
51
|
1 |
|
$this->haltedByUser = true; |
|
52
|
|
|
} |
|
53
|
1 |
|
}); |
|
54
|
10 |
|
} elseif (false === $handle($event->getContent())) { |
|
55
|
8 |
|
$this->haltedByUser = true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
11 |
|
} |
|
59
|
|
|
|
|
60
|
13 |
|
public function writeFunction($ch, $str) |
|
61
|
13 |
|
{ |
|
62
|
13 |
|
if ($this->haltedByUser) { |
|
63
|
|
|
// @codeCoverageIgnoreStart |
|
64
|
|
|
return 0; |
|
65
|
|
|
// @codeCoverageIgnoreEnd |
|
66
|
|
|
} |
|
67
|
13 |
|
$this->eventBuffer .= $str; |
|
68
|
13 |
|
if (200 !== curl_getinfo($ch, CURLINFO_HTTP_CODE)) { |
|
69
|
2 |
|
ResponseBodyDecoder::getDecodedResponse($this->headerResponse, $this->eventBuffer); |
|
70
|
1 |
|
throw new \UnexpectedValueException('Unexpected response: ' . $this->eventBuffer); |
|
71
|
|
|
} |
|
72
|
11 |
|
while (false !== $pos = strpos($this->eventBuffer, "\n")) { |
|
73
|
11 |
|
$line = substr($this->eventBuffer, 0, $pos + 1); |
|
74
|
11 |
|
$this->eventBuffer = substr($this->eventBuffer, $pos + 1); |
|
75
|
11 |
|
$this->processLine($line); |
|
76
|
11 |
|
if ($this->haltedByUser) { |
|
77
|
9 |
|
return 0; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
8 |
|
return strlen($str); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
13 |
|
public function isHaltedByUser() |
|
84
|
13 |
|
{ |
|
85
|
13 |
|
return $this->haltedByUser; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|