- } elseif ( $fd == 1 ) {
- // From stdout
- $outBuffer .= $block;
- } elseif ( $fd == 2 ) {
- // From stderr
- $errBuffer .= $block;
- } elseif ( $fd == 3 ) {
- // From log FD
- $logBuffer .= $block;
- if ( strpos( $block, "\n" ) !== false ) {
- $lines = explode( "\n", $logBuffer );
- $logBuffer = array_pop( $lines );
+ }
+
+ if ( $res === '' || $res === 0 ) {
+ // End of file?
+ if ( feof( $pipe ) ) {
+ fclose( $pipes[$fd] );
+ unset( $pipes[$fd] );
+ }
+ } elseif ( $isWrite ) {
+ $buffers[$fd] = (string)substr( $buffers[$fd], $res );
+ if ( $buffers[$fd] === '' ) {
+ fclose( $pipes[$fd] );
+ unset( $pipes[$fd] );
+ }
+ } else {
+ $buffers[$fd] .= $res;
+ if ( $fd === 3 && strpos( $res, "\n" ) !== false ) {
+ // For the log FD, every line is a separate log entry.
+ $lines = explode( "\n", $buffers[3] );
+ $buffers[3] = array_pop( $lines );