Class kartik\wordreport\utils\Command

Inheritancekartik\wordreport\utils\Command » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable

This Command library helps managing and executing shell commands. Modified version of the Command class from mikehaertl/php-shellcommand.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$args string The command args that where set with setArgs() or added with addArg() separated by spaces kartik\wordreport\utils\Command
$behaviors yii\base\Behavior[] List of behaviors attached to this component. yii\base\Component
$captureStdErr boolean Whether to capture stderr (2>&1) when useExec is true. kartik\wordreport\utils\Command
$command string|null The command that was set through setCommand() or passed to the constructor. kartik\wordreport\utils\Command
$error string The error message, either stderr or an internal message. kartik\wordreport\utils\Command
$escapeArgs boolean Whether to escape any argument passed through addArg(). kartik\wordreport\utils\Command
$escapeCommand boolean Whether to escape the command passed to setCommand() or the constructor. kartik\wordreport\utils\Command
$execCommand string|boolean The full command string to execute. kartik\wordreport\utils\Command
$exitCode integer|null The exit code or null if command was not executed yet kartik\wordreport\utils\Command
$isExecuted boolean Whether the command was successfully executed kartik\wordreport\utils\Command
$isWindows boolean Whether we are on a Windows OS kartik\wordreport\utils\Command
$locale null|string The locale to temporarily set before calling escapeshellargs(). kartik\wordreport\utils\Command
$nonBlockingMode boolean|null Whether to set the stdin/stdout/stderr streams to non-blocking mode when proc_open() is used. kartik\wordreport\utils\Command
$output string The command output (stdout). kartik\wordreport\utils\Command
$postExecuteScript string Post execution scripts that will be executed after the command and separated by && (only for non windows) kartik\wordreport\utils\Command
$preExecuteScript string Pre execution scripts that will be executed before the command and separated by && (only for non windows) kartik\wordreport\utils\Command
$procCwd string|null The initial working dir for proc_open(). kartik\wordreport\utils\Command
$procEnv array|null An array with environment variables to pass to proc_open(). kartik\wordreport\utils\Command
$procOptions array|null An array of other_options for proc_open(). kartik\wordreport\utils\Command
$stdErr string The stderr output. kartik\wordreport\utils\Command
$stdIn string|resource If set, the string will be piped to the command via standard input. kartik\wordreport\utils\Command
$timeout integer The time in seconds after which a command should be terminated. kartik\wordreport\utils\Command
$useExec boolean Whether to use exec() instead of proc_open(). kartik\wordreport\utils\Command

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_args array The list of command arguments kartik\wordreport\utils\Command
$_command string The command to execute kartik\wordreport\utils\Command
$_error string The error message kartik\wordreport\utils\Command
$_execCommand string The full command string to execute kartik\wordreport\utils\Command
$_executed boolean Whether the command was successfully executed kartik\wordreport\utils\Command
$_exitCode integer The exit code kartik\wordreport\utils\Command
$_stdErr string The stderr output kartik\wordreport\utils\Command
$_stdIn null|string|resource To pipe to standard input kartik\wordreport\utils\Command
$_stdOut string The stdout output kartik\wordreport\utils\Command

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addArg() kartik\wordreport\utils\Command
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
execute() Execute the command kartik\wordreport\utils\Command
getArgs() kartik\wordreport\utils\Command
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCommand() kartik\wordreport\utils\Command
getError() kartik\wordreport\utils\Command
getExecCommand() kartik\wordreport\utils\Command
getExitCode() kartik\wordreport\utils\Command
getIsExecuted() kartik\wordreport\utils\Command
getIsWindows() kartik\wordreport\utils\Command
getOutput() kartik\wordreport\utils\Command
getStdErr() kartik\wordreport\utils\Command
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\BaseObject
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
setArgs() kartik\wordreport\utils\Command
setCommand() kartik\wordreport\utils\Command
setStdIn() kartik\wordreport\utils\Command
trigger() Triggers an event. yii\base\Component

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 boolean $_executed false
$_exitCode protected property

The exit code

protected integer $_exitCode null
$_stdErr protected property

The stderr output

protected string $_stdErr ''
$_stdIn protected property

To pipe to standard input

protected null|string|resource $_stdIn null
$_stdOut protected property

The stdout output

protected string $_stdOut ''
$args public property

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

public string getArgs ( )
public static setArgs ( $args )
$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 boolean $captureStdErr true
$command public property

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

public string|null getCommand ( )
public static setCommand ( $command )
$error public read-only property

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

public string getError ( $trim true )
$escapeArgs public property

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

public boolean $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 boolean $escapeCommand false
$execCommand public read-only property

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

$exitCode public read-only property

The exit code or null if command was not executed yet

$isExecuted public read-only property

Whether the command was successfully executed

$isWindows public read-only property

Whether we are on a Windows OS

$locale public property

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

public null|string $locale null
$nonBlockingMode public property

Whether to set the stdin/stdout/stderr streams to non-blocking mode when proc_open() is used. This allows to have huge inputs/outputs without making the process hang. The default is null which will enable the feature on Non-Windows systems. Set it to true or false to manually enable/disable it. It does not work on Windows.

$output public read-only property

The command output (stdout). Empty if none.

public string getOutput ( $trim true )
$postExecuteScript public property

Post execution scripts that will be executed after the command and separated by && (only for non windows)

public string $postExecuteScript null
$preExecuteScript public property

Pre execution scripts that will be executed before the command and separated by && (only for non windows)

public string $preExecuteScript 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
$stdErr public read-only property

The stderr output. Empty if none.

public string getStdErr ( $trim true )
$stdIn public write-only property

If set, the string will be piped to the command via standard input. This enables the same functionality as piping on the command line. It can also be a resource like a file handle or a stream in which case its content will be piped into the command like an input redirection.

public static setStdIn ( $stdIn )
$timeout public property

The time in seconds after which a command should be terminated. This only works in non-blocking mode. Default is null which means the process is never terminated.

public integer $timeout 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 boolean $useExec false

Method Details

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 boolean|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 boolean execute ( )
return boolean

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

getArgs() public method

public string getArgs ( )
return string

The command args that where set with 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 boolean

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

return string

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

getExecCommand() public method

public string|boolean getExecCommand ( )
return string|boolean

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

getExitCode() public method

public integer|null getExitCode ( )
return integer|null

The exit code or null if command was not executed yet

getIsExecuted() public method

public boolean getIsExecuted ( )
return boolean

Whether the command was successfully executed

getIsWindows() public method

public boolean getIsWindows ( )
return boolean

Whether we are on a Windows OS

getOutput() public method

public string getOutput ( $trim true )
$trim boolean

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 boolean

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

return static

For method chaining

setStdIn() public method

public static setStdIn ( $stdIn )
$stdIn string|resource

If set, the string will be piped to the command via standard input. This enables the same functionality as piping on the command line. It can also be a resource like a file handle or a stream in which case its content will be piped into the command like an input redirection.

return static

For method chaining