Introduction
Getting started with the Sandbox package requires just three steps: define an execution policy, create a sandbox instance with your chosen driver, and execute a command. This guide walks through each step and covers the most common patterns you will use day-to-day.Step 1: Create an Execution Policy
Every sandbox requires anExecutionPolicy that defines the constraints for command execution. The simplest way to create one is with the in() factory, which sets the base working directory:
default() factory, which defaults to /tmp:
Step 2: Create a Sandbox Instance
Use one of the static factory methods on theSandbox class to create an instance with your preferred driver:
CanExecuteCommand, so you can swap drivers without changing your calling code.
Dynamic Driver Selection
When the driver is determined at runtime (for example, from a configuration file), use thefromPolicy() builder combined with the using() method. You can pass either a SandboxDriver enum case or a plain string:
host, docker, podman, firejail, bubblewrap. An InvalidArgumentException is thrown if the value does not match any known driver.
Step 3: Execute a Command
Call theexecute() method with a command expressed as an array of strings (argv format):
Passing Standard Input
To pipe data into the command’s stdin, pass it as the second argument:Checking the Result
TheExecResult object provides everything you need to determine whether the command succeeded and to retrieve its output:
Complete Example
Here is a complete example that creates a policy, builds a sandbox, executes a PHP script, and handles the result:Dependency Injection
Since all drivers implement theCanExecuteCommand interface, you can type-hint against the interface in your application services. This makes it easy to swap implementations and simplifies testing with FakeSandbox:
Next Steps
- Learn how to fine-tune execution constraints in Execution Policy.
- Explore the available isolation backends in Drivers.
- Set up real-time output consumption in Streaming and Results.