Class mikehaertl\shellcommand\Command

Inheritancemikehaertl\shellcommand\Command

Command

This class represents a shell command.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined 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

Hide inherited properties

PropertyTypeDescriptionDefined 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

Property Details

$_args protected property

The list of command arguments

protected array $_args = []
$_command protected property

The command to execute

protected string $_command null
$_error protected property

The error message

protected string $_error ''
$_execCommand protected property

The full command string to execute

protected string $_execCommand null
$_executed protected property

Whether the command was successfully executed

protected bool $_executed false
$_exitCode protected property

The exit code

protected int $_exitCode null
$_stdErr protected property

The stderr output

protected string $_stdErr ''
$_stdOut protected property

The stdout output

protected string $_stdOut ''
$captureStdErr public property

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.

public bool $captureStdErr true
$escapeArgs public property

Whether to escape any argument passed through addArg(). Default is true.

public bool $escapeArgs true
$escapeCommand public property

Whether to escape the command passed to setCommand() or the constructor. This is only useful if $escapeArgs is false. Default is false.

public bool $escapeCommand false
$locale public property

The locale to temporarily set before calling escapeshellargs(). Default is null for none.

public null|string $locale null
$procCwd public property

The initial working dir for proc_open(). Default is null for current PHP working dir.

public string|null $procCwd null
$procEnv public property

An array with environment variables to pass to proc_open(). Default is null for none.

public array|null $procEnv null
$procOptions public property

An array of other_options for proc_open(). Default is null for none.

public array|null $procOptions null
$useExec public property

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.

public bool $useExec false

Method Details

__construct() public method

public void __construct ( $options null )
$options string|array

Either a command string or an options array (see setOptions())

__toString() public method

public string __toString ( )
return string

The current command string to execute

addArg() public method

public static addArg ( $key, $value null, $escape null )
$key string

The argument key to add e.g. --feature or --name=. If the key does not end with and =, the $value will be separated by a space, if any. Keys are not escaped unless $value is null and $escape is true.

$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. addArg('--exclude', array('val1','val2')) which will create the option --exclude 'val1' 'val2'.

$escape bool|null

If set, this overrides the $escapeArgs setting and enforces escaping/no escaping

return static

For method chaining

execute() public method

Execute the command

public bool execute ( )
return bool

Whether execution was successful. If false, error details can be obtained through getError(), getStdErr() and getExitCode().

getArgs() public method

public string getArgs ( )
return string

The command args that where set through setArgs() or added with addArg() separated by spaces

getCommand() public method

public string|null getCommand ( )
return string|null

The command that was set through setCommand() or passed to the constructor. Null if none.

getError() public method

public string getError ( $trim true )
$trim bool

Whether to trim() the return value. The default is true.

return string

The error message, either stderr or internal message. Empty if none.

getExecCommand() public method

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.

getExecuted() public method

public string getExecuted ( )
return string

Whether the command was successfully executed

getExitCode() public method

public int|null getExitCode ( )
return int|null

The exit code or null if command was not executed yet

getIsWindows() public method

public bool getIsWindows ( )
return bool

Whether we are on a Windows OS

getOutput() public method

public string getOutput ( $trim true )
$trim bool

Whether to trim() the return value. The default is true.

return string

The command output (stdout). Empty if none.

getStdErr() public method

public string getStdErr ( $trim true )
$trim bool

Whether to trim() the return value. The default is true.

return string

The stderr output. Empty if none.

setArgs() public method

public static setArgs ( $args )
$args string

The command arguments as string. Note that these will not get escaped!

return static

For method chaining

setCommand() public method

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

setOptions() public method

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 fileName option which will be passed to setFileName().

return static

For method chaining