1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File: AsynchronousProcessRunner.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* Github: https://github.com/maciejslawik |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MSlwk\ReactPhpPlayground\Standalone\ChildProcess; |
12
|
|
|
|
13
|
|
|
use MSlwk\ReactPhpPlayground\Api\Data\ProcessInterface; |
14
|
|
|
use MSlwk\ReactPhpPlayground\Model\Adapter\ReactPHP\ProcessFactory; |
15
|
|
|
use MSlwk\TypeSafeArray\ObjectArray; |
16
|
|
|
use React\EventLoop\LoopInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AsynchronousProcessRunner |
20
|
|
|
* @package MSlwk\ReactPhpPlayground\Standalone\ChildProcess |
21
|
|
|
*/ |
22
|
|
|
class AsynchronousProcessRunner implements ProcessRunnerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var LoopInterface |
26
|
|
|
*/ |
27
|
|
|
private $loop; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ProcessFactory |
31
|
|
|
*/ |
32
|
|
|
private $processFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* AsynchronousProcessRunner constructor. |
36
|
|
|
* @param LoopInterface $loop |
37
|
|
|
* @param ProcessFactory $processFactory |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
LoopInterface $loop, |
41
|
|
|
ProcessFactory $processFactory |
42
|
|
|
) { |
43
|
|
|
$this->loop = $loop; |
44
|
|
|
$this->processFactory = $processFactory; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ObjectArray $processes |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function runProcesses(ObjectArray $processes): void |
52
|
|
|
{ |
53
|
|
|
/** @var ProcessInterface $process */ |
54
|
|
|
foreach ($processes as $process) { |
55
|
|
|
$this->createProcessDefinition($process); |
56
|
|
|
} |
57
|
|
|
$this->loop->run(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @codeCoverageIgnore |
62
|
|
|
* @param ProcessInterface $process |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
protected function createProcessDefinition(ProcessInterface $process): void |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$reactProcess = $this->processFactory->create($process->getCommand()); |
68
|
|
|
$reactProcess->start($this->loop); |
69
|
|
|
|
70
|
|
|
$reactProcess->stdout->on('data', function ($chunk) { |
71
|
|
|
echo $chunk; |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.