Class mikehaertl\shellcommand\Command
Inheritance | mikehaertl\shellcommand\Command |
---|
Command
This class represents a shell command.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$captureStdErr | bool | Whether to capture stderr (2>&1) when useExec is true. |
mikehaertl\shellcommand\Command |
$escapeArgs | bool | Whether to escape any argument passed through addArg() . |
mikehaertl\shellcommand\Command |
$escapeCommand | bool | Whether to escape the command passed to setCommand() or the constructor. |
mikehaertl\shellcommand\Command |
$locale | null|string | The locale to temporarily set before calling escapeshellargs() . |
mikehaertl\shellcommand\Command |
$procCwd | string|null | The initial working dir for proc_open() . |
mikehaertl\shellcommand\Command |
$procEnv | array|null | An array with environment variables to pass to proc_open() . |
mikehaertl\shellcommand\Command |
$procOptions | array|null | An array of other_options for proc_open() . |
mikehaertl\shellcommand\Command |
$useExec | bool | Whether to use exec() instead of proc_open() . |
mikehaertl\shellcommand\Command |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$_args | array | The list of command arguments | mikehaertl\shellcommand\Command |
$_command | string | The command to execute | mikehaertl\shellcommand\Command |
$_error | string | The error message | mikehaertl\shellcommand\Command |
$_execCommand | string | The full command string to execute | mikehaertl\shellcommand\Command |
$_executed | bool | Whether the command was successfully executed | mikehaertl\shellcommand\Command |
$_exitCode | int | The exit code | mikehaertl\shellcommand\Command |
$_stdErr | string | The stderr output | mikehaertl\shellcommand\Command |
$_stdOut | string | The stdout output | mikehaertl\shellcommand\Command |
Public Methods
Property Details
The list of command arguments
The command to execute
The error message
The full command string to execute
Whether the command was successfully executed
The exit code
The stderr output
The stdout output
Whether to capture stderr (2>&1) when useExec
is true. This will try to redirect the
stderr to stdout and provide the complete output of both in getStdErr()
and getError()
.
Default is true
.
Whether to escape any argument passed through addArg()
. Default is true
.
Whether to escape the command passed to setCommand()
or the constructor.
This is only useful if $escapeArgs
is false
. Default is false
.
The locale to temporarily set before calling escapeshellargs()
. Default is null
for none.
The initial working dir for proc_open()
. Default is null
for current PHP working dir.
An array with environment variables to pass to proc_open()
. Default is null
for none.
An array of other_options for proc_open()
. Default is null
for none.
Whether to use exec()
instead of proc_open()
. This can be used on Windows system
to workaround some quirks there. Note, that any errors from your command will be output directly
to the PHP output stream. getStdErr()
will also not work anymore and thus you also won't get
the error output from getError()
in this case. You also can't pass any environment
variables to the command if this is enabled. Default is false
.
Method Details
public void __construct ( $options = null ) | ||
$options | string|array | Either a command string or an options array (see setOptions()) |
public string __toString ( ) | ||
return | string | The current command string to execute |
---|
public static addArg ( $key, $value = null, $escape = null ) | ||
$key | string | The argument key to add e.g. |
$value | string|array|null | The optional argument value which will get escaped if $escapeArgs is true.
An array can be passed to add more than one value for a key, e.g. |
$escape | bool|null | If set, this overrides the $escapeArgs setting and enforces escaping/no escaping |
return | static | For method chaining |
---|
Execute the command
public bool execute ( ) | ||
return | bool | Whether execution was successful. If false, error details can be obtained through getError(), getStdErr() and getExitCode(). |
---|
public string getArgs ( ) | ||
return | string | The command args that where set through setArgs() or added with addArg() separated by spaces |
---|
public string|null getCommand ( ) | ||
return | string|null | The command that was set through setCommand() or passed to the constructor. Null if none. |
---|
public string getError ( ) | ||
return | string | The error message, either stderr or internal message. Empty if none. |
---|
public string|bool getExecCommand ( ) | ||
return | string|bool | The full command string to execute. If no command was set with setCommand() or passed to the constructor it will return false. |
---|
public string getExecuted ( ) | ||
return | string | Whether the command was successfully executed |
---|
public int|null getExitCode ( ) | ||
return | int|null | The exit code or null if command was not executed yet |
---|
public bool getIsWindows ( ) | ||
return | bool | Whether we are on a Windows OS |
---|
public string getOutput ( ) | ||
return | string | The command output (stdout). Empty if none. |
---|
public string getStdErr ( ) | ||
return | string | The stderr output. Empty if none. |
---|
public static setArgs ( $args ) | ||
$args | string | The command arguments as string. Note that these will not get escaped! |
return | static | For method chaining |
---|
public static setCommand ( $command ) | ||
$command | string | The command or full command string to execute, like 'gzip' or 'gzip -d'. You can still call addArg() to add more arguments to the command. If $escapeCommand was set to true, the command gets escaped through escapeshellcmd(). |
return | static | For method chaining |
---|
public static setOptions ( $options ) | ||
$options | array | Array of name => value options that should be applied to the object
You can also pass options that use a setter, e.g. you can pass a |
return | static | For method chaining |
---|