@@ -30,7 +30,7 @@ escaping arguments to prevent security issues. It replaces PHP functions like
30
30
use Symfony\Component\Process\Process;
31
31
use Symfony\Component\Process\Exception\ProcessFailedException;
32
32
33
- $process = new Process('ls -lsa');
33
+ $process = new Process(array( 'ls', ' -lsa') );
34
34
$process->run();
35
35
36
36
// executes after the command finishes
@@ -68,7 +68,7 @@ You can also use the :class:`Symfony\\Component\\Process\\Process` class with th
68
68
foreach construct to get the output while it is generated. By default, the loop waits
69
69
for new output before going to the next iteration::
70
70
71
- $process = new Process('ls -lsa');
71
+ $process = new Process(array( 'ls', ' -lsa') );
72
72
$process->start();
73
73
74
74
foreach ($process as $type => $data) {
@@ -85,7 +85,7 @@ for new output before going to the next iteration::
85
85
it is generated. That iterator is exposed via the ``getIterator() `` method
86
86
to allow customizing its behavior::
87
87
88
- $process = new Process('ls -lsa');
88
+ $process = new Process(array( 'ls', ' -lsa') );
89
89
$process->start();
90
90
$iterator = $process->getIterator($process::ITER_SKIP_ERR | $process::ITER_KEEP_OUTPUT);
91
91
foreach ($iterator as $data) {
@@ -100,7 +100,7 @@ with a non-zero code)::
100
100
use Symfony\Component\Process\Exception\ProcessFailedException;
101
101
use Symfony\Component\Process\Process;
102
102
103
- $process = new Process('ls -lsa');
103
+ $process = new Process(array( 'ls', ' -lsa') );
104
104
105
105
try {
106
106
$process->mustRun();
@@ -110,6 +110,38 @@ with a non-zero code)::
110
110
echo $exception->getMessage();
111
111
}
112
112
113
+ .. tip ::
114
+
115
+ .. versionadded :: 3.3
116
+ The ability to define commands as arrays of arguments was introduced in
117
+ Symfony 3.3.
118
+
119
+ Using array of arguments is the recommended way to define commands. This
120
+ saves you from any escaping and allows sending signals seamlessly
121
+ (e.g. to stop processes before completion.)::
122
+
123
+ $process = new Process(array('/path/command', '--flag', 'arg 1', 'etc.'));
124
+
125
+ If you need use stream redirections, conditional execution, or any other
126
+ features provided by the shell of your operating system, you can also define
127
+ commands as strings.
128
+
129
+ Please note that each OS provides a different syntax for their command-lines
130
+ so that it becomes your responsibility to deal with escaping and portability.
131
+
132
+ To provide any variable arguments to command-line string, pass them as
133
+ environment variables using the second argument of the ``run() ``,
134
+ ``mustRun() `` or ``start() `` methods. Referencing them is also OS-dependent::
135
+
136
+ // On Unix-like OSes (Linux, macOS)
137
+ $process = new Process('echo "$MESSAGE"');
138
+
139
+ // On Windows
140
+ $process = new Process('echo "!MESSAGE!"');
141
+
142
+ // On both Unix-like and Windows
143
+ $process->run(null, array('MESSAGE' => 'Something to output'));
144
+
113
145
Getting real-time Process Output
114
146
--------------------------------
115
147
@@ -120,7 +152,7 @@ anonymous function to the
120
152
121
153
use Symfony\Component\Process\Process;
122
154
123
- $process = new Process('ls -lsa');
155
+ $process = new Process(array( 'ls', ' -lsa') );
124
156
$process->run(function ($type, $buffer) {
125
157
if (Process::ERR === $type) {
126
158
echo 'ERR > '.$buffer;
@@ -139,7 +171,7 @@ process, the :method:`Symfony\\Component\\Process\\Process::isRunning` method
139
171
to check if the process is done and the
140
172
:method: `Symfony\\ Component\\ Process\\ Process::getOutput ` method to get the output::
141
173
142
- $process = new Process('ls -lsa');
174
+ $process = new Process(array( 'ls', ' -lsa') );
143
175
$process->start();
144
176
145
177
while ($process->isRunning()) {
@@ -151,7 +183,7 @@ to check if the process is done and the
151
183
You can also wait for a process to end if you started it asynchronously and
152
184
are done doing other stuff::
153
185
154
- $process = new Process('ls -lsa');
186
+ $process = new Process(array( 'ls', ' -lsa') );
155
187
$process->start();
156
188
157
189
// ... do other things
@@ -190,7 +222,7 @@ are done doing other stuff::
190
222
a callback that is called repeatedly whilst the process is still running, passing
191
223
in the output and its type::
192
224
193
- $process = new Process('ls -lsa');
225
+ $process = new Process(array( 'ls', ' -lsa') );
194
226
$process->start();
195
227
196
228
$process->wait(function ($type, $buffer) {
@@ -209,7 +241,7 @@ Before a process is started, you can specify its standard input using either the
209
241
of the constructor. The provided input can be a string, a stream resource or a
210
242
Traversable object::
211
243
212
- $process = new Process('cat');
244
+ $process = new Process('cat'] );
213
245
$process->setInput('foobar');
214
246
$process->run();
215
247
@@ -222,7 +254,7 @@ provides the :class:`Symfony\\Component\\Process\\InputStream` class::
222
254
$input = new InputStream();
223
255
$input->write('foo');
224
256
225
- $process = new Process('cat');
257
+ $process = new Process(array( 'cat') );
226
258
$process->setInput($input);
227
259
$process->start();
228
260
@@ -248,7 +280,7 @@ The input of a process can also be defined using `PHP streams`_::
248
280
249
281
$stream = fopen('php://temporary', 'w+');
250
282
251
- $process = new Process('cat');
283
+ $process = new Process(array( 'cat') );
252
284
$process->setInput($stream);
253
285
$process->start();
254
286
@@ -274,7 +306,7 @@ is sent to the running process. The default signal sent to a process is ``SIGKIL
274
306
Please read the :ref: `signal documentation below<reference-process-signal> `
275
307
to find out more about signal handling in the Process component::
276
308
277
- $process = new Process('ls -lsa');
309
+ $process = new Process(array( 'ls', ' -lsa') );
278
310
$process->start();
279
311
280
312
// ... do other things
@@ -303,7 +335,7 @@ timeout (in seconds)::
303
335
304
336
use Symfony\Component\Process\Process;
305
337
306
- $process = new Process('ls -lsa');
338
+ $process = new Process(array( 'ls', ' -lsa') );
307
339
$process->setTimeout(3600);
308
340
$process->run();
309
341
@@ -335,7 +367,7 @@ considers the time since the last output was produced by the process::
335
367
336
368
use Symfony\Component\Process\Process;
337
369
338
- $process = new Process('something-with-variable-runtime');
370
+ $process = new Process(array( 'something-with-variable-runtime') );
339
371
$process->setTimeout(3600);
340
372
$process->setIdleTimeout(60);
341
373
$process->run();
@@ -351,21 +383,12 @@ When running a program asynchronously, you can send it POSIX signals with the
351
383
352
384
use Symfony\Component\Process\Process;
353
385
354
- $process = new Process('find / -name " rabbit"' );
386
+ $process = new Process(array( 'find', '/', ' -name', ' rabbit') );
355
387
$process->start();
356
388
357
389
// will send a SIGKILL to the process
358
390
$process->signal(SIGKILL);
359
391
360
- .. caution ::
361
-
362
- Due to some limitations in PHP, if you're using signals with the Process
363
- component, you may have to prefix your commands with `exec `_. Please read
364
- `Symfony Issue#5759 `_ and `PHP Bug#39992 `_ to understand why this is happening.
365
-
366
- POSIX signals are not available on Windows platforms, please refer to the
367
- `PHP documentation `_ for available signals.
368
-
369
392
Process Pid
370
393
-----------
371
394
@@ -374,17 +397,11 @@ You can access the `pid`_ of a running process with the
374
397
375
398
use Symfony\Component\Process\Process;
376
399
377
- $process = new Process('/usr/bin/php worker.php');
400
+ $process = new Process(array( '/usr/bin/php', ' worker.php') );
378
401
$process->start();
379
402
380
403
$pid = $process->getPid();
381
404
382
- .. caution ::
383
-
384
- Due to some limitations in PHP, if you want to get the pid of a symfony Process,
385
- you may have to prefix your commands with `exec `_. Please read
386
- `Symfony Issue#5759 `_ to understand why this is happening.
387
-
388
405
Disabling Output
389
406
----------------
390
407
@@ -395,7 +412,7 @@ Use :method:`Symfony\\Component\\Process\\Process::disableOutput` and
395
412
396
413
use Symfony\Component\Process\Process;
397
414
398
- $process = new Process('/usr/bin/php worker.php');
415
+ $process = new Process(array( '/usr/bin/php', ' worker.php') );
399
416
$process->disableOutput();
400
417
$process->run();
401
418
0 commit comments