Skip to content
A simple to use process executor.
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.phan
src
tests
.gitignore
.scrutinizer.yml
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

saxulum-processes-executor

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Monthly Downloads Latest Stable Version Latest Unstable Version

Description

A simple to use process executor.

Requirements

  • php: ~7.0
  • psr/log: ~1.0
  • symfony/process: ~4.0|~3.0|~2.8

Installation

Through Composer as saxulum/saxulum-processes-executor.

Usage

Simple use without any callbacks

<?php

use Symfony\Component\Process\Process;
use Saxulum\ProcessesExecutor\ProcessesExecutor;

$processes = [
    new Process('php subprocess1.php'),
    new Process('php subprocess2.php'),
    new Process('php subprocess3.php'),
    new Process('php subprocess4.php'),
    new Process('php subprocess5.php'),
];

$startedCommandLines = [];

$executor = new ProcessesExecutor();
$executor->execute($processes);

With start callback (get called onces per process)

<?php

use Symfony\Component\Process\Process;
use Saxulum\ProcessesExecutor\ProcessesExecutor;

$processes = [
    new Process('php subprocess1.php'),
    new Process('php subprocess2.php'),
    new Process('php subprocess3.php'),
    new Process('php subprocess4.php'),
    new Process('php subprocess5.php'),
];

$startedCommandLines = [];

$executor = new ProcessesExecutor();
$executor->execute(
    $processes,
    function (Process $process, $key) use (&$startedProcesses) {
        $startedCommandLines[$key] = $process->getCommandLine();
    }
);

With iteration callback (get called onces per iteration)

<?php

use Symfony\Component\Process\Process;
use Saxulum\ProcessesExecutor\ProcessesExecutor;

$processes = [
    new Process('php subprocess1.php'),
    new Process('php subprocess2.php'),
    new Process('php subprocess3.php'),
    new Process('php subprocess4.php'),
    new Process('php subprocess5.php'),
];

$outputs = [];
$errorOutputs = [];

$executor = new ProcessesExecutor();
$executor->execute(
    $processes,
    null,
    function (array $processes) use (&$outputs, &$errorOutputs) {
        foreach ($processes as $key => $process) {
            /** @var Process $process */
            if ('' !== $output = $process->getIncrementalOutput()) {
                if (!isset($outputs[$key])) {
                    $outputs[$key] = '';
                }

                $outputs[$key] .= $output;
            }
            if ('' !== $errorOutput = $process->getIncrementalErrorOutput()) {
                if (!isset($outputs[$key])) {
                    $errorOutputs[$key] = '';
                }

                $errorOutputs[$key] .= $errorOutput;
            }
        }
    }
);

With finish callback (get called onces per process)

<?php

use Symfony\Component\Process\Process;
use Saxulum\ProcessesExecutor\ProcessesExecutor;

$processes = [
    new Process('php subprocess1.php'),
    new Process('php subprocess2.php'),
    new Process('php subprocess3.php'),
    new Process('php subprocess4.php'),
    new Process('php subprocess5.php'),
];

$outputs = [];
$errorOutputs = [];

$executor = new ProcessesExecutor();
$executor->execute(
    $processes,
    null,
    null,
    function (Process $process, $key) use (&$outputs, &$errorOutputs) {
        if ('' !== $output = $process->getOutput()) {
            $outputs[$key] = $output;
        }
        if ('' !== $errorOutput = $process->getErrorOutput()) {
            $errorOutputs[$key] = $errorOutput;
        }
    }
);

Copyright

Dominik Zogg 2016

You can’t perform that action at this time.