1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ChildProcess\Messenger; |
4
|
|
|
|
5
|
|
|
use React\ChildProcess\Process; |
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise\FulfilledPromise; |
8
|
|
|
use React\Stream\Stream; |
9
|
|
|
use React\Stream\Util; |
10
|
|
|
use Tivie\OS\Detector; |
11
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory; |
12
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
13
|
|
|
|
14
|
|
|
class Factory |
15
|
|
|
{ |
16
|
|
|
const INTERVAL = 0.1; |
17
|
|
|
const TIMEOUT = 13; |
18
|
|
|
const TERMINATE_TIMEOUT = 1; |
19
|
|
|
const PROCESS_REGISTER = 'wyrihaximus.react.child-process.messenger.child.register'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Process $process |
23
|
|
|
* @param LoopInterface $loop |
24
|
|
|
* @param array $options |
25
|
|
|
* @param float $interval |
26
|
|
|
* @return \React\Promise\PromiseInterface |
27
|
|
|
*/ |
28
|
1 |
|
public static function parent( |
29
|
1 |
|
Process $process, |
30
|
|
|
LoopInterface $loop, |
31
|
|
|
array $options = [], |
32
|
|
|
$interval = self::INTERVAL |
33
|
|
|
) { |
34
|
1 |
|
$process->start($loop, $interval); |
35
|
|
|
|
36
|
1 |
|
return \WyriHaximus\React\tickingPromise($loop, $interval, [$process, 'isRunning'])-> |
37
|
|
|
then(function () use ($process, $options) { |
38
|
1 |
|
$messenger = new Messenger($process->stdin, $process->stdout, $process->stderr, [ |
39
|
1 |
|
'read_err' => 'stderr', |
40
|
1 |
|
'read' => 'stdout', |
41
|
1 |
|
'write' => 'stdin', |
42
|
|
|
'callForward' => function ($name, $arguments) use ($process) { |
43
|
1 |
|
return call_user_func_array([$process, $name], $arguments); |
44
|
1 |
|
}, |
45
|
1 |
|
] + $options); |
46
|
|
|
|
47
|
1 |
|
Util::forwardEvents($process, $messenger, [ |
48
|
1 |
|
'exit', |
49
|
1 |
|
]); |
50
|
|
|
|
51
|
1 |
|
return \React\Promise\resolve($messenger); |
52
|
1 |
|
}) |
53
|
1 |
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param LoopInterface $loop |
58
|
|
|
* @param array $options |
59
|
|
|
* @param null $termiteCallable |
60
|
|
|
* @return Messenger |
61
|
|
|
*/ |
62
|
2 |
|
public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null) |
63
|
|
|
{ |
64
|
2 |
|
$messenger = new Messenger(new Stream(STDIN, $loop), new Stream(STDOUT, $loop), new Stream(STDERR, $loop), [ |
65
|
2 |
|
'read' => 'stdin', |
66
|
2 |
|
'write_err' => 'stderr', |
67
|
2 |
|
'write' => 'stdout', |
68
|
2 |
|
] + $options); |
69
|
|
|
|
70
|
2 |
|
if ($termiteCallable === null) { |
71
|
|
|
$termiteCallable = function () use ($loop) { |
72
|
1 |
|
$loop->addTimer( |
73
|
2 |
|
self::TERMINATE_TIMEOUT, |
|
|
|
|
74
|
|
|
[ |
75
|
1 |
|
$loop, |
76
|
1 |
|
'stop', |
77
|
1 |
|
] |
78
|
1 |
|
); |
79
|
2 |
|
}; |
80
|
2 |
|
} |
81
|
|
|
|
82
|
2 |
|
$messenger->registerRpc( |
83
|
2 |
|
Messenger::TERMINATE_RPC, |
84
|
1 |
|
function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) { |
85
|
1 |
|
$messenger->emit('terminate', [ |
86
|
1 |
|
$messenger, |
87
|
1 |
|
]); |
88
|
1 |
|
$termiteCallable($payload, $messenger); |
89
|
1 |
|
return new FulfilledPromise(); |
90
|
|
|
} |
91
|
2 |
|
); |
92
|
|
|
|
93
|
2 |
|
return $messenger; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Detector|null $detector |
98
|
|
|
* @return string |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
4 |
|
public static function getProcessForCurrentOS(Detector $detector = null) |
102
|
|
|
{ |
103
|
4 |
|
if ($detector === null) { |
104
|
1 |
|
$detector = new Detector(); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
4 |
|
if ($detector->isUnixLike()) { |
108
|
2 |
|
return 'php ' . __DIR__ . DIRECTORY_SEPARATOR . 'process.php'; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
if ($detector->isWindowsLike()) { |
112
|
1 |
|
return 'php.exe ' . __DIR__ . DIRECTORY_SEPARATOR . 'process.php'; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
throw new \Exception('Unknown OS family'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: