forked from WyriHaximus/reactphp-child-process-messenger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.php
162 lines (143 loc) · 4.88 KB
/
Factory.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace WyriHaximus\React\ChildProcess\Messenger;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
use React\Promise\FulfilledPromise;
use React\Stream\Stream;
use React\Stream\Util;
use Tivie\OS\Detector;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
class Factory
{
const INTERVAL = 0.1;
const TIMEOUT = 13;
const TERMINATE_TIMEOUT = 1;
const PROCESS_REGISTER = 'wyrihaximus.react.child-process.messenger.child.register';
/**
* @param Process $process
* @param LoopInterface $loop
* @param array $options
* @param float $interval
* @return \React\Promise\PromiseInterface
*/
public static function parent(
Process $process,
LoopInterface $loop,
array $options = [],
$interval = self::INTERVAL
) {
$process->start($loop, $interval);
return \WyriHaximus\React\tickingPromise($loop, $interval, [$process, 'isRunning'])->
then(function () use ($process, $options) {
$messenger = new Messenger($process->stdin, $process->stdout, $process->stderr, [
'read_err' => 'stderr',
'read' => 'stdout',
'write' => 'stdin',
'callForward' => function ($name, $arguments) use ($process) {
return call_user_func_array([$process, $name], $arguments);
},
] + $options);
Util::forwardEvents($process, $messenger, [
'exit',
]);
return \React\Promise\resolve($messenger);
})
;
}
/**
* @param LoopInterface $loop
* @param array $options
* @param null $termiteCallable
* @return Messenger
*/
public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null)
{
$messenger = new Messenger(new Stream(STDIN, $loop), new Stream(STDOUT, $loop), new Stream(STDERR, $loop), [
'read' => 'stdin',
'write_err' => 'stderr',
'write' => 'stdout',
] + $options);
if ($termiteCallable === null) {
$termiteCallable = function () use ($loop) {
$loop->addTimer(
self::TERMINATE_TIMEOUT,
[
$loop,
'stop',
]
);
};
}
$messenger->registerRpc(
Messenger::TERMINATE_RPC,
function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) {
$messenger->emit('terminate', [
$messenger,
]);
$termiteCallable($payload, $messenger);
return new FulfilledPromise();
}
);
return $messenger;
}
/**
* @param string $className
* @param LoopInterface $loop
* @param array $options
* @param float $interval
* @return \React\Promise\PromiseInterface
* @throws \Exception
*/
public static function parentFromClass(
$className,
LoopInterface $loop,
array $options = [],
$interval = self::INTERVAL
) {
if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) {
throw new \Exception('Given class doesn\'t implement ChildInterface');
}
$template = '%s';
if (isset($options['cmdTemplate'])) {
$template = $options['cmdTemplate'];
unset($options['cmdTemplate']);
}
$process = new Process(
sprintf(
$template,
self::getProcessForCurrentOS() . ' ' . ArgvEncoder::encode($options)
)
);
return static::parent(
$process,
$loop,
$options,
$interval
)->then(function (Messenger $messenger) use ($className) {
return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [
'className' => $className,
]))->then(function () use ($messenger) {
return \React\Promise\resolve($messenger);
});
});
}
/**
* @param Detector|null $detector
* @return string
* @throws \Exception
*/
public static function getProcessForCurrentOS(Detector $detector = null)
{
if ($detector === null) {
$detector = new Detector();
}
if ($detector->isUnixLike()) {
return 'php ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php';
}
if ($detector->isWindowsLike()) {
return 'php.exe ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php';
}
throw new \Exception('Unknown OS family');
}
}