@@ -40,19 +40,6 @@ escaping arguments to prevent security issues. It replaces PHP functions like
40
40
41
41
echo $process->getOutput();
42
42
43
- .. tip ::
44
-
45
- In addition to passing the command binary and its arguments as a string, you
46
- can also pass them as an array, which is useful when building a complex
47
- command programmatically::
48
-
49
- // traditional string based commands
50
- $builder = new Process('ls -lsa');
51
- // same example but using an array
52
- $builder = new Process(array('ls', '-lsa'));
53
- // the array can contain any number of arguments and options
54
- $builder = new Process(array('ls', '-l', '-s', '-a'));
55
-
56
43
The ``getOutput() `` method always returns the whole content of the standard
57
44
output of the command and ``getErrorOutput() `` the content of the error
58
45
output. Alternatively, the :method: `Symfony\\ Component\\ Process\\ Process::getIncrementalOutput `
@@ -110,37 +97,39 @@ with a non-zero code)::
110
97
echo $exception->getMessage();
111
98
}
112
99
113
- .. tip ::
100
+ Using features from the OS shell
101
+ --------------------------------
114
102
115
- .. versionadded :: 3.3
116
- The ability to define commands as arrays of arguments was introduced in
117
- Symfony 3.3.
103
+ .. versionadded :: 4.2
104
+ The ``fromShellCommandline() `` static method was introduced in Symfony 4.2.
118
105
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.)::
106
+ Using array of arguments is the recommended way to define commands. This
107
+ saves you from any escaping and allows sending signals seamlessly
108
+ (e.g. to stop processes before completion.)::
122
109
123
- $process = new Process(array('/path/command', '--flag', 'arg 1', 'etc.'));
110
+ $process = new Process(array('/path/command', '--flag', 'arg 1', 'etc.'));
124
111
125
- If you need to use stream redirections, conditional execution, or any other
126
- feature provided by the shell of your operating system, you can also define
127
- commands as strings.
112
+ If you need to use stream redirections, conditional execution, or any other
113
+ feature provided by the shell of your operating system, you can also define
114
+ commands as strings using the
115
+ :method: `Symfony\\ Component\\ Process\\ Process::fromShellCommandline ` static
116
+ factory.
128
117
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.
118
+ Please note that each OS provides a different syntax for their command-lines
119
+ so that it becomes your responsibility to deal with escaping and portability.
131
120
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::
121
+ To provide any variable arguments to command-line string, pass them as
122
+ environment variables using the second argument of the ``run() ``,
123
+ ``mustRun() `` or ``start() `` methods. Referencing them is also OS-dependent::
135
124
136
- // On Unix-like OSes (Linux, macOS)
137
- $process = new Process('echo "$MESSAGE"');
125
+ // On Unix-like OSes (Linux, macOS)
126
+ $process = Process::fromShellCommandline ('echo "$MESSAGE"');
138
127
139
- // On Windows
140
- $process = new Process('echo "!MESSAGE!"');
128
+ // On Windows
129
+ $process = Process::fromShellCommandline ('echo "!MESSAGE!"');
141
130
142
- // On both Unix-like and Windows
143
- $process->run(null, array('MESSAGE' => 'Something to output'));
131
+ // On both Unix-like and Windows
132
+ $process->run(null, array('MESSAGE' => 'Something to output'));
144
133
145
134
Getting real-time Process Output
146
135
--------------------------------
@@ -261,7 +250,7 @@ Before a process is started, you can specify its standard input using either the
261
250
of the constructor. The provided input can be a string, a stream resource or a
262
251
Traversable object::
263
252
264
- $process = new Process('cat');
253
+ $process = new Process(array( 'cat') );
265
254
$process->setInput('foobar');
266
255
$process->run();
267
256
0 commit comments