Skip to content

Commit 8f70e6f

Browse files
author
James Newell
committed
got php-process working on another windows computer which was failing
1 parent 7732fbe commit 8f70e6f

File tree

2 files changed

+31
-100
lines changed

2 files changed

+31
-100
lines changed

src/deit/process/Process.php

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use deit\stream\PhpOutputStream;
1111
use deit\stream\NullInputStream;
1212
use deit\stream\NullOutputStream;
13+
use deit\stream\RewindBeforeReadInputStream;
1314

1415
/**
1516
* Process
@@ -97,50 +98,46 @@ public static function exec($command, array $options = array()) {
9798

9899
do {
99100

100-
//fetch all stdout data before the process ends
101-
while (!$spawn->getOutputStream()->end()) {
102-
103-
//fetch stdout data
104-
$buffer = $spawn->getOutputStream()->read(1024);
105-
106-
//write to the stream or call the function
107-
if (!empty($buffer)) {
108-
if ($options['stdout'] instanceof OutputStream) {
109-
$options['stdout']->write($buffer);
110-
} else {
111-
call_user_func($options['stdout'], $buffer);
112-
}
113-
}
114-
115-
}
116-
117-
//fetch all stderr data before the process ends
118-
while (!$spawn->getErrorStream()->end()) {
119-
120-
//fetch stderr data
121-
$buffer = $spawn->getErrorStream()->read(1024);
122-
123-
//write to the stream or call the function
124-
if (!empty($buffer)) {
125-
if ($options['stderr'] instanceof OutputStream) {
126-
$options['stderr']->write($buffer);
127-
} else {
128-
call_user_func($options['stderr'], $buffer);
129-
}
130-
}
131-
132-
}
101+
//fetch all stdout and stderr data before the process ends
102+
self::pipeStream($spawn->getOutputStream(), $options['stdout']);
103+
self::pipeStream($spawn->getErrorStream(), $options['stderr']);
133104

134105
//todo: allow the user to specify a timeout option
135106

136107
} while ($spawn->isRunning() || !$spawn->getOutputStream()->end() || !$spawn->getErrorStream()->end());
137108

109+
//fetch all stdout and stderr data after the process ends
110+
self::pipeStream($spawn->getOutputStream(), $options['stdout']);
111+
self::pipeStream($spawn->getErrorStream(), $options['stderr']);
138112

139113
$spawn->wait(); //todo: allow the user to specify a timeout option
140114

141115
return $spawn->getExitCode();
142116
}
143117

118+
/**
119+
* Pipes input from the stream to the callback
120+
* @param InputStream $in
121+
* @param OutputStream|callable $out
122+
*/
123+
static private function pipeStream($in, $out) {
124+
do {
125+
126+
//fetch stderr data
127+
$buffer = $in->read(1024);
128+
129+
//write to the stream or call the function
130+
if (!empty($buffer)) {
131+
if ($out instanceof OutputStream) {
132+
$out->write($buffer);
133+
} else {
134+
call_user_func($out, $buffer);
135+
}
136+
}
137+
138+
}while (!$in->end()) ;
139+
}
140+
144141
/**
145142
* The process resource
146143
* @var resource
@@ -316,7 +313,7 @@ public function getOutputStream() {
316313
if (!isset($this->streams[self::PIPE_STDOUT])) {
317314
$this->streams[self::PIPE_STDOUT] = new PhpInputStream($this->pipes[self::PIPE_STDOUT], false);
318315
if (OS::isWin()) {
319-
$this->streams[self::PIPE_STDOUT] = new ProcessInputStream($this->streams[self::PIPE_STDOUT], $this);
316+
$this->streams[self::PIPE_STDOUT] = new RewindBeforeReadInputStream($this->streams[self::PIPE_STDOUT]);
320317
}
321318
}
322319
return $this->streams[self::PIPE_STDOUT];

src/deit/process/ProcessInputStream.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)