Repository navigation
Expand file tree
/
Copy pathLineSeparatedReader.php
More file actions
100 lines (80 loc) · 2.31 KB
/
Copy pathLineSeparatedReader.php
File metadata and controls
100 lines (80 loc) · 2.31 KB
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
<?php
namespace Clue\React\SshProxy\Io;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
use React\Stream\Util;
use Evenement\EventEmitter;
/** @internal */
class LineSeparatedReader extends EventEmitter implements ReadableStreamInterface
{
private $stream;
private $buffer = '';
private $closed = false;
private $maxlength = 65536;
public function __construct(ReadableStreamInterface $stream)
{
$this->stream = $stream;
if (!$stream->isReadable()) {
$this->close();
return;
}
$this->stream->on('data', array($this, 'handleData'));
$this->stream->on('end', array($this, 'handleEnd'));
$this->stream->on('error', array($this, 'handleError'));
$this->stream->on('close', array($this, 'close'));
}
public function isReadable()
{
return $this->stream->isReadable();
}
public function pause()
{
$this->stream->pause();
}
public function resume()
{
$this->stream->resume();
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
return Util::pipe($this, $dest, $options);
}
public function handleData($chunk)
{
$this->buffer .= $chunk;
while (($pos = strpos($this->buffer, "\n")) !== false && $pos <= $this->maxlength) {
$chunk = rtrim(substr($this->buffer, 0, $pos), "\r\n");
$this->buffer = (string)substr($this->buffer, $pos + 1);
$this->emit('data', array($chunk));
}
if (isset($this->buffer[$this->maxlength])) {
$this->handleError(new \OverflowException('Buffer size exceeded'));
}
}
public function handleEnd()
{
if ($this->buffer !== '') {
$this->handleData("\n");
}
if (!$this->closed) {
$this->emit('end');
$this->close();
}
}
public function handleError(\Exception $e)
{
$this->emit('error', array($e));
$this->close();
}
public function close()
{
if ($this->closed) {
return;
}
$this->closed = true;
$this->buffer = '';
$this->stream->close();
$this->emit('close');
$this->removeAllListeners();
}
}