1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ChildProcess\Messenger; |
4
|
|
|
|
5
|
|
|
use React\ChildProcess\Process; |
|
|
|
|
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise\FulfilledPromise; |
8
|
|
|
use React\Stream\Stream; |
9
|
|
|
use React\Stream\Util; |
10
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessengesFactory; |
11
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
12
|
|
|
|
13
|
|
|
class Factory |
14
|
|
|
{ |
15
|
|
|
const INTERVAL = 0.1; |
16
|
|
|
const TIMEOUT = 13; |
17
|
|
|
const TERMINATE_TIMEOUT = 1; |
18
|
|
|
const PROCESS_REGISTER = 'wyrihaximus.react.child-process.messenger.child.register'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Process $process |
22
|
|
|
* @param LoopInterface $loop |
23
|
|
|
* @param array $options |
24
|
|
|
* @param float $interval |
25
|
|
|
* @return \React\Promise\PromiseInterface |
26
|
|
|
*/ |
27
|
2 |
|
public static function parent( |
28
|
|
|
Process $process, |
29
|
|
|
LoopInterface $loop, |
30
|
|
|
array $options = [], |
31
|
|
|
$interval = self::INTERVAL |
32
|
|
|
) { |
33
|
2 |
|
$process->start($loop, $interval); |
34
|
|
|
|
35
|
2 |
|
return \WyriHaximus\React\tickingPromise($loop, $interval, [$process, 'isRunning'])-> |
36
|
|
|
then(function () use ($process, $options) { |
37
|
2 |
|
$messenger = new Messenger($process->stdin, $process->stdout, $process->stderr, [ |
38
|
2 |
|
'read_err' => 'stderr', |
39
|
2 |
|
'read' => 'stdout', |
40
|
2 |
|
'write' => 'stdin', |
41
|
|
|
'callForward' => function ($name, $arguments) use ($process) { |
42
|
1 |
|
return call_user_func_array([$process, $name], $arguments); |
43
|
2 |
|
}, |
44
|
2 |
|
] + $options); |
45
|
|
|
|
46
|
2 |
|
Util::forwardEvents($process, $messenger, [ |
47
|
2 |
|
'exit', |
48
|
|
|
]); |
49
|
|
|
|
50
|
2 |
|
return \React\Promise\resolve($messenger); |
51
|
2 |
|
}) |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param LoopInterface $loop |
57
|
|
|
* @param array $options |
58
|
|
|
* @param null $termiteCallable |
59
|
|
|
* @return Messenger |
60
|
|
|
*/ |
61
|
2 |
|
public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null) |
62
|
|
|
{ |
63
|
2 |
|
$stdin = new Stream(STDIN, $loop); |
64
|
2 |
|
$stdout = new Stream(STDOUT, $loop); |
65
|
2 |
|
$stderr = new Stream(STDERR, $loop); |
66
|
|
|
|
67
|
2 |
|
$messenger = new Messenger($stdin, $stdout, $stderr, [ |
68
|
|
|
'read' => 'stdin', |
69
|
|
|
'write_err' => 'stderr', |
70
|
|
|
'write' => 'stdout', |
71
|
2 |
|
] + $options); |
72
|
|
|
|
73
|
|
|
$closeNStop = function () use ($loop) { |
74
|
|
|
$loop->stop(); |
75
|
2 |
|
}; |
76
|
2 |
|
$stdin->on('close', $closeNStop); |
77
|
2 |
|
$stdout->on('close', $closeNStop); |
78
|
2 |
|
$stderr->on('close', $closeNStop); |
79
|
|
|
|
80
|
2 |
|
if ($termiteCallable === null) { |
81
|
|
|
$termiteCallable = function () use ($loop) { |
82
|
1 |
|
$loop->addTimer( |
83
|
1 |
|
self::TERMINATE_TIMEOUT, |
84
|
|
|
[ |
85
|
1 |
|
$loop, |
86
|
1 |
|
'stop', |
87
|
|
|
] |
88
|
|
|
); |
89
|
2 |
|
}; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$messenger->registerRpc( |
93
|
2 |
|
Messenger::TERMINATE_RPC, |
94
|
|
|
function (Payload $payload, Messenger $messenger) use ($loop, $termiteCallable) { |
95
|
1 |
|
$messenger->emit('terminate', [ |
96
|
1 |
|
$messenger, |
97
|
|
|
]); |
98
|
1 |
|
$termiteCallable($payload, $messenger); |
99
|
|
|
|
100
|
1 |
|
return new FulfilledPromise([]); |
101
|
2 |
|
} |
102
|
|
|
); |
103
|
|
|
|
104
|
2 |
|
return $messenger; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $className |
109
|
|
|
* @param LoopInterface $loop |
110
|
|
|
* @param array $options |
111
|
|
|
* @param float $interval |
112
|
|
|
* @throws \Exception |
113
|
|
|
* @return \React\Promise\PromiseInterface |
114
|
|
|
*/ |
115
|
2 |
|
public static function parentFromClass( |
116
|
|
|
$className, |
117
|
|
|
LoopInterface $loop, |
118
|
|
|
array $options = [], |
119
|
|
|
$interval = self::INTERVAL |
120
|
|
|
) { |
121
|
2 |
|
if (!is_subclass_of($className, 'WyriHaximus\React\ChildProcess\Messenger\ChildInterface')) { |
122
|
1 |
|
throw new \Exception('Given class doesn\'t implement ChildInterface'); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$template = '%s'; |
126
|
1 |
|
if (isset($options['cmdTemplate'])) { |
127
|
|
|
$template = $options['cmdTemplate']; |
128
|
|
|
unset($options['cmdTemplate']); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$command = PHP_BINARY . ' ' . __DIR__ . DIRECTORY_SEPARATOR . 'child-process.php'; |
132
|
1 |
|
$process = new Process( |
133
|
1 |
|
sprintf( |
134
|
|
|
$template, |
135
|
1 |
|
$command . ' ' . ArgvEncoder::encode($options) |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
|
139
|
1 |
|
return static::parent( |
140
|
|
|
$process, |
141
|
|
|
$loop, |
142
|
|
|
$options, |
143
|
1 |
|
$interval |
144
|
|
|
)->then(function (Messenger $messenger) use ($className) { |
145
|
1 |
|
return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, [ |
146
|
1 |
|
'className' => $className, |
147
|
1 |
|
]))->then(function () use ($messenger) { |
148
|
1 |
|
return \React\Promise\resolve($messenger); |
149
|
1 |
|
}); |
150
|
1 |
|
}); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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: