fix!: handle php errors with custom error handling

This commit changes the api of the stream classes, since indication of
success or failure is no longer necessary.
This commit is contained in:
2026-02-13 23:26:32 +01:00
parent 3a5cad161d
commit 5797059008
14 changed files with 232 additions and 135 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Nih\CommandBuilder;
use InvalidArgumentException;
use Override;
use Stringable;
use ValueError;
@@ -202,10 +203,19 @@ final class Command implements Stringable
*
* Defaults to inherit when used with spawn or status, and defaults to piped
* when used with output.
*
* @param StdioInterface|resource $stdin
*
* @throws InvalidArgumentException
* When the provided stdin handle is neither a StdioInterface nor a live
* stream resource.
*/
public function stdin(StdioInterface $stdin): static
public function stdin(mixed $stdin): static
{
$this->stdin = $stdin;
$this->stdin = $stdin instanceof StdioInterface
? $stdin
: Stdio::stream($stdin);
return $this;
}
@@ -214,10 +224,19 @@ final class Command implements Stringable
*
* Defaults to inherit when used with spawn or status, and defaults to piped
* when used with output.
*
* @param StdioInterface|resource $stdout
*
* @throws InvalidArgumentException
* When the provided stdout handle is neither a StdioInterface nor a live
* stream resource.
*/
public function stdout(StdioInterface $stdout): static
public function stdout(mixed $stdout): static
{
$this->stdout = $stdout;
$this->stdout = $stdout instanceof StdioInterface
? $stdout
: Stdio::stream($stdout);
return $this;
}
@@ -226,10 +245,19 @@ final class Command implements Stringable
*
* Defaults to inherit when used with spawn or status, and defaults to piped
* when used with output.
*
* @param StdioInterface|resource $stderr
*
* @throws InvalidArgumentException
* When the provided stderr handle is neither a StdioInterface nor a live
* stream resource.
*/
public function stderr(StdioInterface $stderr): static
public function stderr(mixed $stderr): static
{
$this->stderr = $stderr;
$this->stderr = $stderr instanceof StdioInterface
? $stderr
: Stdio::stream($stderr);
return $this;
}
@@ -385,21 +413,21 @@ final class Command implements Stringable
}
}
$proc = proc_open(
[$program, ...$this->args],
$descriptorSpec,
$pipes,
$this->cwd,
$environment,
);
if ($proc === false) {
throw new CommandException(sprintf(
'Program "%s" failed to start',
$this->program,
));
try {
set_error_handler(CommandException::handleError(...));
$proc = proc_open(
[$program, ...$this->args],
$descriptorSpec,
$pipes,
$this->cwd,
$environment,
);
} finally {
restore_error_handler();
}
assert($proc !== false);
return new Child($proc, $pipes);
}
}