@@ -52,16 +52,18 @@ public function __construct($disableOutput, $input)
52
52
Process::STDERR => tempnam (sys_get_temp_dir (), 'err_sf_proc ' ),
53
53
);
54
54
foreach ($ this ->files as $ offset => $ file ) {
55
- if (false === $ file || false === $ this ->fileHandles [$ offset ] = fopen ($ file , 'rb ' )) {
55
+ if (false === $ file || false === $ this ->fileHandles [$ offset ] = @ fopen ($ file , 'rb ' )) {
56
56
throw new RuntimeException ('A temporary file could not be opened to write the process output to, verify that your TEMP environment variable is writable ' );
57
57
}
58
58
}
59
59
}
60
60
61
61
if (is_resource ($ input )) {
62
62
$ this ->input = $ input ;
63
- } else {
63
+ } elseif ( is_string ( $ input )) {
64
64
$ this ->inputBuffer = $ input ;
65
+ } else {
66
+ $ this ->inputBuffer = (string ) $ input ;
65
67
}
66
68
}
67
69
@@ -117,19 +119,12 @@ public function readAndWrite($blocking, $close = false)
117
119
if (0 !== fseek ($ fileHandle , $ this ->readBytes [$ type ])) {
118
120
continue ;
119
121
}
120
- $ data = '' ;
121
- $ dataread = null ;
122
- while (!feof ($ fileHandle )) {
123
- if (false !== $ dataread = fread ($ fileHandle , self ::CHUNK_SIZE )) {
124
- $ data .= $ dataread ;
125
- }
126
- }
127
- if (0 < $ length = strlen ($ data )) {
128
- $ this ->readBytes [$ type ] += $ length ;
129
- $ read [$ type ] = $ data ;
130
- }
122
+ $ data = fread ($ fileHandle , self ::CHUNK_SIZE );
131
123
132
- if (false === $ dataread || (true === $ close && feof ($ fileHandle ) && '' === $ data )) {
124
+ if (isset ($ data [0 ])) {
125
+ $ this ->readBytes [$ type ] += strlen ($ data );
126
+ $ read [$ type ] = $ data ;
127
+ } elseif ($ close && feof ($ fileHandle )) {
133
128
fclose ($ this ->fileHandles [$ type ]);
134
129
unset($ this ->fileHandles [$ type ]);
135
130
}
@@ -143,7 +138,7 @@ public function readAndWrite($blocking, $close = false)
143
138
*/
144
139
public function areOpen ()
145
140
{
146
- return ( bool ) $ this ->pipes && ( bool ) $ this ->fileHandles ;
141
+ return $ this ->pipes && $ this ->fileHandles ;
147
142
}
148
143
149
144
/**
@@ -198,9 +193,9 @@ private function write($blocking, $close)
198
193
199
194
$ this ->unblock ();
200
195
201
- $ r = null !== $ this -> input ? array (' input ' => $ this -> input ) : null ;
202
- $ w = isset ( $ this ->pipes [ 0 ]) ? array ($ this ->pipes [ 0 ] ) : null ;
203
- $ e = null ;
196
+ $ e = array () ;
197
+ $ r = null !== $ this ->input ? array (' input ' => $ this ->input ) : $ e ;
198
+ $ w = isset ( $ this -> pipes [ 0 ]) ? array ( $ this -> pipes [ 0 ]) : $ e ;
204
199
205
200
// let's have a look if something changed in streams
206
201
if (false === $ n = @stream_select ($ r , $ w , $ e , 0 , $ blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0 )) {
@@ -218,34 +213,25 @@ private function write($blocking, $close)
218
213
return ;
219
214
}
220
215
221
- if (null !== $ w && 0 < count ($ r )) {
222
- $ data = '' ;
223
- while ($ dataread = fread ($ r ['input ' ], self ::CHUNK_SIZE )) {
224
- $ data .= $ dataread ;
225
- }
216
+ foreach ($ r as $ pipe ) {
217
+ $ this ->inputBuffer .= $ data = fread ($ pipe , self ::CHUNK_SIZE );
226
218
227
- $ this ->inputBuffer .= $ data ;
228
-
229
- if (false === $ data || (true === $ close && feof ($ r ['input ' ]) && '' === $ data )) {
219
+ if (!isset ($ data [0 ]) && $ close && feof ($ pipe )) {
230
220
// no more data to read on input resource
231
221
// use an empty buffer in the next reads
232
222
$ this ->input = null ;
233
223
}
234
224
}
235
225
236
- if (null !== $ w && 0 < count ($ w )) {
237
- while (strlen ($ this ->inputBuffer )) {
238
- $ written = fwrite ($ w [0 ], $ this ->inputBuffer , 2 << 18 );
239
- if ($ written > 0 ) {
240
- $ this ->inputBuffer = (string ) substr ($ this ->inputBuffer , $ written );
241
- } else {
242
- break ;
243
- }
226
+ if (isset ($ this ->inputBuffer [0 ])) {
227
+ foreach ($ w as $ pipe ) {
228
+ $ written = fwrite ($ pipe , $ this ->inputBuffer );
229
+ $ this ->inputBuffer = substr ($ this ->inputBuffer , $ written );
244
230
}
245
231
}
246
232
247
233
// no input to read on resource, buffer is empty and stdin still open
248
- if ('' === $ this ->inputBuffer && null === $ this ->input && isset ($ this ->pipes [0 ])) {
234
+ if (! isset ( $ this ->inputBuffer [ 0 ]) && null === $ this ->input && isset ($ this ->pipes [0 ])) {
249
235
fclose ($ this ->pipes [0 ]);
250
236
unset($ this ->pipes [0 ]);
251
237
}
0 commit comments