|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use React\EventLoop\LoopInterface; |
|
4
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface; |
|
5
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessagesFactory; |
|
6
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
|
7
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messenger; |
|
8
|
|
|
|
|
9
|
|
|
final class ExamplesChildProcess implements ChildInterface |
|
10
|
|
|
{ |
|
11
|
|
|
public static function create(Messenger $messenger, LoopInterface $loop): void |
|
12
|
|
|
{ |
|
13
|
|
|
$messenger->registerRpc('error', function (Payload $payload) { |
|
|
|
|
|
|
14
|
|
|
throw new Exception('whoops'); |
|
15
|
|
|
}); |
|
16
|
|
|
|
|
17
|
|
|
$messenger->registerRpc('isPrime', function (Payload $payload) { |
|
18
|
|
|
return \React\Promise\resolve([ |
|
19
|
|
|
'isPrime' => self::isPrime($payload['number']), |
|
20
|
|
|
]); |
|
21
|
|
|
}); |
|
22
|
|
|
$messenger->on('message', function (Payload $payload, Messenger $messenger) { |
|
23
|
|
|
$messenger->message(MessagesFactory::message([ |
|
24
|
|
|
'time' => (new DateTime('@' . $payload['time'] * $payload['i']))->format('c'), |
|
25
|
|
|
])); |
|
26
|
|
|
}); |
|
27
|
|
|
$messenger->registerRpc('format', function (Payload $payload) { |
|
28
|
|
|
return \React\Promise\resolve([ |
|
29
|
|
|
'formattedTime' => (new DateTime('@' . $payload['unixTime']))->format('c'), |
|
30
|
|
|
]); |
|
31
|
|
|
}); |
|
32
|
|
|
$messenger->registerRpc('overflow', function () { |
|
33
|
|
|
\ini_set('memory_limit', '20M'); |
|
34
|
|
|
|
|
35
|
|
|
$string = ''; |
|
36
|
|
|
while (true) { |
|
37
|
|
|
$string .= '0123456789'; |
|
38
|
|
|
} |
|
39
|
|
|
}); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function isPrime($n) |
|
43
|
|
|
{ |
|
44
|
|
|
// Source: https://stackoverflow.com/a/39743570 |
|
45
|
|
|
for ($i = $n >> 1; $i && $n % $i--; /* void */) { |
|
46
|
|
|
/* void */ |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return !$i && $n > 1; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.