-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathfunctions.php
More file actions
86 lines (76 loc) · 2.85 KB
/
functions.php
File metadata and controls
86 lines (76 loc) · 2.85 KB
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
<?php declare(strict_types=1);
namespace Amp\Parallel\Context\Internal;
use Amp\ByteStream\StreamChannel;
use Amp\Cancellation;
use Amp\Future;
use Amp\Parallel\Ipc;
use Amp\Serialization\NativeSerializer;
use Amp\Serialization\SerializationException;
use Amp\Serialization\Serializer;
/** @internal */
function runContext(
string $uri,
string $key,
Cancellation $connectCancellation,
array $argv,
Serializer $serializer = new NativeSerializer(),
): void {
/** @noinspection PhpUnusedLocalVariableInspection */
$argc = \count($argv);
try {
$socket = Ipc\connect($uri, $key, $connectCancellation);
$ipcChannel = new StreamChannel($socket, $socket, $serializer);
$socket = Ipc\connect($uri, $key, $connectCancellation);
$resultChannel = new StreamChannel($socket, $socket, $serializer);
} catch (\Throwable $exception) {
\trigger_error($exception->getMessage(), E_USER_ERROR);
}
try {
if (!isset($argv[0])) {
throw new \Error("No script path given");
}
if (!\is_file($argv[0])) {
throw new \Error(\sprintf(
"No script found at '%s' (be sure to provide the full path to the script)",
$argv[0],
));
}
try {
// Protect current scope by requiring script within another function.
// Using $argc, so it is available to the required script.
$callable = (function () use ($argc, $argv): callable {
/** @psalm-suppress UnresolvableInclude */
return require $argv[0];
})();
} catch (\TypeError $exception) {
throw new \Error(\sprintf(
"Script '%s' did not return a callable function: %s",
$argv[0],
$exception->getMessage(),
), 0, $exception);
} catch (\ParseError $exception) {
throw new \Error(\sprintf(
"Script '%s' contains a parse error: %s",
$argv[0],
$exception->getMessage(),
), 0, $exception);
}
$returnValue = $callable(new ContextChannel($ipcChannel));
$result = new ExitSuccess($returnValue instanceof Future ? $returnValue->await() : $returnValue);
} catch (\Throwable $exception) {
$result = new ExitFailure($exception);
}
try {
try {
$resultChannel->send($result);
} catch (SerializationException $exception) {
// Serializing the result failed. Send the reason why.
$resultChannel->send(new ExitFailure($exception));
}
} catch (\Throwable $exception) {
\trigger_error(\sprintf(
"Could not send result to parent: '%s'; be sure to shutdown the child before ending the parent",
$exception->getMessage(),
), E_USER_ERROR);
}
}