X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fatick;a=blobdiff_plain;f=lib%2Fatick%2FIO%2FProcess.php;fp=lib%2Fatick%2FIO%2FProcess.php;h=e4d9bc382bfd42bf6340dcbd299a71f856caa6eb;hp=0000000000000000000000000000000000000000;hb=363315d4d8cbca1850183e27243e317359e261e5;hpb=35f7f2f1715347ce20976dcf6747047e2396b35e diff --git a/lib/atick/IO/Process.php b/lib/atick/IO/Process.php new file mode 100644 index 0000000..e4d9bc3 --- /dev/null +++ b/lib/atick/IO/Process.php @@ -0,0 +1,77 @@ +process = proc_open($command, [["pipe","r"],["pipe","w"],["pipe","w"]], $this->pipes, $cwd, $env); + + if (!is_resource($this->process) || !($status = proc_get_status($this->process))) { + throw new \RuntimeException("Could not open proc '$command': " . error_get_last()["message"]); + } + + stream_set_blocking($this->pipes[1], false); + stream_set_blocking($this->pipes[2], false); + } + + /** + * Cleanup pipes and proc handle + */ + function __destruct() { + foreach ($this->pipes as $fd) { + if (is_resource($fd)) { + fclose($fd); + } + } + proc_close($this->process); + } + + /** + * @inheritdoc + * @return resource + */ + function getOutput() { + return $this->pipes[1]; + } + + /** + * @inheritdoc + * @return resource + */ + function getInput() { + return $this->pipes[0]; + } + + /** + * @inheritdoc + * @param resource $fd + * @return resource + */ + function __invoke($fd) { + if ($fd) { + copy($fd, $this->getInput()); + } + return $this->getOutput(); + } +}