Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 35e65db

Browse filesBrowse files
committed
minor #9988 [Process] document command-as-arrays (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [Process] document command-as-arrays Fixed #9897 Commits ------- b638c88 [Process] document command-as-arrays
2 parents be4a69e + b638c88 commit 35e65db
Copy full SHA for 35e65db

File tree

Expand file treeCollapse file tree

1 file changed

+49
-67
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+49
-67
lines changed

‎components/process.rst

Copy file name to clipboardExpand all lines: components/process.rst
+49-67Lines changed: 49 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ a command in a sub-process::
2727
use Symfony\Component\Process\Process;
2828
use Symfony\Component\Process\Exception\ProcessFailedException;
2929

30-
$process = new Process('ls -lsa');
30+
$process = new Process(array('ls', '-lsa'));
3131
$process->run();
3232

3333
// executes after the command finishes
@@ -55,7 +55,7 @@ You can also use the :class:`Symfony\\Component\\Process\\Process` class with th
5555
foreach construct to get the output while it is generated. By default, the loop waits
5656
for new output before going to the next iteration::
5757

58-
$process = new Process('ls -lsa');
58+
$process = new Process(array('ls', '-lsa'));
5959
$process->start();
6060

6161
foreach ($process as $type => $data) {
@@ -72,7 +72,7 @@ for new output before going to the next iteration::
7272
it is generated. That iterator is exposed via the ``getIterator()`` method
7373
to allow customizing its behavior::
7474

75-
$process = new Process('ls -lsa');
75+
$process = new Process(array('ls', '-lsa'));
7676
$process->start();
7777
$iterator = $process->getIterator($process::ITER_SKIP_ERR | $process::ITER_KEEP_OUTPUT);
7878
foreach ($iterator as $data) {
@@ -90,7 +90,7 @@ with a non-zero code)::
9090
use Symfony\Component\Process\Exception\ProcessFailedException;
9191
use Symfony\Component\Process\Process;
9292

93-
$process = new Process('ls -lsa');
93+
$process = new Process(array('ls', '-lsa'));
9494

9595
try {
9696
$process->mustRun();
@@ -100,6 +100,38 @@ with a non-zero code)::
100100
echo $exception->getMessage();
101101
}
102102

103+
.. tip::
104+
105+
.. versionadded:: 3.3
106+
The ability to define commands as arrays of arguments was introduced in
107+
Symfony 3.3.
108+
109+
Using array of arguments is the recommended way to define commands. This
110+
saves you from any escaping and allows sending signals seamlessly
111+
(e.g. to stop processes before completion.)::
112+
113+
$process = new Process(array('/path/command', '--flag', 'arg 1', 'etc.'));
114+
115+
If you need use stream redirections, conditional execution, or any other
116+
features provided by the shell of your operating system, you can also define
117+
commands as strings.
118+
119+
Please note that each OS provides a different syntax for their command-lines
120+
so that it becomes your responsibility to deal with escaping and portability.
121+
122+
To provide any variable arguments to command-line string, pass them as
123+
environment variables using the second argument of the ``run()``,
124+
``mustRun()`` or ``start()`` methods. Referencing them is also OS-dependent::
125+
126+
// On Unix-like OSes (Linux, macOS)
127+
$process = new Process('echo "$MESSAGE"');
128+
129+
// On Windows
130+
$process = new Process('echo "!MESSAGE!"');
131+
132+
// On both Unix-like and Windows
133+
$process->run(null, array('MESSAGE' => 'Something to output'));
134+
103135
Getting real-time Process Output
104136
--------------------------------
105137

@@ -110,7 +142,7 @@ anonymous function to the
110142

111143
use Symfony\Component\Process\Process;
112144

