forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnedProcess.php
More file actions
93 lines (78 loc) · 2.17 KB
/
Copy pathSpawnedProcess.php
File metadata and controls
93 lines (78 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php declare(strict_types = 1);
namespace PHPStan\Parallel;
use PHPStan\ShouldNotHappenException;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
use Throwable;
use function fclose;
use function rewind;
use function stream_get_contents;
use function tmpfile;
/**
* Parallel worker backed by a freshly spawned PHP process (react/child-process).
* The spawned worker re-boots the whole application via WorkerCommand.
*
* @see ForkedProcess for the pcntl_fork()-based alternative that skips the re-boot.
*/
final class SpawnedProcess extends ProcessBase
{
private Process $process;
/** @var resource */
private $stdOut;
/** @var resource */
private $stdErr;
public function __construct(
private string $command,
LoopInterface $loop,
float $timeoutSeconds,
)
{
parent::__construct($loop, $timeoutSeconds);
}
/**
* @param callable(mixed[] $json) : void $onData
* @param callable(Throwable $exception): void $onError
* @param callable(?int $exitCode, string $output) : void $onExit
*/
public function start(callable $onData, callable $onError, callable $onExit): void
{
$tmpStdOut = tmpfile();
if ($tmpStdOut === false) {
throw new ShouldNotHappenException('Failed creating temp file for stdout.');
}
$tmpStdErr = tmpfile();
if ($tmpStdErr === false) {
throw new ShouldNotHappenException('Failed creating temp file for stderr.');
}
$this->stdOut = $tmpStdOut;
$this->stdErr = $tmpStdErr;
$this->process = new Process($this->command, fds: [
1 => $this->stdOut,
2 => $this->stdErr,
]);
$this->process->start($this->loop);
$this->setCallbacks($onData, $onError);
$this->process->on('exit', function ($exitCode) use ($onExit): void {
$this->cancelTimer();
$output = '';
rewind($this->stdOut);
$output .= stream_get_contents($this->stdOut);
rewind($this->stdErr);
$output .= stream_get_contents($this->stdErr);
$onExit($exitCode, $output);
fclose($this->stdOut);
fclose($this->stdErr);
});
}
public function quit(): void
{
$this->cancelTimer();
if (!$this->process->isRunning()) {
return;
}
foreach ($this->process->pipes as $pipe) {
$pipe->close();
}
$this->endConnection();
}
}