1+ <?php
2+
3+ namespace React \Stream ;
4+
5+ use Evenement \EventEmitter ;
6+
7+ class CompositeStream extends EventEmitter implements ReadableStreamInterface, WritableStreamInterface
8+ {
9+ protected $ readable ;
10+ protected $ writable ;
11+ protected $ pipeSource ;
12+
13+ public function __construct (ReadableStreamInterface $ readable , WritableStreamInterface $ writable )
14+ {
15+ $ this ->readable = $ readable ;
16+ $ this ->writable = $ writable ;
17+
18+ $ this ->forwardEvents ($ this ->readable , ['data ' , 'end ' , 'error ' , 'close ' ]);
19+ $ this ->forwardEvents ($ this ->writable , ['drain ' , 'error ' , 'close ' , 'pipe ' ]);
20+
21+ $ this ->readable ->on ('close ' , array ($ this , 'close ' ));
22+ $ this ->writable ->on ('close ' , array ($ this , 'close ' ));
23+
24+ $ this ->on ('pipe ' , array ($ this , 'handlePipeEvent ' ));
25+ }
26+
27+ public function handlePipeEvent ($ source )
28+ {
29+ $ this ->pipeSource = $ source ;
30+ }
31+
32+ public function isReadable ()
33+ {
34+ return $ this ->readable ->isReadable ();
35+ }
36+
37+ public function pause ()
38+ {
39+ if ($ this ->pipeSource ) {
40+ $ this ->pipeSource ->pause ();
41+ }
42+
43+ $ this ->readable ->pause ();
44+ }
45+
46+ public function resume ()
47+ {
48+ if ($ this ->pipeSource ) {
49+ $ this ->pipeSource ->resume ();
50+ }
51+
52+ $ this ->readable ->resume ();
53+ }
54+
55+ public function pipe (WritableStreamInterface $ dest , array $ options = array ())
56+ {
57+ Util::pipe ($ this , $ dest , $ options );
58+
59+ return $ dest ;
60+ }
61+
62+ public function isWritable ()
63+ {
64+ return $ this ->writable ->isWritable ();
65+ }
66+
67+ public function write ($ data )
68+ {
69+ return $ this ->writable ->write ($ data );
70+ }
71+
72+ public function end ($ data = null )
73+ {
74+ $ this ->writable ->end ($ data );
75+ }
76+
77+ public function close ()
78+ {
79+ $ this ->pipeSource = true ;
80+
81+ $ this ->readable ->close ();
82+ $ this ->writable ->close ();
83+ }
84+
85+ protected function forwardEvents ($ stream , array $ events )
86+ {
87+ $ that = $ this ;
88+
89+ foreach ($ events as $ event ) {
90+ $ stream ->on ($ event , function () use ($ event , $ that ) {
91+ $ that ->emit ($ event , func_get_args ());
92+ });
93+ }
94+ }
95+ }
0 commit comments