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

@@ -6,27 +6,55 @@ namespace Nih\CommandBuilder;
use Stringable;
abstract class Stdio
final class Stdio
{
abstract public function getDescriptorSpec(): array;
public const INHERIT = 0;
public const PIPE = 1;
public const FILE = 2;
public const STREAM = 3;
public static function file(string|Stringable $file, string $mode): self
{
return new StdioFile((string) $file, $mode);
/**
* @param null
* |resource
* |array{0: 'file', 1: string, 2: string}
* |array{0: 'pipe',1: string} $descriptorSpec
*/
public function __construct(
public readonly int $type,
public readonly mixed $descriptorSpec,
) {
}
public static function piped(string $mode): self
public static function inherit(): self
{
return new StdioPiped($mode);
return new self(self::INHERIT, null);
}
public static function inherit(): null
public static function piped(): self
{
return null;
return new self(self::PIPE, ['pipe', 'r']);
}
public static function null(): self
{
return new StdioFile('/dev/null', 'a+');
return self::file('/dev/null', 'a+');
}
public static function file(string|Stringable $file, string $mode): self
{
return new self(self::FILE, ['file', (string) $file, $mode]);
}
/**
* @param resource|StreamReadable|StreamWritable $stream
*/
public static function stream($stream): self
{
if (is_object($stream)) {
$stream = $stream->stream;
}
return new self(self::STREAM, $stream);
}
}