Filesystem & Processes
file_get_contents(), exec() and gethostbyname() all block the PHP thread. In a PAM worker that means every other request on the worker waits too. The runtime ships native replacements that hand the work to Tokio and suspend only the current request’s Fiber:
| Class | Replaces |
|---|---|
Pam\Filesystem\File |
file_get_contents(), file_put_contents() |
Pam\Process\Command |
exec(), shell_exec(), proc_open() |
Pam\Net\Dns |
gethostbyname(), gethostbynamel() |
Pam\Signal\Signal |
pcntl_signal() wait loops |
All four are built in and need no Composer package. Outside a request Fiber (a CLI script, a test, worker boot) they fall back to the equivalent blocking PHP call, so the same code runs everywhere.
<?php
declare(strict_types=1);
use Pam\Filesystem\File;use Pam\Http\Request;use Pam\Http\Response;use Pam\Http\Server;use Pam\Net\Dns;use Pam\Process\Command;
Server::create(static function (Request $request, Response $response): Response { $config = File::read(__DIR__ . '/config.json', maxBytes: 1024); $written = File::write(__DIR__ . '/storage/out.txt', "hello\n"); $command = Command::run(['echo', 'ready'], timeout: 2.0); $addresses = Dns::resolve('localhost', timeout: 2.0);
return $response->json([ 'config' => json_decode($config, true), 'written' => $written, 'command' => [$command->kind->name, $command->exitCode, $command->stdout], 'dns' => $addresses, ]);})->listen(3000, '127.0.0.1');{"config":{"name":"demo"},"written":6,"command":["Exited",0,"ready\n"],"dns":["127.0.0.1","::1"]}Reading and Writing Files
Section titled “Reading and Writing Files”public static function read(string $path, int $maxBytes = 16 * 1024 * 1024, float $timeout = 30.0): string;public static function write(string $path, string $contents, bool $atomic = true, float $timeout = 30.0): int;read() returns the file’s contents and throws RuntimeException when the file is missing, unreadable or larger than $maxBytes. The limit must be between 1 byte and 256 MiB, which protects the worker from loading an unexpectedly huge file into memory.
write() returns the number of bytes written. With $atomic (the default) inside a request, the contents are written to a temporary file next to the destination, flushed to disk and renamed into place, so readers never see a half-written file. Pass atomic: false to write the destination directly. In the blocking fallback, write() uses file_put_contents() with LOCK_EX.
use Pam\Filesystem\File;
$report = File::read(__DIR__ . '/storage/reports/2026-11.csv', maxBytes: 64 * 1024 * 1024);
File::write(__DIR__ . '/storage/cache/routes.json', json_encode($routes, JSON_THROW_ON_ERROR));Paths must be non-empty and contain no NUL bytes. File does not restrict which directories you can reach, so never pass a user-supplied path without validating it against an allowed base directory.
Running Commands
Section titled “Running Commands”public static function run( array $arguments, string $stdin = '', float $timeout = 30.0, int $maxOutputBytes = 8 * 1024 * 1024,): CommandResult;The command is an argument list, never a shell string: arguments are passed to the program as-is, so user input cannot inject shell syntax. Output on stdout and stderr is captured up to $maxOutputBytes.
use Pam\Process\Command;
$result = Command::run(['git', 'rev-parse', '--short', 'HEAD'], timeout: 2.0);
if ($result->successful()) { $revision = trim($result->stdout);}
$formatted = Command::run(['jq', '.'], stdin: $json, timeout: 5.0)->stdout;CommandResult has kind (CommandExitKind::Exited, TimedOut or Signalled), exitCode, stdout and stderr. successful() is true only for Exited with code 0. A command that exceeds its timeout is killed and reported as TimedOut; the call itself does not throw.
When you need a pool limit, futures for parallel commands or truncation flags, use Pam\Task\ProcessPool.
Resolving Host Names
Section titled “Resolving Host Names”public static function resolve(string $host, float $timeout = 5.0): array;Dns::resolve() returns every address of a host name as a list of strings (IPv4 and IPv6). Inside a request it uses Tokio’s resolver; in the fallback it calls gethostbynamel(). It throws InvalidArgumentException for an empty or over-long host name and RuntimeException when resolution fails.
use Pam\Net\Dns;
$addresses = Dns::resolve('api.example.com', timeout: 2.0);Use it, for example, to check that a user-supplied webhook URL does not point at a private network before calling it.
Waiting for Signals
Section titled “Waiting for Signals”public static function wait(int $signal, float $timeout = 30.0): int;Signal::wait() suspends until the process receives $signal (1 to 64) and returns the signal number, or throws RuntimeException('Signal wait timed out.'). Outside a request it needs the pcntl extension.
use Pam\Signal\Signal;
Signal::wait(SIGUSR1, timeout: 60.0);To install a handler instead of waiting, use Pam\Async\onSignal(). Do not trap SIGTERM or SIGHUP in HTTP workers: PAM’s supervisor uses them for graceful shutdown and reloads. Use pam_draining() to find out whether your worker is draining.
Timeouts
Section titled “Timeouts”Each call’s timeout is capped by the request deadline (requestTimeoutMs), and every operation is cancelled if the client disconnects. A native operation with a non-positive timeout throws InvalidArgumentException.
Next Steps
Section titled “Next Steps”- Background Tasks: process pools and task workers.
- Async I/O: futures, deadlines and cancellation.
- HTTP Client: call other services cooperatively.