@@ -38,7 +38,9 @@ public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
38
38
if (is_resource ($ input )) {
39
39
$ this ->input = $ input ;
40
40
} else {
41
- $ this ->inputBuffer = (string ) $ input ;
41
+ $ this ->input = fopen ('php://temp ' , 'w+ ' );
42
+ fwrite ($ this ->input , $ input );
43
+ fseek ($ this ->input , 0 );
42
44
}
43
45
}
44
46
@@ -147,16 +149,16 @@ public function readAndWrite($blocking, $close = false)
147
149
// lose key association, we have to find back the key
148
150
$ type = (false !== $ found = array_search ($ pipe , $ this ->pipes )) ? $ found : 'input ' ;
149
151
$ data = '' ;
150
- while ('' !== $ dataread = (string ) fread ($ pipe , self ::CHUNK_SIZE )) {
151
- $ data .= $ dataread ;
152
- }
153
-
154
- if ('' !== $ data ) {
155
- if ($ type === 'input ' ) {
156
- $ this ->inputBuffer .= $ data ;
157
- } else {
158
- $ read [$ type ] = $ data ;
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
+ if ('' !== $ data ) {
158
+ $ read [$ type ] = rtrim ($ data , "\x00" );
159
159
}
160
+ } else if (null !== $ w && 0 < count ($ w )) {
161
+ stream_copy_to_stream ($ this ->input , $ w [0 ], 4096 );
160
162
}
161
163
162
164
if (false === $ data || (true === $ close && feof ($ pipe ) && '' === $ data )) {
@@ -171,19 +173,8 @@ public function readAndWrite($blocking, $close = false)
171
173
}
172
174
}
173
175
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
-
185
- // no input to read on resource, buffer is empty and stdin still open
186
- if ('' === $ this ->inputBuffer && null === $ this ->input && isset ($ this ->pipes [0 ])) {
176
+ // no input to read on resource and stdin still open
177
+ if (null === $ this ->input && isset ($ this ->pipes [0 ])) {
187
178
fclose ($ this ->pipes [0 ]);
188
179
unset($ this ->pipes [0 ]);
189
180
}
0 commit comments