@@ -33,7 +33,7 @@ class Stream extends EventEmitter implements DuplexStreamInterface
3333 protected $ loop ;
3434 protected $ buffer ;
3535
36- public function __construct ($ stream , LoopInterface $ loop )
36+ public function __construct ($ stream , LoopInterface $ loop, WritableStreamInterface $ buffer = null )
3737 {
3838 $ this ->stream = $ stream ;
3939 if (!is_resource ($ this ->stream ) || get_resource_type ($ this ->stream ) !== "stream " ) {
@@ -52,16 +52,21 @@ public function __construct($stream, LoopInterface $loop)
5252 stream_set_read_buffer ($ this ->stream , 0 );
5353 }
5454
55+ if ($ buffer === null ) {
56+ $ buffer = new Buffer ($ stream , $ loop );
57+ }
58+
5559 $ this ->loop = $ loop ;
56- $ this ->buffer = new Buffer ( $ this -> stream , $ this -> loop ) ;
60+ $ this ->buffer = $ buffer ;
5761
5862 $ that = $ this ;
5963
6064 $ this ->buffer ->on ('error ' , function ($ error ) use ($ that ) {
6165 $ that ->emit ('error ' , array ($ error , $ that ));
62- $ that ->close ();
6366 });
6467
68+ $ this ->buffer ->on ('close ' , array ($ this , 'close ' ));
69+
6570 $ this ->buffer ->on ('drain ' , function () use ($ that ) {
6671 $ that ->emit ('drain ' , array ($ that ));
6772 });
@@ -114,7 +119,7 @@ public function close()
114119 $ this ->emit ('end ' , array ($ this ));
115120 $ this ->emit ('close ' , array ($ this ));
116121 $ this ->loop ->removeStream ($ this ->stream );
117- $ this ->buffer ->removeAllListeners ();
122+ $ this ->buffer ->close ();
118123 $ this ->removeAllListeners ();
119124
120125 $ this ->handleClose ();
@@ -131,8 +136,6 @@ public function end($data = null)
131136 $ this ->readable = false ;
132137 $ this ->writable = false ;
133138
134- $ this ->buffer ->on ('close ' , array ($ this , 'close ' ));
135-
136139 $ this ->buffer ->end ($ data );
137140 }
138141
@@ -182,6 +185,9 @@ public function handleClose()
182185 }
183186 }
184187
188+ /**
189+ * @return WritableStreamInterface|Buffer
190+ */
185191 public function getBuffer ()
186192 {
187193 return $ this ->buffer ;
0 commit comments