forked from reactphp/stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompositeStream.php
84 lines (64 loc) · 1.82 KB
/
CompositeStream.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
<?php
namespace React\Stream;
use Evenement\EventEmitter;
class CompositeStream extends EventEmitter implements DuplexStreamInterface
{
protected $readable;
protected $writable;
protected $pipeSource;
public function __construct(ReadableStreamInterface $readable, WritableStreamInterface $writable)
{
$this->readable = $readable;
$this->writable = $writable;
Util::forwardEvents($this->readable, $this, array('data', 'end', 'error', 'close'));
Util::forwardEvents($this->writable, $this, array('drain', 'error', 'close', 'pipe'));
$this->readable->on('close', array($this, 'close'));
$this->writable->on('close', array($this, 'close'));
$this->on('pipe', array($this, 'handlePipeEvent'));
}
public function handlePipeEvent($source)
{
$this->pipeSource = $source;
}
public function isReadable()
{
return $this->readable->isReadable();
}
public function pause()
{
if ($this->pipeSource) {
$this->pipeSource->pause();
}
$this->readable->pause();
}
public function resume()
{
if ($this->pipeSource) {
$this->pipeSource->resume();
}
$this->readable->resume();
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
Util::pipe($this, $dest, $options);
return $dest;
}
public function isWritable()
{
return $this->writable->isWritable();
}
public function write($data)
{
return $this->writable->write($data);
}
public function end($data = null)
{
$this->writable->end($data);
}
public function close()
{
$this->pipeSource = null;
$this->readable->close();
$this->writable->close();
}
}