Introduction
The Sandbox package supports two complementary ways to work with command output. You can consume output incrementally through a streaming callback as the command runs, and you can inspect the complete result after execution finishes through theExecResult value object. Both mechanisms work with every driver.
Streaming Output
The Streaming Callback
Theexecute() method accepts an optional third argument: a callable that receives output chunks in real time as the command produces them. This is useful for progress reporting, logging, or forwarding output to a user interface.
| Argument | Type | Description |
|---|---|---|
$type | string | 'out' for stdout, 'err' for stderr |
$chunk | string | Raw bytes from the output stream |
Streaming and Output Caps
The streaming callback receives all output, even when the retained buffer inExecResult has been truncated due to output caps. This means you can use the callback to write output to a file or database without worrying about the cap:
Streaming with Standard Input
You can combine stdin and the streaming callback in the same call:Idle Timeout Interaction
The streaming callback does not affect idle timeout tracking. The idle timeout is reset whenever the process produces any output on either stdout or stderr, regardless of whether a callback is provided. However, the callback gives you visibility into when output arrives, which is valuable for diagnosing timeout issues:The ExecResult API
Every call toexecute() returns an ExecResult instance — a readonly value object containing the complete outcome of the execution.
Output Access
combinedOutput() method appends stderr to stdout, separated by a newline if both are non-empty. This is convenient when you do not need to distinguish between the two streams.
Exit Status
success() method checks both the exit code and the timeout flag. A command that exits with code 0 but was forcefully terminated due to a timeout is not considered successful.
Common exit codes:
| Code | Meaning |
|---|---|
0 | Success |
1 | General error |
124 | Timeout (GNU convention, used by the Sandbox package) |
126 | Command found but not executable |
127 | Command not found |
128+N | Killed by signal N (e.g., 137 = SIGKILL) |
Timing
Timeout Detection
124 and timedOut() returns true. The output captured up to the point of termination is still available through stdout() and stderr().
Truncation Detection
Serialization
toArray() method returns a flat associative array suitable for JSON serialization, logging, or passing to the FakeSandbox for test fixtures.
Common Patterns
Guard Against Failure
The most common pattern is to checksuccess() before using the output: