fix: handle stdio properly

This commit is contained in:
2025-08-17 13:55:24 +02:00
parent 4ae0545ab4
commit 5f6f5bf04b
15 changed files with 311 additions and 120 deletions

View File

@@ -9,20 +9,40 @@ trait StreamReadable
/**
* @param resource $stream
*/
public function __construct(private $stream)
public function __construct(public readonly mixed $stream)
{
}
public function read(int $length): ?string
{
if (!is_resource($this->stream)) {
throw new CommandException('Cannot read from closed stream');
}
return fread($this->stream, $length) ?: null;
}
public function getContents(?int $length = null, int $offset = -1): ?string
{
if (!is_resource($this->stream)) {
throw new CommandException('Cannot read from closed stream');
}
$contents = stream_get_contents($this->stream, $length, $offset);
return $contents === false
? null
: $contents;
}
public function close(): bool
{
return is_resource($this->stream)
? fclose($this->stream)
: true;
}
public function __destruct()
{
$this->close();
}
}