1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ChildProcess\Messenger; |
4
|
|
|
|
5
|
|
|
use React\ChildProcess\Process; |
|
|
|
|
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise; |
8
|
|
|
use React\Socket\ConnectionInterface; |
9
|
|
|
use React\Socket\Connector; |
10
|
|
|
use React\Socket\Server; |
11
|
|
|
use WyriHaximus\FileDescriptors\Factory as FileDescriptorsFactory; |
12
|
|
|
use WyriHaximus\FileDescriptors\ListerInterface; |
13
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory; |
14
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
15
|
|
|
|
16
|
|
|
final class Factory |
17
|
|
|
{ |
18
|
|
|
const INTERVAL = 0.1; |
19
|
|
|
const TIMEOUT = 13; |
20
|
|
|
const TERMINATE_TIMEOUT = 1; |
21
|
|
|
const PROCESS_REGISTER = 'wyrihaximus.react.child-process.messenger.child.register'; |
22
|
|
|
|
23
|
|
|
public static function parent( |
24
|
|
|
Process $process, |
25
|
|
|
LoopInterface $loop, |
26
|
|
|
array $options = [] |
27
|
|
|
) { |
28
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($process, $loop, $options) { |
29
|
|
|
$server = new Server('127.0.0.1:0', $loop); |
30
|
|
|
$argvString = \escapeshellarg(ArgvEncoder::encode([ |
31
|
|
|
'address' => $server->getAddress(), |
32
|
|
|
])); |
33
|
|
|
|
34
|
|
|
$process = new Process($process->getCommand() . ' ' . $argvString); |
|
|
|
|
35
|
|
|
|
36
|
|
|
self::startParent($process, $server, $loop, $options)->done($resolve, $reject); |
|
|
|
|
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $class |
42
|
|
|
* @param LoopInterface $loop |
43
|
|
|
* @param array $options |
44
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
45
|
2 |
|
*/ |
46
|
|
|
public static function parentFromClass( |
47
|
|
|
$class, |
48
|
|
|
LoopInterface $loop, |
49
|
|
|
array $options = [] |
50
|
2 |
|
) { |
51
|
1 |
|
if (!\is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) { |
52
|
|
|
throw new \Exception('Given class doesn\'t implement ChildInterface'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) { |
56
|
1 |
|
$server = new Server('127.0.0.1:0', $loop); |
57
|
1 |
|
$options['address'] = $server->getAddress(); |
58
|
|
|
$options['random'] = \bin2hex(\random_bytes(512)); |
59
|
1 |
|
|
60
|
1 |
|
$template = '%s'; |
61
|
|
|
if (isset($options['cmdTemplate'])) { |
62
|
|
|
$template = $options['cmdTemplate']; |
63
|
|
|
unset($options['cmdTemplate']); |
64
|
|
|
} |
65
|
1 |
|
|
66
|
1 |
|
$fds = []; |
67
|
1 |
|
if (\DIRECTORY_SEPARATOR !== '\\') { |
68
|
1 |
|
if (isset($options['fileDescriptorLister']) && $options['fileDescriptorLister'] instanceof ListerInterface) { |
69
|
1 |
|
/** @var ListerInterface $fileDescriptorLister */ |
70
|
1 |
|
$fileDescriptorLister = $options['fileDescriptorLister']; |
71
|
1 |
|
unset($options['fileDescriptorLister']); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
if (!isset($fileDescriptorLister)) { |
75
|
|
|
/** @var ListerInterface $fileDescriptorLister */ |
76
|
|
|
$fileDescriptorLister = FileDescriptorsFactory::create(); |
77
|
1 |
|
} |
78
|
1 |
|
|
79
|
|
|
foreach (self::listFileDescriptors(($fileDescriptorLister)) as $id) { |
80
|
1 |
|
$fds[(int)$id] = ['file', '/dev/null', 'r']; |
81
|
1 |
|
} |
82
|
1 |
|
} |
83
|
1 |
|
|
84
|
|
|
$phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : '')); |
85
|
|
|
$childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php'); |
86
|
|
|
$argvString = \escapeshellarg(ArgvEncoder::encode($options)); |
87
|
|
|
$command = $phpBinary . ' ' . $childProcessPath; |
88
|
|
|
|
89
|
|
|
$process = new Process( |
90
|
|
|
\sprintf( |
91
|
|
|
$template, |
92
|
|
|
$command . ' ' . $argvString |
93
|
|
|
), |
94
|
|
|
null, |
95
|
|
|
null, |
96
|
|
|
$fds |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$connectTimeout = isset($options['connect-timeout']) ? $options['connect-timeout'] : 5; |
100
|
|
|
\WyriHaximus\React\futurePromise($loop)->then(function () use ($process, $server, $loop, $options, $connectTimeout) { |
|
|
|
|
101
|
|
|
return Promise\Timer\timeout(self::startParent($process, $server, $loop, $options), $connectTimeout, $loop); |
102
|
|
|
})->then(function (Messenger $messenger) use ($class) { |
103
|
|
|
return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [ |
104
|
|
|
'className' => $class, |
105
|
|
|
]))->then(function ($p) use ($messenger) { |
|
|
|
|
106
|
|
|
return Promise\resolve($messenger); |
107
|
|
|
}); |
108
|
|
|
})->done($resolve, $reject); |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param LoopInterface $loop |
114
|
|
|
* @param array $options |
115
|
|
|
* @param callable $termiteCallable |
116
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null) |
119
|
|
|
{ |
120
|
|
|
$connectTimeout = isset($options['connect-timeout']) ? $options['connect-timeout'] : 5; |
121
|
|
|
|
122
|
|
|
return (new Connector($loop, ['timeout' => $connectTimeout]))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options, $loop, $connectTimeout) { |
123
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($connection, $options, $loop, $connectTimeout) { |
124
|
|
|
Promise\Timer\timeout(Promise\Stream\first($connection), $connectTimeout, $loop)->then(function ($chunk) use ($resolve, $connection, $options) { |
125
|
|
|
list($confirmation) = \explode(PHP_EOL, $chunk); |
126
|
|
|
if ($confirmation === 'syn') { |
127
|
|
|
$connection->write('ack' . PHP_EOL); |
128
|
|
|
$resolve(new Messenger($connection, $options)); |
129
|
|
|
} |
130
|
|
|
}, $reject); |
131
|
|
|
$connection->write(\hash_hmac('sha512', $options['address'], $options['random']) . PHP_EOL); |
132
|
|
|
}); |
133
|
|
|
})->then(function (Messenger $messenger) use ($loop, $termiteCallable) { |
134
|
|
|
if ($termiteCallable === null) { |
135
|
|
|
$termiteCallable = function () use ($loop) { |
|
|
|
|
136
|
1 |
|
$loop->addTimer( |
137
|
|
|
self::TERMINATE_TIMEOUT, |
138
|
|
|
[ |
139
|
|
|
$loop, |
140
|
|
|
'stop', |
141
|
|
|
] |
142
|
|
|
); |
143
|
1 |
|
}; |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
$messenger->registerRpc( |
147
|
1 |
|
Messenger::TERMINATE_RPC, |
148
|
1 |
|
function (Payload $payload, Messenger $messenger) use ($termiteCallable) { |
149
|
1 |
|
$messenger->emit('terminate', [ |
150
|
1 |
|
$messenger, |
151
|
|
|
]); |
152
|
1 |
|
$termiteCallable($payload, $messenger); |
153
|
|
|
|
154
|
|
|
return Promise\resolve([]); |
155
|
1 |
|
} |
156
|
1 |
|
); |
157
|
1 |
|
|
158
|
|
|
return $messenger; |
159
|
1 |
|
}); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
|
|
private static function startParent( |
163
|
|
|
Process $process, |
164
|
1 |
|
Server $server, |
165
|
|
|
LoopInterface $loop, |
166
|
1 |
|
array $options |
167
|
|
|
) { |
168
|
1 |
|
return (new Promise\Promise(function ($resolve, $reject) use ($process, $server, $loop, $options) { |
169
|
1 |
|
$server->on( |
170
|
1 |
|
'connection', |
171
|
|
|
function (ConnectionInterface $connection) use ($server, $resolve, $reject, $options, $loop) { |
172
|
1 |
|
Promise\Stream\first($connection)->then(function ($chunk) use ($options, $connection, $resolve) { |
173
|
1 |
|
list($confirmation) = \explode(PHP_EOL, $chunk); |
174
|
1 |
|
if ($confirmation === \hash_hmac('sha512', $options['address'], $options['random'])) { |
175
|
|
|
$connection->write('syn' . PHP_EOL); |
176
|
|
|
|
177
|
|
|
return Promise\Stream\first($connection); |
178
|
|
|
} |
179
|
1 |
|
})->then(function ($chunk) use ($options, $connection) { |
180
|
|
|
list($confirmation) = \explode(PHP_EOL, $chunk); |
181
|
1 |
|
if ($confirmation === 'ack') { |
182
|
1 |
|
return Promise\resolve(new Messenger($connection, $options)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return Promise\reject(new \RuntimeException('Handshake failed')); |
186
|
|
|
})->always(function () use ($server) { |
187
|
|
|
$server->close(); |
188
|
|
|
})->done($resolve, $reject); |
189
|
|
|
} |
190
|
|
|
); |
191
|
|
|
$server->on('error', function ($et) use ($reject) { |
192
|
|
|
$reject($et); |
193
|
|
|
}); |
194
|
|
|
|
195
|
|
|
$process->start($loop); |
196
|
|
|
}, function () use ($server, $process) { |
197
|
|
|
$server->close(); |
198
|
|
|
$process->terminate(); |
199
|
|
|
}))->then(function (Messenger $messenger) use ($loop, $process) { |
200
|
|
|
$loop->addPeriodicTimer(self::INTERVAL, function ($timer) use ($messenger, $loop, $process) { |
201
|
|
|
if (!$process->isRunning()) { |
202
|
|
|
$loop->cancelTimer($timer); |
203
|
|
|
|
204
|
|
|
$exitCode = $process->getExitCode(); |
205
|
|
|
if ($exitCode === 0) { |
206
|
|
|
return; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$messenger->crashed($exitCode); |
210
|
|
|
} |
211
|
|
|
}); |
212
|
|
|
|
213
|
|
|
return $messenger; |
214
|
|
|
}); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private static function listFileDescriptors(ListerInterface $fileDescriptorLister) |
218
|
|
|
{ |
219
|
|
|
if (\method_exists($fileDescriptorLister, 'list')) { |
220
|
|
|
return $fileDescriptorLister->list(); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $fileDescriptorLister->listFileDescriptors(); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: