Created
June 28, 2012 18:36
-
-
Save jippi/3013112 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace nodes; | |
use \nodes\Log; | |
class Command { | |
public static function execute($command, $cwd = null, $env = null, $allowSudo = true) { | |
$descriptorspec = array( | |
0 => array("pipe", "r"), // stdin is a pipe that the child will read from | |
1 => array("pipe", "w"), // stdout is a pipe that the child will write to | |
2 => array("pipe", "w") // stderr is a pipe that the child will write to | |
); | |
Log::debug("Executing command: $command"); | |
if (!empty($cwd)) { | |
Log::debug("--> cwd = $cwd"); | |
} | |
// Execute command | |
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env); | |
if (!is_resource($process)) { | |
Log::err("Could not execute command: $command"); | |
throw new Exception("Could not execute command: $command"); | |
} | |
// close stdin | |
fclose($pipes[0]); | |
$stdout = $stderr = $buffer = $errbuf = ""; | |
while (($buffer = fgets($pipes[1], 1024)) != NULL || ($errbuf = fgets($pipes[2], 1024)) != NULL) { | |
if (!empty($buffer)) { | |
$stdout .= $buffer; | |
Log::out('--> stdout: ' . trim($buffer)); | |
} | |
if (!empty($errbuf)) { | |
$stderr .= $errbuf; | |
Log::err('--> stderr: ' . trim($errbuf)); | |
} | |
} | |
fclose($pipes[1]); | |
fclose($pipes[2]); | |
$exit_code = proc_close($process); | |
Log::debug("--> exit_code: $exit_code"); | |
unset($pipes[0], $pipes[1], $pipes[2], $pipes); | |
unset($descriptorspec[0], $descriptorspec[1], $descriptorspec[2], $descriptorspec); | |
return compact('stdout', 'stderr', 'exit_code', 'command', 'cwd', 'env'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace nodes; | |
class Log { | |
static $history = array(); | |
static $echo = true; | |
static $log = false; | |
public static function out($message, $args = array(), $newline = true) { | |
$message = \String::insert($message, $args); | |
if ($newline) { | |
$message .= "\n"; | |
} | |
if (static::$log) { | |
static::$history[] = $message; | |
} | |
if (static::$echo) { | |
echo $message; | |
} | |
} | |
public static function debug($message, $args = array(), $newline = true) { | |
if (!\Configure::read('debug')) { | |
return; | |
} | |
static::out($message, $args, $newline); | |
} | |
public static function err($message, $args = array(), $newline = true) { | |
static::out('--> Error: ' . $message, $args, $newline); | |
} | |
public static function reset() { | |
static::$history = array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment