1
1
<?php
2
2
3
3
namespace deit \process ;
4
-
4
+ use deit \ platform \ System as OS ;
5
5
use deit \stream \StreamUtil ;
6
+ use deit \stream \Closeable ;
6
7
use deit \stream \InputStream ;
7
8
use deit \stream \OutputStream ;
8
9
use deit \stream \PhpInputStream ;
9
10
use deit \stream \PhpOutputStream ;
10
- use deit \platform \ System as OS ;
11
+ use deit \stream \ RewindBeforeReadInputStream ;
11
12
12
13
/**
13
14
* Process
@@ -47,6 +48,8 @@ public static function spawn($command, array $options = array()) {
47
48
public static function exec ($ command , array $ options = array ()) {
48
49
$ spawn = self ::spawn ($ command , $ options );
49
50
51
+ //TODO: use stream_select() to prevent deadlocks
52
+
50
53
if (isset ($ options ['stdin ' ])) {
51
54
if (!$ options ['stdin ' ] instanceof InputStream) {
52
55
throw new ProcessException ("Invalid stream provided for redirecting process input. " );
@@ -182,8 +185,6 @@ private function __construct($command, array $options = array()) {
182
185
if (OS ::isWin ()) {
183
186
$ this ->pipes [self ::PIPE_STDOUT ] = $ spec [self ::PIPE_STDOUT ];
184
187
$ this ->pipes [self ::PIPE_STDERR ] = $ spec [self ::PIPE_STDERR ];
185
- fseek ($ this ->pipes [self ::PIPE_STDOUT ], 0 );
186
- fseek ($ this ->pipes [self ::PIPE_STDERR ], 0 );
187
188
}
188
189
189
190
}
@@ -241,6 +242,9 @@ public function getOutputStream() {
241
242
$ this ->assertIsOpen ();
242
243
if (!isset ($ this ->streams [self ::PIPE_STDOUT ])) {
243
244
$ this ->streams [self ::PIPE_STDOUT ] = new PhpInputStream ($ this ->pipes [self ::PIPE_STDOUT ], false );
245
+ if (OS ::isWin ()) {
246
+ $ this ->streams [self ::PIPE_STDOUT ] = new RewindBeforeReadInputStream ($ this ->streams [self ::PIPE_STDOUT ]);
247
+ }
244
248
}
245
249
return $ this ->streams [self ::PIPE_STDOUT ];
246
250
}
@@ -253,6 +257,9 @@ public function getErrorStream() {
253
257
$ this ->assertIsOpen ();
254
258
if (!isset ($ this ->streams [self ::PIPE_STDERR ])) {
255
259
$ this ->streams [self ::PIPE_STDERR ] = new PhpInputStream ($ this ->pipes [self ::PIPE_STDERR ], false );
260
+ if (OS ::isWin ()) {
261
+ $ this ->streams [self ::PIPE_STDERR ] = new RewindBeforeReadInputStream ($ this ->streams [self ::PIPE_STDERR ]);
262
+ }
256
263
}
257
264
return $ this ->streams [self ::PIPE_STDERR ];
258
265
}
@@ -395,7 +402,7 @@ private function destroy() {
395
402
// --- close the pipes ---
396
403
397
404
foreach ($ this ->streams as $ stream ) {
398
- if (!$ stream ->isClosed ()) {
405
+ if ($ stream instanceof Closable && !$ stream ->isClosed ()) {
399
406
$ stream ->close ();
400
407
}
401
408
}
0 commit comments