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\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory; |
12
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
13
|
|
|
|
14
|
|
|
final 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
|
|
|
public static function parent( |
22
|
|
|
Process $process, |
23
|
|
|
LoopInterface $loop, |
24
|
|
|
array $options = [] |
25
|
|
|
) { |
26
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($process, $loop, $options) { |
27
|
|
|
$server = new Server('127.0.0.1:0', $loop); |
28
|
|
|
$argvString = \escapeshellarg(ArgvEncoder::encode([ |
29
|
|
|
'address' => $server->getAddress(), |
30
|
|
|
])); |
31
|
|
|
|
32
|
|
|
$process = new Process($process->getCommand() . ' ' . $argvString); |
|
|
|
|
33
|
|
|
|
34
|
|
|
self::startParent($process, $server, $loop, $options)->done($resolve, $reject); |
|
|
|
|
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $class |
40
|
|
|
* @param LoopInterface $loop |
41
|
|
|
* @param array $options |
42
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
43
|
|
|
*/ |
44
|
2 |
|
public static function parentFromClass( |
45
|
|
|
$class, |
46
|
|
|
LoopInterface $loop, |
47
|
|
|
array $options = [] |
48
|
|
|
) { |
49
|
2 |
|
if (!is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) { |
50
|
1 |
|
throw new \Exception('Given class doesn\'t implement ChildInterface'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) { |
54
|
1 |
|
$server = new Server('127.0.0.1:0', $loop); |
55
|
1 |
|
$options['address'] = $server->getAddress(); |
56
|
1 |
|
$options['random'] = bin2hex(random_bytes(512)); |
57
|
|
|
|
58
|
1 |
|
$template = '%s'; |
59
|
1 |
|
if (isset($options['cmdTemplate'])) { |
60
|
|
|
$template = $options['cmdTemplate']; |
61
|
|
|
unset($options['cmdTemplate']); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : '')); |
65
|
1 |
|
$childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php'); |
66
|
1 |
|
$argvString = \escapeshellarg(ArgvEncoder::encode($options)); |
67
|
1 |
|
$command = $phpBinary . ' ' . $childProcessPath; |
68
|
1 |
|
$fds = []; |
69
|
1 |
|
foreach (scandir('/proc/self/fd', SCANDIR_SORT_NONE) as $id) { |
70
|
1 |
|
if (!in_array($id, ['.', '..'], true)) { |
71
|
1 |
|
$fds[(int)$id] = ['file', '/dev/null', 'r']; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$process = new Process( |
76
|
1 |
|
sprintf( |
77
|
1 |
|
$template, |
78
|
1 |
|
$command . ' ' . $argvString |
79
|
|
|
), |
80
|
1 |
|
null, |
81
|
1 |
|
null, |
82
|
1 |
|
$fds |
83
|
|
|
); |
84
|
|
|
|
85
|
1 |
|
$connectTimeout = isset($options['connect-timeout']) ? $options['connect-timeout'] : 5; |
86
|
|
|
\WyriHaximus\React\futurePromise($loop)->then(function () use ($process, $server, $loop, $options, $connectTimeout) { |
87
|
1 |
|
return Promise\Timer\timeout(self::startParent($process, $server, $loop, $options), $connectTimeout, $loop); |
88
|
|
|
})->then(function (Messenger $messenger) use ($class) { |
89
|
1 |
|
return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [ |
90
|
1 |
|
'className' => $class, |
91
|
|
|
]))->then(function ($p) use ($messenger) { |
|
|
|
|
92
|
1 |
|
return Promise\resolve($messenger); |
93
|
1 |
|
}); |
94
|
1 |
|
})->done($resolve, $reject); |
95
|
1 |
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param LoopInterface $loop |
100
|
|
|
* @param array $options |
101
|
|
|
* @param callable $termiteCallable |
102
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null) |
105
|
|
|
{ |
106
|
|
|
$connectTimeout = isset($options['connect-timeout']) ? $options['connect-timeout'] : 5; |
107
|
|
|
|
108
|
|
|
return (new Connector($loop, ['timeout' => $connectTimeout]))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options, $loop, $connectTimeout) { |
109
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($connection, $options, $loop, $connectTimeout) { |
110
|
|
|
Promise\Timer\timeout(Promise\Stream\first($connection), $connectTimeout, $loop)->then(function ($chunk) use ($resolve, $connection, $options) { |
111
|
|
|
list($confirmation) = explode(PHP_EOL, $chunk); |
112
|
|
|
if ($confirmation === 'syn') { |
113
|
|
|
$connection->write('ack' . PHP_EOL); |
114
|
|
|
$resolve(new Messenger($connection, $options)); |
115
|
|
|
} |
116
|
|
|
}, $reject); |
117
|
|
|
$connection->write(hash_hmac('sha512', $options['address'], $options['random']) . PHP_EOL); |
118
|
|
|
}); |
119
|
|
|
})->then(function (Messenger $messenger) use ($loop, $termiteCallable) { |
120
|
|
|
if ($termiteCallable === null) { |
121
|
|
|
$termiteCallable = function () use ($loop) { |
|
|
|
|
122
|
|
|
$loop->addTimer( |
123
|
|
|
self::TERMINATE_TIMEOUT, |
124
|
|
|
[ |
125
|
|
|
$loop, |
126
|
|
|
'stop', |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
}; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$messenger->registerRpc( |
133
|
|
|
Messenger::TERMINATE_RPC, |
134
|
|
|
function (Payload $payload, Messenger $messenger) use ($termiteCallable) { |
135
|
|
|
$messenger->emit('terminate', [ |
136
|
|
|
$messenger, |
137
|
|
|
]); |
138
|
|
|
$termiteCallable($payload, $messenger); |
139
|
|
|
|
140
|
|
|
return Promise\resolve([]); |
141
|
|
|
} |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return $messenger; |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
private static function startParent( |
149
|
|
|
Process $process, |
150
|
|
|
Server $server, |
151
|
|
|
LoopInterface $loop, |
152
|
|
|
array $options |
153
|
|
|
) { |
154
|
|
|
return (new Promise\Promise(function ($resolve, $reject) use ($process, $server, $loop, $options) { |
155
|
1 |
|
$server->on( |
156
|
1 |
|
'connection', |
157
|
|
|
function (ConnectionInterface $connection) use ($server, $resolve, $reject, $options, $loop) { |
158
|
|
|
Promise\Stream\first($connection)->then(function ($chunk) use ($options, $connection, $resolve) { |
159
|
1 |
|
list($confirmation) = explode(PHP_EOL, $chunk); |
160
|
1 |
|
if ($confirmation === hash_hmac('sha512', $options['address'], $options['random'])) { |
161
|
1 |
|
$connection->write('syn' . PHP_EOL); |
162
|
|
|
|
163
|
1 |
|
return Promise\Stream\first($connection); |
164
|
|
|
} |
165
|
|
|
})->then(function ($chunk) use ($options, $connection) { |
166
|
1 |
|
list($confirmation) = explode(PHP_EOL, $chunk); |
167
|
1 |
|
if ($confirmation === 'ack') { |
168
|
1 |
|
return Promise\resolve(new Messenger($connection, $options)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return Promise\reject(new \RuntimeException('Handshake failed')); |
172
|
|
|
})->always(function () use ($server) { |
173
|
1 |
|
$server->close(); |
174
|
1 |
|
})->done($resolve, $reject); |
175
|
1 |
|
} |
176
|
|
|
); |
177
|
|
|
$server->on('error', function ($et) use ($reject) { |
178
|
|
|
$reject($et); |
179
|
1 |
|
}); |
180
|
|
|
|
181
|
1 |
|
$process->start($loop); |
182
|
|
|
}, function () use ($server, $process) { |
183
|
|
|
$server->close(); |
184
|
|
|
$process->terminate(); |
185
|
|
|
}))->then(function (Messenger $messenger) use ($loop, $process) { |
186
|
1 |
|
$loop->addPeriodicTimer(self::INTERVAL, function ($timer) use ($messenger, $loop, $process) { |
187
|
1 |
|
if (!$process->isRunning()) { |
188
|
1 |
|
$loop->cancelTimer($timer); |
189
|
|
|
|
190
|
1 |
|
$exitCode = $process->getExitCode(); |
191
|
1 |
|
if ($exitCode === 0) { |
192
|
1 |
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$messenger->crashed($exitCode); |
196
|
|
|
} |
197
|
1 |
|
}); |
198
|
|
|
|
199
|
1 |
|
return $messenger; |
200
|
1 |
|
}); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
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: