@@ -43,13 +43,32 @@ 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
+ .. versionadded :: 3.1
54
+ Support for streaming the output of a process was introduced in
55
+ Symfony 3.1.
56
+
57
+ You can also use the :class: `Symfony\\ Component\\ Process\\ Process ` class with the
58
+ foreach construct to get the output while it is generated. By default, the loop waits
59
+ for new output before going to the next iteration::
60
+
61
+ $process = new Process('ls -lsa');
62
+ $process->start();
63
+
64
+ foreach ($process as $type => $data) {
65
+ if ($process::OUT === $type) {
66
+ echo "\nRead from stdout: ".$data;
67
+ } else { // $process::ERR === $type
68
+ echo "\nRead from stderr: ".$data;
69
+ }
70
+ }
71
+
53
72
The ``mustRun() `` method is identical to ``run() ``, except that it will throw
54
73
a :class: `Symfony\\ Component\\ Process\\ Exception\\ ProcessFailedException `
55
74
if the process couldn't be executed successfully (i.e. the process exited
@@ -128,6 +147,50 @@ 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
+ .. versionadded :: 3.1
154
+ Support for streaming the input of a process was introduced in
155
+ Symfony 3.1.
156
+
157
+ Before a process is started, you can specify its standard input using either the
158
+ :method: `Symfony\\ Component\\ Process\\ Process::setInput ` method or the 4th argument
159
+ of the constructor. The provided input can be a string, a stream resource or a
160
+ Traversable object::
161
+
162
+ $process = new Process('cat');
163
+ $process->setInput('foobar');
164
+ $process->run();
165
+
166
+ When this input is fully written to the subprocess standard input, the corresponding
167
+ pipe is closed.
168
+
169
+ In order to write to a subprocess standard input while it is running, the component
170
+ provides the :class: `Symfony\\ Component\\ Process\\ InputStream ` class::
171
+
172
+ $input = new InputStream();
173
+ $input->write('foo');
174
+
175
+ $process = new Process('cat');
176
+ $process->setInput($input);
177
+ $process->start();
178
+
179
+ // ... read process output or do other things
180
+
181
+ $input->write('bar');
182
+ $input->close();
183
+
184
+ $process->wait();
185
+
186
+ // will echo: foobar
187
+ echo $process->getOutput();
188
+
189
+ The :method: `Symfony\\ Component\\ Process\\ InputStream::write ` method accepts scalars,
190
+ stream resources or Traversable objects as argument. As shown in the above example,
191
+ you need to explicitly call the :method: `Symfony\\ Component\\ Process\\ InputStream::close `
192
+ method when you are done writing to the standard input of the subprocess.
193
+
131
194
Stopping a Process
132
195
------------------
133
196
0 commit comments