113-
$process = new Process('ls -lsa');
145+
$process = new Process(array('ls', '-lsa'));
114146
$process->run(function ($type, $buffer) {
115147
if (Process::ERR === $type) {
116148
echo 'ERR > '.$buffer;
@@ -129,7 +161,7 @@ process, the :method:`Symfony\\Component\\Process\\Process::isRunning` method
129161
to check if the process is done and the
130162
:method:`Symfony\\Component\\Process\\Process::getOutput` method to get the output::
131163

132-
$process = new Process('ls -lsa');
164+
$process = new Process(array('ls', '-lsa'));
133165
$process->start();
134166

135167
while ($process->isRunning()) {
@@ -141,7 +173,7 @@ to check if the process is done and the
141173
You can also wait for a process to end if you started it asynchronously and
142174
are done doing other stuff::
143175

144-
$process = new Process('ls -lsa');
176+
$process = new Process(array('ls', '-lsa'));
145177
$process->start();
146178

147179
// ... do other things
@@ -180,7 +212,7 @@ are done doing other stuff::
180212
a callback that is called repeatedly whilst the process is still running, passing
181213
in the output and its type::
182214

183-
$process = new Process('ls -lsa');
215+
$process = new Process(array('ls', '-lsa'));
184216
$process->start();
185217

186218
$process->wait(function ($type, $buffer) {
@@ -199,7 +231,7 @@ Before a process is started, you can specify its standard input using either the
199231
of the constructor. The provided input can be a string, a stream resource or a
200232
Traversable object::
201233

202-
$process = new Process('cat');
234+
$process = new Process('cat']);
203235
$process->setInput('foobar');
204236
$process->run();
205237

@@ -212,7 +244,7 @@ provides the :class:`Symfony\\Component\\Process\\InputStream` class::
212244
$input = new InputStream();
213245
$input->write('foo');
214246

215-
$process = new Process('cat');
247+
$process = new Process(array('cat'));
216248
$process->setInput($input);
217249
$process->start();
218250

@@ -238,7 +270,7 @@ The input of a process can also be defined using `PHP streams`_::
238270

239271
$stream = fopen('php://temporary', 'w+');
240272

241-
$process = new Process('cat');
273+
$process = new Process(array('cat'));
242274
$process->setInput($stream);
243275
$process->start();
244276

@@ -264,7 +296,7 @@ is sent to the running process. The default signal sent to a process is ``SIGKIL
264296
Please read the :ref:`signal documentation below<reference-process-signal>`
265297
to find out more about signal handling in the Process component::
266298

267-
$process = new Process('ls -lsa');
299+
$process = new Process(array('ls', '-lsa'));
268300
$process->start();
269301

270302
// ... do other things
@@ -285,38 +317,6 @@ instead::
285317
);
286318
$process->run();
287319

288-
To make your code work better on all platforms, you might want to use the
289-
:class:`Symfony\\Component\\Process\\ProcessBuilder` class instead::
290-
291-
use Symfony\Component\Process\ProcessBuilder;
292-
293-
$processBuilder = new ProcessBuilder(array('ls', '-lsa'));
294-
$processBuilder->getProcess()->run();
295-
296-
In case you are building a binary driver, you can use the
297-
:method:`Symfony\\Component\\Process\\ProcessBuilder::setPrefix` method to prefix all
298-
the generated process commands.
299-
300-
The following example will generate two process commands for a tar binary
301-
adapter::
302-
303-
use Symfony\Component\Process\ProcessBuilder;
304-
305-
$processBuilder = new ProcessBuilder();
306-
$processBuilder->setPrefix('/usr/bin/tar');
307-
308-
// '/usr/bin/tar' '--list' '--file=archive.tar.gz'
309-
echo $processBuilder
310-
->setArguments(array('--list', '--file=archive.tar.gz'))
311-
->getProcess()
312-
->getCommandLine();
313-
314-
// '/usr/bin/tar' '-xzf' 'archive.tar.gz'
315-
echo $processBuilder
316-
->setArguments(array('-xzf', 'archive.tar.gz'))
317-
->getProcess()
318-
->getCommandLine();
319-
320320
Process Timeout
321321
---------------
322322

@@ -325,7 +325,7 @@ timeout (in seconds)::
325325

326326
use Symfony\Component\Process\Process;
327327

328-
$process = new Process('ls -lsa');
328+
$process = new Process(array('ls', '-lsa'));
329329
$process->setTimeout(3600);
330330
$process->run();
331331

@@ -357,7 +357,7 @@ considers the time since the last output was produced by the process::
357357

358358
use Symfony\Component\Process\Process;
359359

360-
$process = new Process('something-with-variable-runtime');
360+
$process = new Process(array('something-with-variable-runtime'));
361361
$process->setTimeout(3600);
362362
$process->setIdleTimeout(60);
363363
$process->run();
@@ -373,21 +373,12 @@ When running a program asynchronously, you can send it POSIX signals with the
373373

374374
use Symfony\Component\Process\Process;
375375

376-
$process = new Process('find / -name "rabbit"');
376+
$process = new Process(array('find', '/', '-name', 'rabbit'));
377377
$process->start();
378378

379379
// will send a SIGKILL to the process
380380
$process->signal(SIGKILL);
381381

382-
.. caution::
383-
384-
Due to some limitations in PHP, if you're using signals with the Process
385-
component, you may have to prefix your commands with `exec`_. Please read
386-
`Symfony Issue#5759`_ and `PHP Bug#39992`_ to understand why this is happening.
387-
388-
POSIX signals are not available on Windows platforms, please refer to the
389-
`PHP documentation`_ for available signals.
390-
391382
Process Pid
392383
-----------
393384

@@ -396,17 +387,11 @@ You can access the `pid`_ of a running process with the
396387

397388
use Symfony\Component\Process\Process;
398389

399-
$process = new Process('/usr/bin/php worker.php');
390+
$process = new Process(array('/usr/bin/php', 'worker.php'));
400391
$process->start();
401392

402393
$pid = $process->getPid();
403394

404-
.. caution::
405-
406-
Due to some limitations in PHP, if you want to get the pid of a symfony Process,
407-
you may have to prefix your commands with `exec`_. Please read
408-
`Symfony Issue#5759`_ to understand why this is happening.
409-
410395
Disabling Output
411396
----------------
412397

@@ -417,7 +402,7 @@ Use :method:`Symfony\\Component\\Process\\Process::disableOutput` and
417402

418403
use Symfony\Component\Process\Process;
419404

420-
$process = new Process('/usr/bin/php worker.php');
405+
$process = new Process(array('/usr/bin/php', 'worker.php'));
421406
$process->disableOutput();
422407
$process->run();
423408

@@ -449,9 +434,6 @@ absolute path of the executable PHP binary available on your server::
449434
$phpBinaryPath = $phpBinaryFinder->find();
450435
// $phpBinaryPath = '/usr/local/bin/php' (the result will be different on your computer)
451436

452-
.. _`Symfony Issue#5759`: https://github.com/symfony/symfony/issues/5759
453-
.. _`PHP Bug#39992`: https://bugs.php.net/bug.php?id=39992
454-
.. _`exec`: https://en.wikipedia.org/wiki/Exec_(operating_system)
455437
.. _`pid`: https://en.wikipedia.org/wiki/Process_identifier
456438
.. _`PHP Documentation`: https://php.net/manual/en/pcntl.constants.php
457439
.. _Packagist: https://packagist.org/packages/symfony/process

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.