22
33namespace React \HttpClient ;
44
5- use Evenement \EventEmitterTrait ;
5+ use Evenement \EventEmitter ;
66use React \Promise ;
77use React \Socket \ConnectionInterface ;
88use React \Socket \ConnectorInterface ;
1515 * @event error
1616 * @event end
1717 */
18- class Request implements WritableStreamInterface
18+ class Request extends EventEmitter implements WritableStreamInterface
1919{
20- use EventEmitterTrait;
21-
2220 const STATE_INIT = 0 ;
2321 const STATE_WRITING_HEAD = 1 ;
2422 const STATE_HEAD_WRITTEN = 2 ;
@@ -54,17 +52,18 @@ private function writeHead()
5452 $ streamRef = &$ this ->stream ;
5553 $ stateRef = &$ this ->state ;
5654 $ pendingWrites = &$ this ->pendingWrites ;
55+ $ that = $ this ;
5756
5857 $ promise = $ this ->connect ();
5958 $ promise ->then (
60- function (ConnectionInterface $ stream ) use ($ requestData , &$ streamRef , &$ stateRef , &$ pendingWrites ) {
59+ function (ConnectionInterface $ stream ) use ($ requestData , &$ streamRef , &$ stateRef , &$ pendingWrites, $ that ) {
6160 $ streamRef = $ stream ;
6261
63- $ stream ->on ('drain ' , array ($ this , 'handleDrain ' ));
64- $ stream ->on ('data ' , array ($ this , 'handleData ' ));
65- $ stream ->on ('end ' , array ($ this , 'handleEnd ' ));
66- $ stream ->on ('error ' , array ($ this , 'handleError ' ));
67- $ stream ->on ('close ' , array ($ this , 'handleClose ' ));
62+ $ stream ->on ('drain ' , array ($ that , 'handleDrain ' ));
63+ $ stream ->on ('data ' , array ($ that , 'handleData ' ));
64+ $ stream ->on ('end ' , array ($ that , 'handleEnd ' ));
65+ $ stream ->on ('error ' , array ($ that , 'handleError ' ));
66+ $ stream ->on ('close ' , array ($ that , 'handleClose ' ));
6867
6968 $ headers = (string ) $ requestData ;
7069
@@ -77,7 +76,7 @@ function (ConnectionInterface $stream) use ($requestData, &$streamRef, &$stateRe
7776 $ pendingWrites = '' ;
7877
7978 if ($ more ) {
80- $ this ->emit ('drain ' );
79+ $ that ->emit ('drain ' );
8180 }
8281 }
8382 },
@@ -154,11 +153,10 @@ public function handleData($data)
154153 return ;
155154 }
156155
157- $ response ->on ('close ' , function () {
158- $ this ->close ();
159- });
160- $ response ->on ('error ' , function (\Exception $ error ) {
161- $ this ->closeError (new \RuntimeException (
156+ $ response ->on ('close ' , array ($ this , 'close ' ));
157+ $ that = $ this ;
158+ $ response ->on ('error ' , function (\Exception $ error ) use ($ that ) {
159+ $ that ->closeError (new \RuntimeException (
162160 "An error occured in the response " ,
163161 0 ,
164162 $ error
0 commit comments