Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

saxulum-legacy/saxulum-processes-executor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A simple to use process executor.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages