Skip to content

Commit 6209fde

Browse files
author
James Newell
committed
updated process to return stdout and stderr with files as pipes on Windows
1 parent b5f6247 commit 6209fde

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

composer.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/deit/process/Process.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

33
namespace deit\process;
4-
4+
use deit\platform\System as OS;
55
use deit\stream\StreamUtil;
6+
use deit\stream\Closeable;
67
use deit\stream\InputStream;
78
use deit\stream\OutputStream;
89
use deit\stream\PhpInputStream;
910
use deit\stream\PhpOutputStream;
10-
use deit\platform\System as OS;
11+
use deit\stream\RewindBeforeReadInputStream;
1112

1213
/**
1314
* Process
@@ -47,6 +48,8 @@ public static function spawn($command, array $options = array()) {
4748
public static function exec($command, array $options = array()) {
4849
$spawn = self::spawn($command, $options);
4950

51+
//TODO: use stream_select() to prevent deadlocks
52+
5053
if (isset($options['stdin'])) {
5154
if (!$options['stdin'] instanceof InputStream) {
5255
throw new ProcessException("Invalid stream provided for redirecting process input.");
@@ -182,8 +185,6 @@ private function __construct($command, array $options = array()) {
182185
if (OS::isWin()) {
183186
$this->pipes[self::PIPE_STDOUT] = $spec[self::PIPE_STDOUT];
184187
$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);
187188
}
188189

189190
}
@@ -241,6 +242,9 @@ public function getOutputStream() {
241242
$this->assertIsOpen();
242243
if (!isset($this->streams[self::PIPE_STDOUT])) {
243244
$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+
}
244248
}
245249
return $this->streams[self::PIPE_STDOUT];
246250
}
@@ -253,6 +257,9 @@ public function getErrorStream() {
253257
$this->assertIsOpen();
254258
if (!isset($this->streams[self::PIPE_STDERR])) {
255259
$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+
}
256263
}
257264
return $this->streams[self::PIPE_STDERR];
258265
}
@@ -395,7 +402,7 @@ private function destroy() {
395402
// --- close the pipes ---
396403

397404
foreach ($this->streams as $stream) {
398-
if (!$stream->isClosed()) {
405+
if ($stream instanceof Closable && !$stream->isClosed()) {
399406
$stream->close();
400407
}
401408
}

0 commit comments

Comments
 (0)