1
1
<?php
2
2
declare (strict_types = 1 );
3
3
namespace Greendrake \AsyncProcess ;
4
+
4
5
use Ds \Set ;
5
6
use function React \Async \await ;
6
7
use Psr \Http \Message \RequestInterface as Request ;
7
8
use React \Http \Browser ;
8
- use React \Http \Server ;
9
+ use React \Http \HttpServer ;
9
10
use React \Promise as BasePromise ;
10
11
use React \Socket \ConnectionInterface ;
11
12
use React \Socket \Server as SocketServer ;
12
13
13
- class Promise {
14
+ class Promise
15
+ {
14
16
15
17
protected BasePromise \Deferred $ deferred ;
16
18
protected BasePromise \Promise $ promise ;
@@ -19,7 +21,8 @@ class Promise {
19
21
private static $ sigHoldHandlerSetup = false ;
20
22
private static $ sigHoldDefaultHandler ;
21
23
22
- public function __construct (protected string $ command ) {
24
+ public function __construct (protected string $ command )
25
+ {
23
26
$ this ->deferred = new BasePromise \Deferred ;
24
27
$ this ->promise = $ this ->deferred ->promise ();
25
28
if (!self ::$ sigHoldHandlerSetup ) {
@@ -64,7 +67,7 @@ public function __construct(protected string $command) {
64
67
});
65
68
});
66
69
// Actually run the one-off HTTP server to wait for what the forked process has to say:
67
- $ server = new Server (function (Request $ request ) use (&$ result ) {
70
+ $ server = new HttpServer (function (Request $ request ) use (&$ result ) {
68
71
$ result = unserialize ((string ) $ request ->getBody ());
69
72
});
70
73
$ server ->listen ($ socket );
@@ -74,12 +77,12 @@ public function __construct(protected string $command) {
74
77
pcntl_signal (SIGCHLD , self ::$ sigHoldDefaultHandler );
75
78
$ browser = new Browser ;
76
79
// Define the function that will report results back to the parent:
77
- $ reportBack = function (int $ forkExitCode = 0 , ?int $ jobExitCode = null , ?array $ result = null , ? \Throwable $ error = null ) use ($ browser , $ httpAddress ) {
80
+ $ reportBack = function (int $ forkExitCode = 0 , ?int $ jobExitCode = null , ?array $ result = null , ? \Throwable $ error = null ) use ($ browser , $ httpAddress ) {
78
81
await ($ browser ->post ('http:// ' . $ httpAddress , [], serialize ([
79
82
'success ' => $ error === null ,
80
83
'result ' => $ result ,
81
84
'code ' => $ jobExitCode ,
82
- 'error ' => $ error,
85
+ 'error ' => $ error
83
86
]))->catch (function () {
84
87
// Don't give a fuck. This is the forked background process, and if anything is wrong, no one is gonna hear anyway.
85
88
}));
@@ -127,7 +130,8 @@ public function __construct(protected string $command) {
127
130
}
128
131
}
129
132
130
- public function getPid (): int {
133
+ public function getPid (): int
134
+ {
131
135
return $ this ->pid ;
132
136
}
133
137
@@ -136,7 +140,8 @@ public function get(): BasePromise\PromiseInterface
136
140
return $ this ->promise ;
137
141
}
138
142
139
- private static function findUnusedPort (): int {
143
+ private static function findUnusedPort (): int
144
+ {
140
145
$ tried = new Set ;
141
146
$ add = function (int $ port ) use ($ tried ) {
142
147
$ tried ->add ($ port );
@@ -148,7 +153,8 @@ private static function findUnusedPort(): int {
148
153
return $ port ;
149
154
}
150
155
151
- private static function isPortOpen (int $ port ): bool {
156
+ private static function isPortOpen (int $ port ): bool
157
+ {
152
158
$ result = false ;
153
159
try {
154
160
if ($ pf = fsockopen ('127.0.0.1 ' , $ port )) {
@@ -163,4 +169,4 @@ private static function isPortOpen(int $port): bool {
163
169
return $ result ;
164
170
}
165
171
166
- }
172
+ }
0 commit comments