fix: handle stdio properly
This commit is contained in:
@@ -19,31 +19,55 @@ final class Child
|
||||
|
||||
public function id(): int
|
||||
{
|
||||
if (!is_resource($this->proc)) {
|
||||
throw new ChildException('Resource was already closed');
|
||||
}
|
||||
|
||||
$status = proc_get_status($this->proc);
|
||||
return $status['pid'];
|
||||
}
|
||||
|
||||
public function output(): Output
|
||||
public function waitWithOutput(): Output
|
||||
{
|
||||
if (!is_resource($this->proc)) {
|
||||
throw new ChildException('Resource was already closed');
|
||||
}
|
||||
|
||||
// Avoid possible deadlock before waiting.
|
||||
$this->stdin?->close();
|
||||
|
||||
$stdout = $this->stdout?->getContents();
|
||||
$stderr = $this->stderr?->getContents();
|
||||
$code = proc_close($this->proc);
|
||||
$status = new ExitStatus(proc_close($this->proc));
|
||||
|
||||
return new Output($stdout, $stderr, $code);
|
||||
return new Output($stdout, $stderr, $status);
|
||||
}
|
||||
|
||||
public function status(): int
|
||||
public function wait(): ExitStatus
|
||||
{
|
||||
return proc_close($this->proc);
|
||||
}
|
||||
if (!is_resource($this->proc)) {
|
||||
throw new ChildException('Resource was already closed');
|
||||
}
|
||||
|
||||
public function wait(): void
|
||||
{
|
||||
proc_close($this->proc);
|
||||
// Avoid possible deadlock before waiting.
|
||||
$this->stdin?->close();
|
||||
|
||||
return new ExitStatus(proc_close($this->proc));
|
||||
}
|
||||
|
||||
public function kill(): bool
|
||||
{
|
||||
if (!is_resource($this->proc)) {
|
||||
throw new ChildException('Resource was already closed');
|
||||
}
|
||||
|
||||
return proc_terminate($this->proc, 10);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (is_resource($this->proc)) {
|
||||
proc_close($this->proc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user