saxulum/saxulum-processes-executor

Saxulum Processes Executor

1.0.0 2018-06-17 10:06 UTC

README

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