|
10 | 10 | use deit\stream\PhpOutputStream;
|
11 | 11 | use deit\stream\NullInputStream;
|
12 | 12 | use deit\stream\NullOutputStream;
|
| 13 | +use deit\stream\RewindBeforeReadInputStream; |
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * Process
|
@@ -97,50 +98,46 @@ public static function exec($command, array $options = array()) {
|
97 | 98 |
|
98 | 99 | do {
|
99 | 100 |
|
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']); |
133 | 104 |
|
134 | 105 | //todo: allow the user to specify a timeout option
|
135 | 106 |
|
136 | 107 | } while ($spawn->isRunning() || !$spawn->getOutputStream()->end() || !$spawn->getErrorStream()->end());
|
137 | 108 |
|
| 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']); |
138 | 112 |
|
139 | 113 | $spawn->wait(); //todo: allow the user to specify a timeout option
|
140 | 114 |
|
141 | 115 | return $spawn->getExitCode();
|
142 | 116 | }
|
143 | 117 |
|
| 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 | + |
144 | 141 | /**
|
145 | 142 | * The process resource
|
146 | 143 | * @var resource
|
@@ -316,7 +313,7 @@ public function getOutputStream() {
|
316 | 313 | if (!isset($this->streams[self::PIPE_STDOUT])) {
|
317 | 314 | $this->streams[self::PIPE_STDOUT] = new PhpInputStream($this->pipes[self::PIPE_STDOUT], false);
|
318 | 315 | 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]); |
320 | 317 | }
|
321 | 318 | }
|
322 | 319 | return $this->streams[self::PIPE_STDOUT];
|
|
0 commit comments