Properties

$escapeArgs

$escapeArgs : bool

Type

bool — whether to escape any argument passed through addArg(). Default is true.

$escapeCommand

$escapeCommand : bool

Type

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

$useExec

$useExec : bool

Type

bool — 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.

$captureStdErr

$captureStdErr : bool

Type

bool — 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`.

$procCwd

$procCwd : string|null

Type

string|null — the initial working dir for proc_open(). Default is null for current PHP working dir.

$procEnv

$procEnv : array|null

Type

array|null — an array with environment variables to pass to proc_open(). Default is null for none.

$procOptions

$procOptions : array|null

Type

array|null — an array of other_options for proc_open(). Default is null for none.

$_command

$_command : string

Type

string — the command to execute

$_args

$_args : array

Type

array — the list of command arguments

$_execCommand

$_execCommand : string

Type

string — the full command string to execute

$_stdOut

$_stdOut : string

Type

string — the stdout output

$_stdErr

$_stdErr : string

Type

string — the stderr output

$_exitCode

$_exitCode : int

Type

int — the exit code

$_error

$_error : string

Type

string — the error message

$_executed

$_executed : bool

Type

bool — whether the command was successfully executed

Methods

__construct()

__construct(string|array  $options = null) : mixed

Parameters

string|array $options

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

Returns

mixed —

setOptions()

setOptions(array  $options) : \Command

Parameters

array $options

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().

Returns

\Command —

for method chaining

setCommand()

setCommand(string  $command) : \Command

Parameters

string $command

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().

Returns

\Command —

for method chaining

getCommand()

getCommand() : string|null

Returns

string|null —

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

getExecCommand()

getExecCommand() : string|bool

Returns

string|bool —

the full command string to execute. If no command was set with setCommand() or passed to the constructor it will return false.

setArgs()

setArgs(string  $args) : \Command

Parameters

string $args

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

Returns

\Command —

for method chaining

getArgs()

getArgs() : string

Returns

string —

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

addArg()

addArg(string  $key, string|array|null  $value = null, bool|null  $escape = null) : \Command

Parameters

string $key

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.

string|array|null $value

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'.

bool|null $escape

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

Returns

\Command —

for method chaining

getOutput()

getOutput() : string

Returns

string —

the command output (stdout). Empty if none.

getError()

getError() : string

Returns

string —

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

getStdErr()

getStdErr() : string

Returns

string —

the stderr output. Empty if none.

getExitCode()

getExitCode() : int|null

Returns

int|null —

the exit code or null if command was not executed yet

getExecuted()

getExecuted() : string

Returns

string —

whether the command was successfully executed

execute()

execute() : bool

Execute the command

Returns

bool —

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

__toString()

__toString() : string

Returns

string —

the current command string to execute