php-vfxapply/src/Process.php

48 lines
1.0 KiB
PHP

<?php
namespace VfxApply;
class Process
{
public function __construct($cmdline, callable $callback)
{
$this->cmdline = $cmdline;
$this->callback = $callback;
}
public function __destruct()
{
}
public function run()
{
$ds = [
0 => [ "pipe", "r" ],
1 => [ "pipe", "w" ],
2 => [ "pipe", "w" ],
];
echo "Exec: {$this->cmdline}\n";
$ps = proc_open($this->cmdline, $ds, $pipes);
list($stdin,$stdout,$stderr) = $pipes;
stream_set_blocking($stdout,false);
$buf = null;
while (true) {
$str = fgets($stdout, 4192);
$status = proc_get_status($ps);
if ($str) {
$ret = call_user_func($this->callback, $str);
if ($ret === false) {
proc_terminate($ps);
}
}
if ($status['running']==false) break;
usleep(10000);
}
proc_close($ps);
return $status['exitcode'];
}
}