Skip to content

Instantly share code, notes, and snippets.

@marten-cz
Created November 14, 2012 09:10
Show Gist options
  • Save marten-cz/4071121 to your computer and use it in GitHub Desktop.
Save marten-cz/4071121 to your computer and use it in GitHub Desktop.
PHP Proc open
<?php
namespace NetteLab\Console;
class Exec
{
public static function call($cmd, $input='')
{
$proc = proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes);
fwrite($pipes[0], $input);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$rtn = proc_close($proc);
if(!empty($stderr))
{
throw new \RuntimeException('Exec::call error: '.print_r(array('cmd'=>$cmd, 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$rtn), 1));
}
return array('stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$rtn);
}
public static function execute($cmd)
{
$out = array();
$rtn = 0;
exec($cmd, $out, $rtn);
if($rtn != 0)
{
throw new \RuntimeException('Exec::execute error: '.print_r(array('cmd'=>$cmd, 'stdout' => $out, 'return' => $rtn), 1));
}
return array('stdout' => $out, 'return' => $rtn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment