public function watch()
{
$watcher = new Watcher();
$watcher->watchPath(getcwd());
$watcher->addListener(function () {
$paths = implode(' ', self::$console->getPaths());
$descriptor = [0 => ['pipe', 'r'], 1 => ['pipe', 'w']];
// Rebuild option string, without watch
$optionString = '';
foreach (self::$console->options as $key => $val) {
if ($key == 'watch') {
continue;
} elseif ($val === true) {
// test
$optionString .= "--{$key} ";
} elseif ($val) {
$optionString .= "--{$key} {$val} ";
}
}
// Run pho in another process and echo its stdout
$procStr = "{$_SERVER['SCRIPT_FILENAME']} {$optionString} {$paths}";
$process = proc_open($procStr, $descriptor, $pipes);
if (is_resource($process)) {
while ($buffer = fread($pipes[1], 16)) {
self::$console->write($buffer);
}
fclose($pipes[0]);
fclose($pipes[1]);
proc_close($process);
}
});
// Ever vigilant
$watcher->watch();
}