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
|
|
|
/** |
22
|
|
|
* @param string $process |
|
|
|
|
23
|
|
|
* @param LoopInterface $loop |
24
|
|
|
* @param array $options |
25
|
|
|
* @param mixed $class |
26
|
|
|
* @return Promise\PromiseInterface<Overwatch> |
|
|
|
|
27
|
|
|
*/ |
28
|
2 |
|
public static function parent( |
29
|
|
|
$class, |
30
|
|
|
LoopInterface $loop, |
31
|
|
|
array $options = [] |
32
|
|
|
) { |
33
|
2 |
|
if (!is_subclass_of($class, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) { |
34
|
1 |
|
throw new \Exception('Given class doesn\'t implement ChildInterface'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return new Promise\Promise(function ($resolve, $reject) use ($class, $loop, $options) { |
38
|
1 |
|
$server = new Server('127.0.0.1:0', $loop); |
39
|
1 |
|
$options['address'] = $server->getAddress(); |
40
|
|
|
|
41
|
1 |
|
$template = '%s'; |
42
|
1 |
|
if (isset($options['cmdTemplate'])) { |
43
|
|
|
$template = $options['cmdTemplate']; |
44
|
|
|
unset($options['cmdTemplate']); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$phpBinary = \escapeshellarg(PHP_BINARY . (PHP_SAPI === 'phpdbg' ? ' -qrr --' : '')); |
48
|
1 |
|
$childProcessPath = \escapeshellarg(__DIR__ . DIRECTORY_SEPARATOR . 'child-process.php'); |
49
|
1 |
|
$argvString = \escapeshellarg(ArgvEncoder::encode($options)); |
50
|
1 |
|
$command = $phpBinary . ' ' . $childProcessPath; |
51
|
1 |
|
$process = new Process( |
52
|
1 |
|
sprintf( |
53
|
1 |
|
$template, |
54
|
1 |
|
$command . ' ' . $argvString |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$server->on('connection', function (ConnectionInterface $connection) use ($server, $resolve, $reject, $class, $options) { |
59
|
1 |
|
$server->close(); |
60
|
1 |
|
$messenger = new Messenger($connection, $options); |
61
|
1 |
|
$resolve($messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [ |
62
|
1 |
|
'className' => $class, |
63
|
|
|
]))->then(function () use ($messenger) { |
64
|
1 |
|
return Promise\resolve($messenger); |
65
|
1 |
|
}, $reject)); |
66
|
1 |
|
}); |
67
|
|
|
$server->on('error', function ($et) use ($reject) { |
68
|
|
|
$reject($et); |
69
|
1 |
|
}); |
70
|
|
|
|
71
|
1 |
|
$process->start($loop); |
72
|
1 |
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param LoopInterface $loop |
77
|
|
|
* @param array $options |
78
|
|
|
* @param callable $termiteCallable |
79
|
|
|
* @return Promise\PromiseInterface<Messenger> |
|
|
|
|
80
|
|
|
*/ |
81
|
1 |
|
public static function child(LoopInterface $loop, array $options = [], callable $termiteCallable = null) |
82
|
|
|
{ |
83
|
|
|
return (new Connector($loop))->connect($options['address'])->then(function (ConnectionInterface $connection) use ($options) { |
84
|
1 |
|
return new Messenger($connection, $options); |
85
|
|
|
})->then(function (Messenger $messenger) use ($loop, $termiteCallable) { |
86
|
1 |
|
if ($termiteCallable === null) { |
87
|
|
|
$termiteCallable = function () use ($loop) { |
|
|
|
|
88
|
1 |
|
$loop->addTimer( |
89
|
1 |
|
self::TERMINATE_TIMEOUT, |
90
|
|
|
[ |
91
|
1 |
|
$loop, |
92
|
1 |
|
'stop', |
93
|
|
|
] |
94
|
|
|
); |
95
|
1 |
|
}; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$messenger->registerRpc( |
99
|
1 |
|
Messenger::TERMINATE_RPC, |
100
|
1 |
|
function (Payload $payload, Messenger $messenger) use ($termiteCallable) { |
101
|
1 |
|
$messenger->emit('terminate', [ |
102
|
1 |
|
$messenger, |
103
|
|
|
]); |
104
|
1 |
|
$termiteCallable($payload, $messenger); |
105
|
|
|
|
106
|
1 |
|
return Promise\resolve([]); |
107
|
1 |
|
} |
108
|
|
|
); |
109
|
|
|
|
110
|
1 |
|
return $messenger; |
111
|
1 |
|
}); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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: