PHP code example of strukt / process

1. Go to this page and download the library: Download strukt/process library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

strukt / process example snippets




$result = process('printf "hello\n"');

echo $result->outputs();

$result = process(
    'printf "first\nsecond\n"',
    static function (string $line): void {
        echo "received: {$line}";
    },
);

$result = process('php -r "echo 2 + 2;"');

echo $result->outputs(); // 4
$process = $result->resource();

$result = process([
    'printf "compile\n"',
    'printf "test\n"',
]);

echo $result->outputs(0); // compile
echo $result->outputs(1); // test

$first = $result->resource(0);
$second = $result->resource(1);

$lines = [];

process('printf "one\ntwo\n"', static function (string $line) use (&$lines): void {
    $lines[] = trim($line);
});

// ['one', 'two']

public function resource(int $idx = 0): Process

public function outputs(int $idx = 0): ?string

public function errors(int $idx = 0): ?string

$result = process('sh -c "printf failure >&2; exit 7"');

echo $result->errors();                 // failure
echo $result->resource()->error();      // failure
echo $result->resource()->close();      // 7, already reaped exit code

switchChannels(true);

$result = process('sh -c "printf output; printf error >&2"');

echo $result->outputs(); // error
echo $result->errors();  // output

switchChannels(false);

$prompt = pprocess('read password; printf "%s\n" "$password"');

$prompt->write('secret');
$prompt->closeInput();

echo $prompt->read(); // secret

$prompt->read();
$prompt->error();
$prompt->readline();
$prompt->isRunning();
$prompt->terminate();
$prompt->close();

$pool = new ProcessPool(3);

$pool->start(PHP_BINARY . ' ' . escapeshellarg($workerScript));

while ($pool->running()) {
    $pool->drain(static function (int $index, string $line): void {
        printf("worker %d: %s", $index, $line);
    });
    usleep(100000);
}

$pool->stop();

$pool = pool(3, [PHP_BINARY, $workerScript, '--prefix=demo']);

$supervisor = new ProcessSupervisor(3, restartCap: 5, backoff: 0.5);

$supervisor->onExit(static function (int $index, int $exitCode): void {
    printf("worker %d exited with %d\n", $index, $exitCode);
});

$supervisor->start(PHP_BINARY . ' ' . escapeshellarg($workerScript));

while (true) {
    $supervisor->supervise(static function (int $index, string $line): void {
        printf("worker %d: %s", $index, $line);
    });
    usleep(100000);
}

$supervisor = supervisor(3, [PHP_BINARY, $workerScript], restartCap: 5);

use Strukt\ManagedSupervisor;

$supervisor = new ManagedSupervisor('mail', size: 3, restartCap: 5);
$supervisor->start(PHP_BINARY . ' ' . escapeshellarg($workerScript));

$state = ManagedSupervisor::status('mail');       // snapshot incl. workers
$running = ManagedSupervisor::running('mail');    // true while the pid is alive
$all = ManagedSupervisor::ps();                   // every pool in the directory
$tail = ManagedSupervisor::tail('mail', 50);      // last 50 log lines
ManagedSupervisor::kill('mail', 0);               // SIGTERM one slot; respawns
ManagedSupervisor::stop('mail');                  // SIGTERM the daemon; reaps

public static function run(
    array $commands,
    ?callable $callback = null,
): ProcessResult

public function wait(callable $callback): void

public function output(): string

public function poll(?callable $callback = null): bool

public static function switchChannels(bool $switch = true): void

public function __construct(
    mixed $process,
    mixed $stdin,
    mixed $stdout,
    mixed $stderr,
)

public static function run(string $command): static

public function __construct(
    mixed $process,
    mixed $stdin,
    mixed $stdout,
    mixed $stderr,
)

public function write(string $input): int