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 $process |
|
|
|
|
40
|
|
|
* @param LoopInterface $loop |
41
|
|
|
* @param array $options |
42
|
|
|
* @param mixed $class |
43
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
44
|
|
|
*/ |
45
|
2 |
|
public static function parentFromClass( |
46
|
|
|
$class, |
47
|
|
|
LoopInterface $loop, |
48
|
|
|
array $options = [] |
49
|
|
|
) { |
50
|
2 |
|
if (!is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) { |
51
|
1 |
|
throw new \Exception('Given class doesn\'t implement ChildInterface'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) { |
55
|
1 |
|
$server = new Server('127.0.0.1:0', $loop); |
56
|
1 |
|
$options['address'] = $server->getAddress(); |
57
|
1 |
|
$options['random'] = bin2hex(random_bytes(512)); |
58
|
|
|
|
59
|
1 |
|
$template = '%s'; |
60
|
1 |
|
if (isset($options['cmdTemplate'])) { |
61
|
|
|
$template = $options['cmdTemplate']; |
62
|
|
|
unset($options['cmdTemplate']); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : '')); |
66
|
1 |
|
$childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php'); |
67
|
1 |
|
$argvString = \escapeshellarg(ArgvEncoder::encode($options)); |
68
|
1 |
|
$command = $phpBinary . ' ' . $childProcessPath; |
69
|
1 |
|
$process = new Process( |
70
|
1 |
|
sprintf( |
71
|
1 |
|
$template, |
72
|
1 |
|
$command . ' ' . $argvString |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
self::startParent($process, $server, $loop, $options)->then(function (Messenger $messenger) use ($class) { |
|
|
|
|
77
|
1 |
|
return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [ |
78
|
1 |
|
'className' => $class, |
79
|
|
|
]))->then(function ($p) use ($messenger) { |
|
|
|
|
80
|
1 |
|
return Promise\resolve($messenger); |
81
|
1 |
|
}); |
82
|
1 |
|
})->done($resolve, $reject); |
83
|
1 |
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param LoopInterface $loop |
88
|
|
|
* @param array $options |
89
|
|
|
* @param callable $termiteCallable |
90
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null) |
93
|
|
|
{ |
94
|
|
|
return (new Connector($loop))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options) { |
95
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($connection, $options) { |
|
|
|
|
96
|
|
|
$connection->write(hash_hmac('sha512', $options['address'], $options['random']) . PHP_EOL); |
97
|
|
View Code Duplication |
Promise\Stream\first($connection)->then(function ($chunk) use ($resolve, $connection, $options) { |
|
|
|
|
98
|
|
|
list($confirmation) = explode(PHP_EOL, $chunk); |
99
|
|
|
if ($confirmation === 'syn') { |
100
|
|
|
$connection->write('ack' . PHP_EOL); |
101
|
|
|
$resolve(new Messenger($connection, $options)); |
102
|
|
|
} |
103
|
|
|
}); |
104
|
|
|
}); |
105
|
|
|
})->then(function (Messenger $messenger) use ($loop, $termiteCallable) { |
106
|
|
|
if ($termiteCallable === null) { |
107
|
|
|
$termiteCallable = function () use ($loop) { |
|
|
|
|
108
|
|
|
$loop->addTimer( |
109
|
|
|
self::TERMINATE_TIMEOUT, |
110
|
|
|
[ |
111
|
|
|
$loop, |
112
|
|
|
'stop', |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
}; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$messenger->registerRpc( |
119
|
|
|
Messenger::TERMINATE_RPC, |
120
|
|
|
function (Payload $payload, Messenger $messenger) use ($termiteCallable) { |
121
|
|
|
$messenger->emit('terminate', [ |
122
|
|
|
$messenger, |
123
|
|
|
]); |
124
|
|
|
$termiteCallable($payload, $messenger); |
125
|
|
|
|
126
|
|
|
return Promise\resolve([]); |
127
|
|
|
} |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $messenger; |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
private static function startParent( |
135
|
|
|
Process $process, |
136
|
|
|
Server $server, |
137
|
|
|
LoopInterface $loop, |
138
|
|
|
array $options |
139
|
|
|
) { |
140
|
|
|
return (new Promise\Promise(function ($resolve, $reject) use ($process, $server, $loop, $options) { |
141
|
1 |
|
$server->on( |
142
|
1 |
|
'connection', |
143
|
|
|
function (ConnectionInterface $connection) use ($server, $resolve, $reject, $options) { |
144
|
|
|
Promise\Stream\first($connection)->then(function ($chunk) use ($server, $options, $connection, $resolve) { |
145
|
1 |
|
list($confirmation) = explode(PHP_EOL, $chunk); |
146
|
1 |
|
if ($confirmation === hash_hmac('sha512', $options['address'], $options['random'])) { |
147
|
1 |
|
$connection->write('syn' . PHP_EOL); |
148
|
1 |
|
$server->close(); |
149
|
|
|
|
150
|
1 |
|
return Promise\Stream\first($connection); |
151
|
|
|
} |
152
|
|
View Code Duplication |
})->then(function ($chunk) use ($options, $connection, $resolve) { |
|
|
|
|
153
|
1 |
|
list($confirmation) = explode(PHP_EOL, $chunk); |
154
|
1 |
|
if ($confirmation === 'ack') { |
155
|
1 |
|
$resolve(new Messenger($connection, $options)); |
156
|
|
|
} |
157
|
1 |
|
}); |
158
|
1 |
|
} |
159
|
|
|
); |
160
|
|
|
$server->on('error', function ($et) use ($reject) { |
161
|
|
|
$reject($et); |
162
|
1 |
|
}); |
163
|
|
|
|
164
|
1 |
|
$process->start($loop); |
165
|
|
|
}))->then(function (Messenger $messenger) use ($loop, $process) { |
166
|
1 |
|
$loop->addPeriodicTimer(self::INTERVAL, function ($timer) use ($messenger, $loop, $process) { |
167
|
1 |
|
if (!$process->isRunning()) { |
168
|
1 |
|
$loop->cancelTimer($timer); |
169
|
|
|
|
170
|
1 |
|
$exitCode = $process->getExitCode(); |
171
|
1 |
|
if ($exitCode === 0) { |
172
|
1 |
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$messenger->crashed($exitCode); |
176
|
|
|
} |
177
|
1 |
|
}); |
178
|
|
|
|
179
|
1 |
|
return $messenger; |
180
|
1 |
|
}); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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: