@@ -38,7 +38,9 @@ public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
3838 if (is_resource ($ input )) {
3939 $ this ->input = $ input ;
4040 } else {
41- $ this ->inputBuffer = (string ) $ input ;
41+ $ this ->input = fopen ('php://temp ' , 'w+ ' );
42+ fwrite ($ this ->input , $ input );
43+ fseek ($ this ->input , 0 );
4244 }
4345 }
4446
@@ -147,16 +149,20 @@ public function readAndWrite($blocking, $close = false)
147149 // lose key association, we have to find back the key
148150 $ type = (false !== $ found = array_search ($ pipe , $ this ->pipes )) ? $ found : 'input ' ;
149151 $ data = '' ;
150- while ('' !== $ dataread = (string ) fread ($ pipe , self ::CHUNK_SIZE )) {
151- $ data .= $ dataread ;
152+ if ($ type !== 'input ' ) {
153+ while ('' !== $ dataread = (string ) fread ($ pipe , self ::CHUNK_SIZE )) {
154+ $ data .= $ dataread ;
155+ }
156+ // Remove extra null chars returned by fread
157+ $ data = rtrim ($ data , "\x00" );
158+ } else {
159+ if (null !== $ w && 0 < count ($ w )) {
160+ stream_copy_to_stream ($ this ->input , $ w [0 ], 4096 );
161+ }
152162 }
153163
154164 if ('' !== $ data ) {
155- if ($ type === 'input ' ) {
156- $ this ->inputBuffer .= $ data ;
157- } else {
158- $ read [$ type ] = $ data ;
159- }
165+ $ read [$ type ] = $ data ;
160166 }
161167
162168 if (false === $ data || (true === $ close && feof ($ pipe ) && '' === $ data )) {
@@ -171,17 +177,6 @@ public function readAndWrite($blocking, $close = false)
171177 }
172178 }
173179
174- if (null !== $ w && 0 < count ($ w )) {
175- while (strlen ($ this ->inputBuffer )) {
176- $ written = fwrite ($ w [0 ], $ this ->inputBuffer , 2 << 18 ); // write 512k
177- if ($ written > 0 ) {
178- $ this ->inputBuffer = (string ) substr ($ this ->inputBuffer , $ written );
179- } else {
180- break ;
181- }
182- }
183- }
184-
185180 // no input to read on resource, buffer is empty and stdin still open
186181 if ('' === $ this ->inputBuffer && null === $ this ->input && isset ($ this ->pipes [0 ])) {
187182 fclose ($ this ->pipes [0 ]);
0 commit comments