@@ -35,7 +35,8 @@ public function write($data)
3535
3636 return true ;
3737 } catch (Exception $ e ) {
38- $ this ->forwardError ($ e );
38+ $ this ->emit ('error ' , [$ e ]);
39+ $ this ->close ();
3940 return false ;
4041 }
4142 }
@@ -53,7 +54,8 @@ public function end($data = null)
5354 }
5455 $ this ->transformEnd ($ data );
5556 } catch (Exception $ e ) {
56- $ this ->forwardError ($ e );
57+ $ this ->emit ('error ' , [$ e ]);
58+ $ this ->close ();
5759 }
5860 }
5961
@@ -107,109 +109,49 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
107109 return $ dest ;
108110 }
109111
110- /**
111- * Forwards a single "data" event to the reading side of the stream
112- *
113- * This will emit an "data" event.
114- *
115- * If the stream is not readable, then this is a NO-OP.
116- *
117- * @param string $data
118- */
119- protected function forwardData ($ data )
120- {
121- if (!$ this ->readable ) {
122- return ;
123- }
124- $ this ->emit ('data ' , array ($ data ));
125- }
126-
127- /**
128- * Forwards an "end" event to the reading side of the stream
129- *
130- * This will emit an "end" event and will then close this stream.
131- *
132- * If the stream is not readable, then this is a NO-OP.
133- *
134- * @uses self::close()
135- */
136- protected function forwardEnd ()
137- {
138- if (!$ this ->readable ) {
139- return ;
140- }
141- $ this ->readable = false ;
142- $ this ->writable = false ;
143-
144- $ this ->emit ('end ' );
145- $ this ->close ();
146- }
147-
148- /**
149- * Forwards the given $error message to the reading side of the stream
150- *
151- * This will emit an "error" event and will then close this stream.
152- *
153- * If the stream is not readable, then this is a NO-OP.
154- *
155- * @param Exception $error
156- * @uses self::close()
157- */
158- protected function forwardError (Exception $ error )
159- {
160- if (!$ this ->readable ) {
161- return ;
162- }
163- $ this ->readable = false ;
164- $ this ->writable = false ;
165-
166- $ this ->emit ('error ' , array ($ error ));
167- $ this ->close ();
168- }
169-
170112 /**
171113 * can be overwritten in order to implement custom transformation behavior
172114 *
173- * This gets passed a single chunk of $data and should invoke `forwardData()`
115+ * This gets passed a single chunk of $data and should emit a `data` event
174116 * with the filtered result.
175117 *
176- * If the given data chunk is not valid, then you should invoke `forwardError()`
177- * or throw an Exception .
118+ * If the given data chunk is not valid, then you should throw an Exception
119+ * which will automatically be turned into an `error` event .
178120 *
179- * If you do not overwrite this method, then its default implementation simply
180- * invokes `forwardData()` on the unmodified input data chunk.
121+ * If you do not overwrite this method, then its default implementation
122+ * simply emits a `data` event with the unmodified input data chunk.
181123 *
182124 * @param string $data
183- * @see self::forwardData()
184125 */
185126 protected function transformData ($ data )
186127 {
187- $ this ->forwardData ( $ data );
128+ $ this ->emit ( ' data ' , [ $ data] );
188129 }
189130
190131 /**
191132 * can be overwritten in order to implement custom stream ending behavior
192133 *
193- * This may get passed a single final chunk of $data and should invoke `forwardEnd()`.
134+ * This may get passed a single final chunk of $data and should emit an
135+ * `end` event and close the stream.
194136 *
195- * If the given data chunk is not valid, then you should invoke `forwardError()`
196- * or throw an Exception .
137+ * If the given data chunk is not valid, then you should throw an Exception
138+ * which will automatically be turned into an `error` event .
197139 *
198140 * If you do not overwrite this method, then its default implementation simply
199141 * invokes `transformData()` on the unmodified input data chunk (if any),
200- * which in turn defaults to invoking `forwardData()` and then finally
201- * invokes `forwardEnd()` .
142+ * which in turn defaults to emitting a `data` event and then finally
143+ * emits an `end` event and closes the stream .
202144 *
203145 * @param string $data
204146 * @see self::transformData()
205- * @see self::forwardData()
206- * @see self::forwardEnd()
207147 */
208148 protected function transformEnd ($ data )
209149 {
210150 if ($ data !== '' ) {
211151 $ this ->transformData ($ data );
212152 }
213- $ this ->forwardEnd ();
153+
154+ $ this ->emit ('end ' );
155+ $ this ->close ();
214156 }
215157}
0 commit comments