1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
3 | namespace Db3v4l\Service; |
||
4 | |||
5 | use Symfony\Component\Process\Process; |
||
0 ignored issues
–
show
The type
Symfony\Component\Process\Process was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
6 | |||
7 | /** |
||
8 | * This ProcessManager is a simple wrapper to enable parallel processing using Symfony Process component. |
||
9 | * Original source code from https://github.com/jagandecapri/symfony-parallel-process/blob/master/src/ProcessManager.php (MIT lic.) |
||
10 | */ |
||
0 ignored issues
–
show
|
|||
11 | class ProcessManager |
||
12 | { |
||
13 | /** |
||
0 ignored issues
–
show
|
|||
14 | * @param Process[] $processes |
||
0 ignored issues
–
show
|
|||
15 | * @param int $maxParallel |
||
0 ignored issues
–
show
|
|||
16 | * @param int $poll microseconds |
||
0 ignored issues
–
show
|
|||
17 | * @param Callable $callback takes 4 args: $type, $buffer, $processIndex, $process |
||
0 ignored issues
–
show
|
|||
18 | */ |
||
0 ignored issues
–
show
|
|||
19 | public function runParallel(array $processes, $maxParallel, $poll = 1000, $callback = null) |
||
20 | { |
||
21 | $this->validateProcesses($processes); |
||
22 | // do not modify the object pointers in the argument, copy to local working variable |
||
23 | $processesQueue = $processes; |
||
24 | // fix maxParallel to be max the number of processes or positive |
||
25 | $maxParallel = min(abs($maxParallel), count($processesQueue)); |
||
26 | // get the first stack of processes to start at the same time |
||
27 | /** @var Process[] $currentProcesses */ |
||
0 ignored issues
–
show
|
|||
28 | $currentProcesses = array_splice($processesQueue, 0, $maxParallel); |
||
29 | // start the initial stack of processes |
||
30 | foreach ($currentProcesses as $idx => $process) { |
||
31 | $process->start(function ($type, $buffer) use ($callback, $idx, $process) { |
||
0 ignored issues
–
show
|
|||
32 | if ($callback) { |
||
33 | $callback($type, $buffer, $idx, $process); |
||
34 | } |
||
35 | }); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
36 | } |
||
37 | do { |
||
38 | // wait for the given time |
||
39 | usleep($poll); |
||
40 | // remove all finished processes from the stack |
||
41 | foreach ($currentProcesses as $index => $process) { |
||
42 | if (!$process->isRunning()) { |
||
43 | unset($currentProcesses[$index]); |
||
44 | // directly add and start new process after the previous finished |
||
45 | if (count($processesQueue) > 0) { |
||
46 | $nextProcess = array_shift($processesQueue); |
||
47 | $nextProcess->start(function ($type, $buffer) use ($callback, $nextProcess) { |
||
0 ignored issues
–
show
|
|||
48 | if ($callback) { |
||
49 | $callback($type, $buffer, $nextProcess); |
||
50 | } |
||
51 | }); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
52 | $currentProcesses[] = $nextProcess; |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | // continue loop while there are processes being executed or waiting for execution |
||
57 | } while (count($processesQueue) > 0 || count($currentProcesses) > 0); |
||
58 | } |
||
59 | |||
60 | /** |
||
0 ignored issues
–
show
|
|||
61 | * @param Process[] $processes |
||
0 ignored issues
–
show
|
|||
62 | */ |
||
0 ignored issues
–
show
|
|||
63 | protected function validateProcesses(array $processes) |
||
64 | { |
||
65 | if (empty($processes)) { |
||
66 | throw new \InvalidArgumentException('Can not run in parallel 0 commands'); |
||
67 | } |
||
68 | foreach ($processes as $process) { |
||
69 | if (!($process instanceof Process)) { |
||
70 | throw new \InvalidArgumentException('Process in array need to be instance of Symfony Process'); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 |