Permalink
Browse files

Changed the way of reading the outputs from command line to be much m…

…ore reliable
  • Loading branch information...
1 parent e94bf1e commit e437061338eebfb559a54f1f830a64528ccbc878 @dpobel committed Apr 16, 2012
Showing with 51 additions and 6 deletions.
  1. +51 −6 classes/fepclioptimizer.php
@@ -116,7 +116,6 @@ protected function getEnvironmentVariables()
public function execute( $code, $level = 2 )
{
$this->originalCodeSize = strlen( $code );
- $originalSize = strlen( $code );
$pipes = array();
$process = proc_open(
$this->getCommand(),
@@ -133,19 +132,65 @@ public function execute( $code, $level = 2 )
{
throw new Exception( 'proc_open() failed' );
}
-
fwrite( $pipes[0], $code );
fclose( $pipes[0] );
- $code = stream_get_contents( $pipes[1] );
- fclose( $pipes[1] );
+ stream_set_blocking( $pipes[1], 0 );
+ stream_set_blocking( $pipes[2], 0 );
+
+ $errorOutput = '';
+ $code = '';
+ while ( true )
+ {
+ $w = $e = array();
+ $r = array( $pipes[1], $pipes[2] );
+ if ( $r[0] === null )
+ {
+ unset( $r[0] );
+ }
+ if ( $r[1] === null )
+ {
+ unset( $r[1] );
+ }
+ if ( empty( $r ) )
+ {
+ break;
+ }
+ $res = stream_select( $r, $w, $e, 0, 200000 );
+ if ( $res === false )
+ {
+ throw new Exception( 'An error occured while reading the outputs of the command' );
+ }
+ if ( $res !== 0 )
+ {
+ foreach ( $r as $fp )
+ {
+ if ( $fp === $pipes[2] )
+ {
+ $errorOutput .= stream_get_contents( $fp );
+ if ( feof( $fp ) )
+ {
+ fclose( $pipes[2] );
+ $pipes[2] = null;
+ }
+ }
+ else if ( $fp === $pipes[1] )
+ {
+ $code .= stream_get_contents( $fp );
+ if ( feof( $fp ) )
+ {
+ fclose( $pipes[1] );
+ $pipes[1] = null;
+ }
+ }
+ }
+ }
+ }
- $errorOutput = stream_get_contents( $pipes[2] );
if ( $errorOutput != '' )
{
eZDebug::writeWarning( $errorOutput, 'Error output' );
}
- fclose( $pipes[2] );
$return = proc_close( $process );
if ( $return != 0 )

0 comments on commit e437061

Please sign in to comment.