@@ -50,6 +50,25 @@ the contents of the output and
5050:method: `Symfony\\ Component\\ Process\\ Process::clearErrorOutput ` clears
5151the contents of the error output.
5252
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+
5372The ``mustRun() `` method is identical to ``run() ``, except that it will throw
5473a :class: `Symfony\\ Component\\ Process\\ Exception\\ ProcessFailedException `
5574if the process couldn't be executed successfully (i.e. the process exited
@@ -128,6 +147,50 @@ are done doing other stuff::
128147 which means that your code will halt at this line until the external
129148 process is completed.
130149
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+
131194Stopping a Process
132195------------------
133196
0 commit comments