5
5
use Evenement \EventEmitter ;
6
6
use React \EventLoop \LoopInterface ;
7
7
use React \Stream \ReadableResourceStream ;
8
+ use React \Stream \ReadableStreamInterface ;
8
9
use React \Stream \WritableResourceStream ;
10
+ use React \Stream \WritableStreamInterface ;
9
11
10
12
/**
11
13
* Process component.
17
19
*/
18
20
class Process extends EventEmitter
19
21
{
22
+ /**
23
+ * @var ?WritableStreamInterface
24
+ */
20
25
public $ stdin ;
26
+
27
+ /**
28
+ * @var ?ReadableStreamInterface
29
+ */
21
30
public $ stdout ;
31
+
32
+ /**
33
+ * @var ?ReadableStreamInterface
34
+ */
22
35
public $ stderr ;
23
36
37
+ /**
38
+ * Array with all process pipes (once started)
39
+ * - 0: STDIN (`WritableStreamInterface`)
40
+ * - 1: STDOUT (`ReadableStreamInterface`)
41
+ * - 2: STDERR (`ReadableStreamInterface`)
42
+ *
43
+ * @var ReadableStreamInterface|WritableStreamInterface
44
+ */
45
+ public $ pipes = array ();
46
+
24
47
private $ cmd ;
25
48
private $ cwd ;
26
49
private $ env ;
27
50
private $ enhanceSigchildCompatibility ;
28
- private $ pipes ;
51
+ private $ sigchildPipe ;
29
52
30
53
private $ process ;
31
54
private $ status ;
@@ -90,13 +113,15 @@ public function start(LoopInterface $loop, $interval = 0.1)
90
113
array ('pipe ' , 'w ' ), // stderr
91
114
);
92
115
116
+ $ sigchild = null ;
93
117
// Read exit code through fourth pipe to work around --enable-sigchild
94
118
if ($ this ->enhanceSigchildCompatibility ) {
95
119
$ fdSpec [] = array ('pipe ' , 'w ' );
96
- $ cmd = sprintf ('(%s) 3>/dev/null; code=$?; echo $code >&3; exit $code ' , $ cmd );
120
+ $ sigchild = 3 ;
121
+ $ cmd = sprintf ('(%s) ' . $ sigchild . '>/dev/null; code=$?; echo $code >& ' . $ sigchild . '; exit $code ' , $ cmd );
97
122
}
98
123
99
- $ this ->process = proc_open ($ cmd , $ fdSpec , $ this -> pipes , $ this ->cwd , $ this ->env );
124
+ $ this ->process = proc_open ($ cmd , $ fdSpec , $ pipes , $ this ->cwd , $ this ->env );
100
125
101
126
if (!is_resource ($ this ->process )) {
102
127
throw new \RuntimeException ('Unable to launch a new process. ' );
@@ -129,11 +154,24 @@ public function start(LoopInterface $loop, $interval = 0.1)
129
154
});
130
155
};
131
156
132
- $ this ->stdin = new WritableResourceStream ($ this ->pipes [0 ], $ loop );
133
- $ this ->stdout = new ReadableResourceStream ($ this ->pipes [1 ], $ loop );
134
- $ this ->stdout ->on ('close ' , $ streamCloseHandler );
135
- $ this ->stderr = new ReadableResourceStream ($ this ->pipes [2 ], $ loop );
136
- $ this ->stderr ->on ('close ' , $ streamCloseHandler );
157
+ if ($ sigchild !== null ) {
158
+ $ this ->sigchildPipe = $ pipes [$ sigchild ];
159
+ unset($ pipes [$ sigchild ]);
160
+ }
161
+
162
+ foreach ($ pipes as $ n => $ fd ) {
163
+ if ($ n === 0 ) {
164
+ $ stream = new WritableResourceStream ($ fd , $ loop );
165
+ } else {
166
+ $ stream = new ReadableResourceStream ($ fd , $ loop );
167
+ $ stream ->on ('close ' , $ streamCloseHandler );
168
+ }
169
+ $ this ->pipes [$ n ] = $ stream ;
170
+ }
171
+
172
+ $ this ->stdin = $ this ->pipes [0 ];
173
+ $ this ->stdout = $ this ->pipes [1 ];
174
+ $ this ->stderr = $ this ->pipes [2 ];
137
175
}
138
176
139
177
/**
@@ -337,11 +375,11 @@ public final static function setSigchildEnabled($sigchild)
337
375
*/
338
376
private function pollExitCodePipe ()
339
377
{
340
- if ( ! isset ( $ this ->pipes [ 3 ]) ) {
378
+ if ($ this ->sigchildPipe === null ) {
341
379
return ;
342
380
}
343
381
344
- $ r = array ($ this ->pipes [ 3 ] );
382
+ $ r = array ($ this ->sigchildPipe );
345
383
$ w = $ e = null ;
346
384
347
385
$ n = @stream_select ($ r , $ w , $ e , 0 );
@@ -364,12 +402,12 @@ private function pollExitCodePipe()
364
402
*/
365
403
private function closeExitCodePipe ()
366
404
{
367
- if ( ! isset ( $ this ->pipes [ 3 ]) ) {
405
+ if ($ this ->sigchildPipe === null ) {
368
406
return ;
369
407
}
370
408
371
- fclose ($ this ->pipes [ 3 ] );
372
- unset( $ this ->pipes [ 3 ]) ;
409
+ fclose ($ this ->sigchildPipe );
410
+ $ this ->sigchildPipe = null ;
373
411
}
374
412
375
413
/**
0 commit comments