@@ -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
@@ -128,6 +143,46 @@ are done doing other stuff::
128
143
which means that your code will halt at this line until the external
129
144
process is completed.
130
145
146
+ Feeding the standard input of a Process
147
+ ---------------------------------------
148
+
149
+ Before a process is started, you can specify its standard input using either the
150
+ :method: `Symfony\\ Component\\ Process\\ Process::setInput ` method or the 4th argument
151
+ of the Process constructor. The provided input can be a string, a stream resource or
152
+ a Traversable object::
153
+
154
+ $process = new Process('cat');
155
+ $process->setInput('foobar');
156
+ $process->run();
157
+
158
+ When this input is fully written to the subprocess standard input, the corresponding
159
+ pipe is closed.
160
+
161
+ In order to write to a subprocess standard input while it is running, the component
162
+ provides the :class: `Symfony\\ Component\\ Process\\ InputStream ` class::
163
+
164
+ $input = new InputStream();
165
+ $input->write('foo');
166
+
167
+ $process = new Process('cat');
168
+ $process->setInput($input);
169
+ $process->start();
170
+
171
+ // ... read process output or do other things
172
+
173
+ $input->write('bar');
174
+ $input->close();
175
+
176
+ $process->wait();
177
+
178
+ // will echo: foobar
179
+ echo $process->getOutput();
180
+
181
+ The :method: `Symfony\\ Component\\ Process\\ InputStream::write ` method accepts scalars,
182
+ stream resources or Traversable objects as argument. As shown is the above example,
183
+ you need to explicitly call the :method: `Symfony\\ Component\\ Process\\ InputStream::close `
184
+ method when you are done writing to the standard input of the subprocess.
185
+
131
186
Stopping a Process
132
187
------------------
133
188
0 commit comments