8000 [Process] Introduce InputStream and iterator for output · symfony/symfony-docs@2795261 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2795261

Browse files
[Process] Introduce InputStream and iterator for output
1 parent d7724dd commit 2795261

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

components/process.rst

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,32 @@ The ``getOutput()`` method always returns the whole content of the standard
4343
output of the command and ``getErrorOutput()`` the content of the error
4444
output. Alternatively, the :method:`Symfony\\Component\\Process\\Process::getIncrementalOutput`
4545
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.
4747

4848
The :method:`Symfony\\Component\\Process\\Process::clearOutput` method clears
4949
the contents of the output and
5050
:method:`Symfony\\Component\\Process\\Process::clearErrorOutput` clears
5151
the 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+
5372
The ``mustRun()`` method is identical to ``run()``, except that it will throw
5473
a :class:`Symfony\\Component\\Process\\Exception\\ProcessFailedException`
5574
if 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+
131194
Stopping a Process
132195
------------------
133196

0 commit comments

Comments
 (0)
0