Skip to content

Commit cd14410

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

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

components/process.rst

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,28 @@ 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+
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+
5368
The ``mustRun()`` method is identical to ``run()``, except that it will throw
5469
a :class:`Symfony\\Component\\Process\\Exception\\ProcessFailedException`
5570
if the process couldn't be executed successfully (i.e. the process exited
@@ -128,6 +143,46 @@ are done doing other stuff::
128143
which means that your code will halt at this line until the external
129144
process is completed.
130145

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+
131186
Stopping a Process
132187
------------------
133188

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy