12
12
class Command
13
13
{
14
14
/**
15
- * @var bool whether to escape any argument passed through `addArg()`. Default is `true`.
15
+ * @var bool whether to escape any argument passed through `addArg()`.
16
+ * Default is `true`.
16
17
*/
17
18
public $ escapeArgs = true ;
18
19
19
20
/**
20
- * @var bool whether to escape the command passed to `setCommand()` or the constructor.
21
- * This is only useful if `$escapeArgs` is `false`. Default is `false`.
21
+ * @var bool whether to escape the command passed to `setCommand()` or the
22
+ * constructor. This is only useful if `$escapeArgs` is `false`. Default
23
+ * is `false`.
22
24
*/
23
25
public $ escapeCommand = false ;
24
26
25
27
/**
26
- * @var bool whether to use `exec()` instead of `proc_open()`. This can be used on Windows system
27
- * to workaround some quirks there. Note, that any errors from your command will be output directly
28
- * to the PHP output stream. `getStdErr()` will also not work anymore and thus you also won't get
29
- * the error output from `getError()` in this case. You also can't pass any environment
30
- * variables to the command if this is enabled. Default is `false`.
28
+ * @var bool whether to use `exec()` instead of `proc_open()`. This can be
29
+ * used on Windows system to workaround some quirks there. Note, that any
30
+ * errors from your command will be output directly to the PHP output
31
+ * stream. `getStdErr()` will also not work anymore and thus you also won't
32
+ * get the error output from `getError()` in this case. You also can't pass
33
+ * any environment variables to the command if this is enabled. Default is
34
+ * `false`.
31
35
*/
32
36
public $ useExec = false ;
33
37
34
38
/**
35
- * @var bool whether to capture stderr (2>&1) when `useExec` is true. This will try to redirect the
36
- * stderr to stdout and provide the complete output of both in `getStdErr()` and `getError()`.
37
- * Default is `true`.
39
+ * @var bool whether to capture stderr (2>&1) when `useExec` is true. This
40
+ * will try to redirect the stderr to stdout and provide the complete
41
+ * output of both in `getStdErr()` and `getError()`. Default is `true`.
38
42
*/
39
43
public $ captureStdErr = true ;
40
44
41
45
/**
42
- * @var string|null the initial working dir for `proc_open()`. Default is `null` for current PHP working dir.
46
+ * @var string|null the initial working dir for `proc_open()`. Default is
47
+ * `null` for current PHP working dir.
43
48
*/
44
49
public $ procCwd ;
45
50
46
51
/**
47
- * @var array|null an array with environment variables to pass to `proc_open()`. Default is `null` for none.
52
+ * @var array|null an array with environment variables to pass to
53
+ * `proc_open()`. Default is `null` for none.
48
54
*/
49
55
public $ procEnv ;
50
56
51
57
/**
52
- * @var array|null an array of other_options for `proc_open()`. Default is `null` for none.
58
+ * @var array|null an array of other_options for `proc_open()`. Default is
59
+ * `null` for none.
53
60
*/
54
61
public $ procOptions ;
55
62
@@ -63,7 +70,8 @@ class Command
63
70
public $ nonBlockingMode ;
64
71
65
72
/**
66
- * @var null|string the locale to temporarily set before calling `escapeshellargs()`. Default is `null` for none.
73
+ * @var null|string the locale to temporarily set before calling
74
+ * `escapeshellargs()`. Default is `null` for none.
67
75
*/
68
76
public $ locale ;
69
77
@@ -113,7 +121,8 @@ class Command
113
121
protected $ _executed = false ;
114
122
115
123
/**
116
- * @param string|array $options either a command string or an options array (see setOptions())
124
+ * @param string|array $options either a command string or an options array
125
+ * @see setOptions
117
126
*/
118
127
public function __construct ($ options = null )
119
128
{
@@ -125,9 +134,10 @@ public function __construct($options = null)
125
134
}
126
135
127
136
/**
128
- * @param array $options array of name => value options that should be applied to the object
129
- * You can also pass options that use a setter, e.g. you can pass a `fileName` option which
130
- * will be passed to `setFileName()`.
137
+ * @param array $options array of name => value options that should be
138
+ * applied to the object You can also pass options that use a setter, e.g.
139
+ * you can pass a `fileName` option which will be passed to
140
+ * `setFileName()`.
131
141
* @throws \Exception
132
142
* @return static for method chaining
133
143
*/
@@ -149,9 +159,10 @@ public function setOptions($options)
149
159
}
150
160
151
161
/**
152
- * @param string $command the command or full command string to execute, like 'gzip' or 'gzip -d'.
153
- * You can still call addArg() to add more arguments to the command. If $escapeCommand was set to true,
154
- * the command gets escaped through escapeshellcmd().
162
+ * @param string $command the command or full command string to execute,
163
+ * like 'gzip' or 'gzip -d'. You can still call addArg() to add more
164
+ * arguments to the command. If $escapeCommand was set to true, the command
165
+ * gets escaped with escapeshellcmd().
155
166
* @return static for method chaining
156
167
*/
157
168
public function setCommand ($ command )
@@ -160,10 +171,12 @@ public function setCommand($command)
160
171
$ command = escapeshellcmd ($ command );
161
172
}
162
173
if ($ this ->getIsWindows ()) {
163
- // Make sure to switch to correct drive like "E:" first if we have a full path in command
174
+ // Make sure to switch to correct drive like "E:" first if we have
175
+ // a full path in command
164
176
if (isset ($ command [1 ]) && $ command [1 ]===': ' ) {
165
177
$ position = 1 ;
166
- // Could be a quoted absolute path because of spaces. i.e. "C:\Program Files (x86)\file.exe"
178
+ // Could be a quoted absolute path because of spaces.
179
+ // i.e. "C:\Program Files (x86)\file.exe"
167
180
} elseif (isset ($ command [2 ]) && $ command [2 ]===': ' ) {
168
181
$ position = 2 ;
169
182
} else {
@@ -172,18 +185,23 @@ public function setCommand($command)
172
185
173
186
// Absolute path. If it's a relative path, let it slide.
174
187
if ($ position ) {
175
- $ command = sprintf ($ command [$ position - 1 ].': && cd %s && %s ' , escapeshellarg (dirname ($ command )), escapeshellarg (basename ($ command )));
188
+ $ command = sprintf (
189
+ $ command [$ position - 1 ] . ': && cd %s && %s ' ,
190
+ escapeshellarg (dirname ($ command )),
191
+ escapeshellarg (basename ($ command ))
192
+ );
176
193
}
177
194
}
178
195
$ this ->_command = $ command ;
179
196
return $ this ;
180
197
}
181
198
182
199
/**
183
- * @param string|resource $stdIn If set, the string will be piped to the command via standard input.
184
- * This enables the same functionality as piping on the command line.
185
- * It can also be a resource like a file handle or a stream in which case its content will be piped
186
- * into the command like an input redirection.
200
+ * @param string|resource $stdIn If set, the string will be piped to the
201
+ * command via standard input. This enables the same functionality as
202
+ * piping on the command line. It can also be a resource like a file
203
+ * handle or a stream in which case its content will be piped into the
204
+ * command like an input redirection.
187
205
* @return static for method chaining
188
206
*/
189
207
public function setStdIn ($ stdIn ) {
@@ -192,16 +210,18 @@ public function setStdIn($stdIn) {
192
210
}
193
211
194
212
/**
195
- * @return string|null the command that was set through setCommand() or passed to the constructor. Null if none.
213
+ * @return string|null the command that was set through setCommand() or
214
+ * passed to the constructor. `null` if none.
196
215
*/
197
216
public function getCommand ()
198
217
{
199
218
return $ this ->_command ;
200
219
}
201
220
202
221
/**
203
- * @return string|bool the full command string to execute. If no command was set with setCommand()
204
- * or passed to the constructor it will return false.
222
+ * @return string|bool the full command string to execute. If no command
223
+ * was set with setCommand() or passed to the constructor it will return
224
+ * `false`.
205
225
*/
206
226
public function getExecCommand ()
207
227
{
@@ -218,7 +238,8 @@ public function getExecCommand()
218
238
}
219
239
220
240
/**
221
- * @param string $args the command arguments as string. Note that these will not get escaped!
241
+ * @param string $args the command arguments as string. Note that these
242
+ * will not get escaped!
222
243
* @return static for method chaining
223
244
*/
224
245
public function setArgs ($ args )
@@ -228,33 +249,38 @@ public function setArgs($args)
228
249
}
229
250
230
251
/**
231
- * @return string the command args that where set through setArgs() or added with addArg() separated by spaces
252
+ * @return string the command args that where set with setArgs() or added
253
+ * with addArg() separated by spaces
232
254
*/
233
255
public function getArgs ()
234
256
{
235
257
return implode (' ' , $ this ->_args );
236
258
}
237
259
238
260
/**
239
- * @param string $key the argument key to add e.g. `--feature` or `--name=`. If the key does not end with
240
- * and `=`, the $value will be separated by a space, if any. Keys are not escaped unless $value is null
261
+ * @param string $key the argument key to add e.g. `--feature` or
262
+ * `--name=`. If the key does not end with and `=`, the $value will be
263
+ * separated by a space, if any. Keys are not escaped unless $value is null
241
264
* and $escape is `true`.
242
- * @param string|array|null $value the optional argument value which will get escaped if $escapeArgs is true.
243
- * An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))`
244
- * which will create the option `--exclude 'val1' 'val2'`.
245
- * @param bool|null $escape if set, this overrides the $escapeArgs setting and enforces escaping/no escaping
265
+ * @param string|array|null $value the optional argument value which will
266
+ * get escaped if $escapeArgs is true. An array can be passed to add more
267
+ * than one value for a key, e.g. `addArg('--exclude',
268
+ * array('val1','val2'))` which will create the option `--exclude 'val1'
269
+ * 'val2'`.
270
+ * @param bool|null $escape if set, this overrides the $escapeArgs setting
271
+ * and enforces escaping/no escaping
246
272
* @return static for method chaining
247
273
*/
248
274
public function addArg ($ key , $ value = null , $ escape = null )
249
275
{
250
- $ doEscape = $ escape !==null ? $ escape : $ this ->escapeArgs ;
251
- $ useLocale = $ doEscape && $ this ->locale !==null ;
276
+ $ doEscape = $ escape !== null ? $ escape : $ this ->escapeArgs ;
277
+ $ useLocale = $ doEscape && $ this ->locale !== null ;
252
278
253
279
if ($ useLocale ) {
254
280
$ locale = setlocale (LC_CTYPE , 0 ); // Returns current locale setting
255
281
setlocale (LC_CTYPE , $ this ->locale );
256
282
}
257
- if ($ value ===null ) {
283
+ if ($ value === null ) {
258
284
// Only escape single arguments if explicitely requested
259
285
$ this ->_args [] = $ escape ? escapeshellarg ($ key ) : $ key ;
260
286
} else {
@@ -264,9 +290,10 @@ public function addArg($key, $value = null, $escape = null)
264
290
foreach ($ value as $ v ) {
265
291
$ params [] = $ doEscape ? escapeshellarg ($ v ) : $ v ;
266
292
}
267
- $ this ->_args [] = $ key. $ separator .implode (' ' ,$ params );
293
+ $ this ->_args [] = $ key . $ separator .implode (' ' ,$ params );
268
294
} else {
269
- $ this ->_args [] = $ key .$ separator .($ doEscape ? escapeshellarg ($ value ) : $ value );
295
+ $ this ->_args [] = $ key . $ separator .
296
+ ($ doEscape ? escapeshellarg ($ value ) : $ value );
270
297
}
271
298
}
272
299
if ($ useLocale ) {
@@ -287,7 +314,8 @@ public function getOutput($trim = true)
287
314
288
315
/**
289
316
* @param bool $trim whether to `trim()` the return value. The default is `true`.
290
- * @return string the error message, either stderr or internal message. Empty if none.
317
+ * @return string the error message, either stderr or an internal message.
318
+ * Empty string if none.
291
319
*/
292
320
public function getError ($ trim = true )
293
321
{
@@ -322,8 +350,8 @@ public function getExecuted()
322
350
/**
323
351
* Execute the command
324
352
*
325
- * @return bool whether execution was successful. If false, error details can be obtained through
326
- * getError(), getStdErr() and getExitCode().
353
+ * @return bool whether execution was successful. If ` false` , error details
354
+ * can be obtained from getError(), getStdErr() and getExitCode().
327
355
*/
328
356
public function execute ()
329
357
{
@@ -337,7 +365,7 @@ public function execute()
337
365
$ execCommand = $ this ->captureStdErr ? "$ command 2>&1 " : $ command ;
338
366
exec ($ execCommand , $ output , $ this ->_exitCode );
339
367
$ this ->_stdOut = implode ("\n" , $ output );
340
- if ($ this ->_exitCode !==0 ) {
368
+ if ($ this ->_exitCode !== 0 ) {
341
369
$ this ->_stdErr = $ this ->_stdOut ;
342
370
$ this ->_error = empty ($ this ->_stdErr ) ? 'Command failed ' : $ this ->_stdErr ;
343
371
return false ;
@@ -460,6 +488,6 @@ public function getIsWindows()
460
488
*/
461
489
public function __toString ()
462
490
{
463
- return (string )$ this ->getExecCommand ();
491
+ return (string ) $ this ->getExecCommand ();
464
492
}
465
493
}
0 commit comments