@@ -43,13 +43,28 @@ The ``getOutput()`` method always returns the whole content of the standard
43
43
output of the command and ``getErrorOutput() `` the content of the error
44
44
output. Alternatively, the :method: `Symfony\\ Component\\ Process\\ Process::getIncrementalOutput `
45
45
and :method: `Symfony\\ Component\\ Process\\ Process::getIncrementalErrorOutput `
46
- methods returns the new outputs since the last call.
46
+ methods return the new output since their last call.
47
47
48
48
The :method: `Symfony\\ Component\\ Process\\ Process::clearOutput ` method clears
49
49
the contents of the output and
50
50
:method: `Symfony\\ Component\\ Process\\ Process::clearErrorOutput ` clears
51
51
the contents of the error output.
52
52
53
+ You can also use the :class: `Symfony\\ Component\\ Process\\ Process ` class with the
54
+ foreach construct to get the output while it is generated. By default, the loop waits
55
+ for new output before going to the next iteration::
56
+
57
+ $process = new Process('ls -lsa');
58
+ $process->start();
59
+
60
+ foreach ($process as $type => $data) {
61
+ if ($process::OUT === $type) {
62
+ echo "\nRead from stdout: ".$data;
63
+ } else { // $process::ERR === $type
64
+ echo "\nRead from stderr: ".$data;
65
+ }
66
+ }
67
+
53
68
The ``mustRun() `` method is identical to ``run() ``, except that it will throw
54
69
a :class: `Symfony\\ Component\\ Process\\ Exception\\ ProcessFailedException `
55
70
if the process couldn't be executed successfully (i.e. the process exited
@@ -68,6 +83,10 @@ with a non-zero code)::
68
83
echo $e->getMessage();
69
84
}
70
85
86
+ .. versionadded :: 3.1
87
+ Streaming the input or the output of a process using iterators
88
+ were added in Symfony 3.1
89
+
71
90
Getting real-time Process Output
72
91
--------------------------------
73
92
@@ -128,6 +147,46 @@ are done doing other stuff::
128
147
which means that your code will halt at this line until the external
129
148
process is completed.
130
149
150
+ Streaming to the standard input of a Process
151
+ --------------------------------------------
152
+
153
+ Before a process is started, you can specify its standard input using either the
154
+ :method: `Symfony\\ Component\\ Process\\ Process::setInput ` method or the 4th argument
155
+ of the constructor. The provided input can be a string, a stream resource or a
156
+ Traversable object::
157
+
158
+ $process = new Process('cat');
159
+ $process->setInput('foobar');
160
+ $process->run();
161
+
162
+ When this input is fully written to the subprocess standard input, the corresponding
163
+ pipe is closed.
164
+
165
+ In order to write to a subprocess standard input while it is running, the component
166
+ provides the :class: `Symfony\\ Component\\ Process\\ InputStream ` class::
167
+
168
+ $input = new InputStream();
169
+ $input->write('foo');
170
+
171
+ $process = new Process('cat');
172
+ $process->setInput($input);
173
+ $process->start();
174
+
175
+ // ... read process output or do other things
176
+
177
+ $input->write('bar');
178
+ $input->close();
179
+
180
+ $process->wait();
181
+
182
+ // will echo: foobar
183
+ echo $process->getOutput();
184
+
185
+ The :method: `Symfony\\ Component\\ Process\\ InputStream::write ` method accepts scalars,
186
+ stream resources or Traversable objects as argument. As shown in the above example,
187
+ you need to explicitly call the :method: `Symfony\\ Component\\ Process\\ InputStream::close `
188
+ method when you are done writing to the standard input of the subprocess.
189
+
131
190
Stopping a Process
132
191
------------------
133
192
0 commit comments