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