Download the PHP package strukt/process without Composer

On this page you can find all versions of the php package strukt/process. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package process

Strukt Process

Latest Stable Version License

Typed, resource-safe process execution for PHP 8.2 and later. Strukt Process supports sequential command batches, line callbacks, captured output, interactive prompt processes, supervised worker pools, and a supervise command-line tool for long-running, daemonized supervised processes.

The package is a Strukt 3 rewrite of 'strukt2/process'. It keeps the original global helpers and process concepts while improving type declarations, resource cleanup, output draining, error capture, and documentation.

Installation

The package depends on 'strukt/base', which supplies the helper registry.

Documentation

The full documentation lives in docs/: a topic guide, an architecture deep dive, a generated per-command reference, and a complete API reference. In particular:

Quick start

Run one command and inspect its output:

Stream complete output lines while a command runs:

process() waits for each command to finish, drains standard output and standard error concurrently, and returns a Strukt\ProcessResult.

Global helpers

The autoloaded src/helpers.php registers all process helpers with the Strukt helper registry.

Helper Parameters Return type Description
switchChannels bool $switch = true void Swaps the logical output and error channels for subsequently created Process objects.
process string|array<int|string, string> $commands, ?callable $callback = null ProcessResult Runs one command or a sequential command batch.
pprocess string $command PromptableProcessInterface Starts an interactive process that accepts input through standard input.
pool int $size, string|list $command, ?string $cwd = null, ?array $env = null ProcessPool Starts a pool of concurrent worker processes.
supervisor int $size, string|list $command, int $restartCap = -1, float $backoff = 0.0, ?string $cwd = null, ?array $env = null ProcessSupervisor Starts a supervised pool that restarts dead workers under a restart policy.

The helpers are available only when their names are not already defined, so they can coexist with applications that provide their own global functions.

Running commands

One command

Multiple commands

Commands run in array order. Each command has its own process resource and captured output:

An empty command list returns an empty ProcessResult. Every command must be a non-empty string; invalid commands throw InvalidArgumentException.

Line callbacks

The callback receives complete logical standard-output lines, including their line endings. A final output fragment without a newline is delivered after the process closes its output stream.

Callbacks may also declare no parameters. Standard error is drained to avoid pipe back-pressure, but it is not sent to the callback.

ProcessResult

Strukt\ProcessResult extends ArrayIterator, so it can be iterated as a list of Strukt\Process objects.

resource

Returns the managed process at a zero-based command index. It throws OutOfBoundsException when the index is absent.

outputs

Returns captured logical standard output for a command. It returns null for an index that has no captured entry and an empty string when the command produced no output.

errors

Returns captured logical standard error for a command. It follows the same null-versus-empty behavior as outputs.

Error output and exit codes

Both output channels are retained:

Process::run() throws RuntimeException when a command cannot be started. The process object uses InvalidArgumentException for invalid constructor handles or empty commands.

Switching channels

The default mapping is standard output to output() and standard error to error(). The mapping can be switched before starting a process:

The setting affects new Process instances and remains enabled until changed. It does not change an already-created process.

Interactive processes

Use pprocess() when a command expects input:

write() appends PHP_EOL to the supplied input and returns the number of bytes written. It throws RuntimeException when standard input is closed or the write fails. Close standard input when the child must receive EOF.

Interactive processes expose the common process methods as well:

Read output before calling close() if it has not already been captured. Calling closeInput(), closeOutput(), closeError(), closePipes(), or closeAll() more than once is safe.

Process pool

ProcessPool supervises a group of concurrently running workers: it spawns a fixed number of copies of one command, lets you drain their output without blocking, and tears them all down together. This fits the classic "spawn N workers, feed a queue, later stop the pool" loop where workers live for the whole batch rather than for one command.

Start the pool with the pool() helper when you do not need the intermediate instance:

start

Spawns $size concurrent copies of the command, each with its own standard three-pipe set so their output stays separate. A string command runs through the shell; an array command runs the program directly. When the string is a simple command (no pipes, redirection, quoting, or substitution), the pool wraps it in exec so the shell replaces itself: the tracked pid is then the real worker, and terminating the pool kills the worker rather than orphaning a child of the shell. Standard input is closed up front because supervised workers rarely read from the parent, and a left-open input pipe risks a deadlock. Starting twice throws RuntimeException.

drain

Reads whatever each worker produced since the last poll without ever blocking on a worker. Complete standard-output lines go to the callback as (int $index, string $line); standard error is drained and retained on each worker (see Process::error()) but not dispatched. With no callback, drain() still records the output on the workers for later Process::output().

stop

Terminates every still-running worker and reaps its exit code, returning an array of codes keyed by spawn index.

Introspection

Method Return Behavior
count int The configured pool size.
active int Workers still running.
running bool True while any worker runs.
processes list\<Process> Workers in spawn order.
process($index) Process One worker by spawn index (throws OutOfBoundsException).

The pool terminates its workers in __destruct() so an abandoned pool never leaves processes behind.

Process supervisor

ProcessSupervisor composes a ProcessPool with a restart policy: when a worker exits (crashes or finishes), supervise() replaces it with a fresh copy of the original command. The replacement keeps the same slot index, so callers that track per-slot state keep their numbering across restarts.

Start a supervisor with the supervisor() helper when you do not need the intermediate instance:

Policy

supervise

supervise() is the only thing a long-running owner needs to call in its loop. It drains worker output first (complete lines go to the optional callback as (int $index, string $line)), so the exit hook sees any final lines a worker wrote before dying, and then revives each dead slot under the policy.

Introspection

Method Return Behavior
count int The configured pool size.
active int Workers still running.
running bool True while any worker runs.
processes list\<Process> Workers in slot order.
process($index) Process One worker by slot index (throws OutOfBoundsException).
restarts($index) int How many times a slot has been restarted.
totalRestarts int All restarts across every slot.
stop array\<int, int> Terminates every worker and returns exit codes keyed by slot.

Managed supervisor

ProcessSupervisor supervises from inside the process that owns it. When the owner is a long-running job -- a queue consumer, a web worker, a scheduled task -- you usually want it to keep running as a background daemon and to be observable from other shells. Strukt\ManagedSupervisor adds that layer: it runs the same supervision loop as a detached daemon and persists everything worth seeing to a runtime directory.

For every supervisor, the daemon writes three artifacts under the runtime directory (default ~/.strukt/supervisor, overridable with the STRUKT_SUPERVISOR_DIR environment variable or the $dir argument):

Artifact Purpose
<name>/supervise.pid Pid of the daemon itself.
<name>/state.json Snapshot of the pool written every supervision pass.
<name>/output.log Combined worker output, one line per slot and supervisor events.

The inspection surface is static on purpose -- status(), ps(), tail(), kill() and stop() read the pid and state files and never join the daemon:

The state snapshot includes the daemon pid, the command, the pool size, the per-slot worker pids, their running flag, exit code, and restart count, plus the total restart count and the log path.

Supervisor CLI

The package ships a supervise binary built on the Strukt console. It wraps ManagedSupervisor with commands to start, list, inspect, kill, and stop long-running supervised processes:

Add --json to any command to render the result as JSON for scripting. Run php vendor/bin/supervise list for the command reference, or generate markdown documentation with php vendor/bin/supervise docs.

Process API

Process::run

Runs commands sequentially and returns their resources and captured streams. The array values must be non-empty command strings.

Process::wait

Drains the logical output and error pipes until the process exits. Complete output lines are passed to the callback. The implementation uses non-blocking pipes and stream_select() so a child that writes a large amount of standard error cannot block while the parent waits on standard output.

Process::output

Returns all logical standard output captured by wait() or read().

Process::poll

Drains whatever output is available right now without blocking, dispatching complete standard-output lines to the callback. Standard error is drained and retained only. A trailing fragment without a newline is delivered once the process exits. Returns true while the process is still running; used by ProcessPool::drain().

Process::switchChannels

Changes the channel mapping used by subsequently constructed processes.

Process::__construct

Creates a process from the process and pipe resources returned by proc_open(). The four values must be live resources. PHP has no resource parameter type, so the implementation validates these arguments at runtime.

Interactive process API

Strukt\Pprocess extends Strukt\AbstractPromptableProcess.

Pprocess::run

Starts a command with standard input, output, and error pipes and returns the interactive process instance.

Pprocess::__construct

Creates an interactive process around existing proc_open() resources.

Common process contract

Strukt\Contract\ProcessInterface defines the shared process operations:

Method Parameters Return type Behavior
getStatus none array Returns the proc_get_status() array; throws when the process handle is closed.
read none string|false Reads available standard output and retains it.
error none string|false Reads available standard error and retains it.
readline none string|false Reads one output line, including its line ending.
terminate none void Terminates a running process; it is safe when already stopped.
close none int Closes resources and returns the process exit code.
closeInput none bool Closes standard input.
closeOutput none bool Closes standard output.
closeError none bool Closes standard error.
closePipes none void Closes all three pipes.
closeAll none void Terminates a running process and closes it.
isRunning none bool Reports whether the process is still running.

Strukt\Contract\PromptableProcessInterface extends this contract with:

The abstract implementations, AbstractProcess and AbstractPromptableProcess, provide the resource management used by Process and Pprocess. Their destructors are defensive and never allow cleanup exceptions to escape during shutdown.

For source compatibility with Strukt 2, the package also provides Strukt\Contract\AbstractProcess and Strukt\Contract\AbstractPromptableProcess as deprecated compatibility subclasses. New code should extend the root-namespace abstract classes.

Resource lifecycle

Process::run() closes standard input before waiting so commands receive EOF, drains both output channels, dispatches pending output, and reaps the child process. The captured strings remain available through ProcessResult and the returned Process objects after cleanup.

For manually managed Pprocess instances:

  1. Call write() as needed.
  2. Call closeInput() when no more input will be sent.
  3. Read output and error.
  4. Call close() or closeAll().

All process objects also attempt safe cleanup in __destruct().

Performance characteristics

Development

Install development dependencies and run the Pest suite:

Validate the package metadata:

The test suite covers command batches, line callbacks, no-argument callbacks, standard-error capture, channel switching, interactive input, worker pools, cleanup, and helper registration.

License

Strukt Process is released under the MIT license.


All versions of process with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
strukt/base Version dev-main
strukt/console Version dev-main
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package strukt/process contains the following files

Loading the files please wait ...