PHP Class yii\console\Controller

A console controller consists of one or several actions known as sub-commands. Users call a console command by specifying the corresponding route which identifies a controller action. The yii program is used when calling a console command, like the following: ~~~ yii [--param1=value1 --param2 ...] ~~~ where is a route to a controller action and the params will be populated as properties of a command. See Controller::options for details.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Controller
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$color whether to enable ANSI color in the output. If not set, ANSI color will only be enabled for terminals that support it.
$help whether to display help information about current command.
$interactive whether to run the command interactively.

Public Methods

Method Description
ansiFormat ( string $string ) : string Formats a string with ANSI codes
bindActionParams ( Action $action, array $params ) : array Binds the parameters to the action.
confirm ( string $message, boolean $default = false ) : boolean Asks user to confirm by typing y or n.
getActionArgsHelp ( Action $action ) : array Returns the help information for the anonymous arguments for the action.
getActionHelp ( Action $action ) : string Returns the detailed help information for the specified action.
getActionHelpSummary ( Action $action ) : string Returns a one-line short summary describing the specified action.
getActionOptionsHelp ( Action $action ) : array Returns the help information for the options for the action.
getHelp ( ) : string Returns help information for this controller.
getHelpSummary ( ) : string Returns one-line short summary describing this controller.
getOptionValues ( string $actionID ) : array Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties.
getPassedOptionValues ( ) : array Returns the properties corresponding to the passed options
getPassedOptions ( ) : array Returns the names of valid options passed during execution.
isColorEnabled ( resource $stream = STDOUT ) : boolean Returns a value indicating whether ANSI color is enabled.
optionAliases ( ) : array Returns option alias names.
options ( string $actionID ) : string[] Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name.
prompt ( string $text, array $options = [] ) : string Prompts the user for input and validates it
runAction ( string $id, array $params = [] ) : integer Runs an action with the specified action ID and parameters.
select ( string $prompt, array $options = [] ) : string Gives the user an option to choose from. Giving '?' as an input will show a list of options to choose from and their explanations.
stderr ( string $string ) : integer | boolean Prints a string to STDERR
stdout ( string $string ) : integer | boolean Prints a string to STDOUT

Protected Methods

Method Description
getActionMethodReflection ( Action $action ) : ReflectionMethod
parseDocCommentDetail ( Reflector $reflection ) : string Returns full description from the docblock.
parseDocCommentSummary ( Reflector $reflection ) : string Returns the first line of docblock.
parseDocCommentTags ( Reflector $reflection ) : array Parses the comment block into tags.

Method Details

ansiFormat() public method

You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. Example: echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
public ansiFormat ( string $string ) : string
$string string the string to be formatted
return string

bindActionParams() public method

This method is invoked by [[Action]] when it begins to run with the given parameters. This method will first bind the parameters with the [[options()|options]] available to the action. It then validates the given arguments.
public bindActionParams ( Action $action, array $params ) : array
$action yii\base\Action the action to be bound with parameters
$params array the parameters to be bound to the action
return array the valid parameters that the action can run with.

confirm() public method

Asks user to confirm by typing y or n.
public confirm ( string $message, boolean $default = false ) : boolean
$message string to echo out before waiting for user input
$default boolean this value is returned if no selection is made.
return boolean whether user confirmed. Will return true if [[interactive]] is false.

getActionArgsHelp() public method

The returned value should be an array. The keys are the argument names, and the values are the corresponding help information. Each value must be an array of the following structure: - required: boolean, whether this argument is required. - type: string, the PHP type of this argument. - default: string, the default value of this argument - comment: string, the comment of this argument The default implementation will return the help information extracted from the doc-comment of the parameters corresponding to the action method.
public getActionArgsHelp ( Action $action ) : array
$action yii\base\Action
return array the help information of the action arguments

getActionHelp() public method

Returns the detailed help information for the specified action.
public getActionHelp ( Action $action ) : string
$action yii\base\Action action to get help for
return string the detailed help information for the specified action.

getActionHelpSummary() public method

Returns a one-line short summary describing the specified action.
public getActionHelpSummary ( Action $action ) : string
$action yii\base\Action action to get summary for
return string a one-line short summary describing the specified action.

getActionMethodReflection() protected method

protected getActionMethodReflection ( Action $action ) : ReflectionMethod
$action yii\base\Action
return ReflectionMethod

getActionOptionsHelp() public method

The returned value should be an array. The keys are the option names, and the values are the corresponding help information. Each value must be an array of the following structure: - type: string, the PHP type of this argument. - default: string, the default value of this argument - comment: string, the comment of this argument The default implementation will return the help information extracted from the doc-comment of the properties corresponding to the action options.
public getActionOptionsHelp ( Action $action ) : array
$action yii\base\Action
return array the help information of the action options

getHelp() public method

You may override this method to return customized help. The default implementation returns help information retrieved from the PHPDoc comment.
public getHelp ( ) : string
return string

getHelpSummary() public method

You may override this method to return customized summary. The default implementation returns first line from the PHPDoc comment.
public getHelpSummary ( ) : string
return string

getOptionValues() public method

Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties.
public getOptionValues ( string $actionID ) : array
$actionID string the action id of the current request
return array properties corresponding to the options for the action

getPassedOptionValues() public method

Returns the properties corresponding to the passed options
public getPassedOptionValues ( ) : array
return array the properties corresponding to the passed options

getPassedOptions() public method

Returns the names of valid options passed during execution.
public getPassedOptions ( ) : array
return array the names of the options passed during execution

isColorEnabled() public method

ANSI color is enabled only if [[color]] is set true or is not set and the terminal supports ANSI color.
public isColorEnabled ( resource $stream = STDOUT ) : boolean
$stream resource the stream to check.
return boolean Whether to enable ANSI style in output.

optionAliases() public method

Child classes may override this method to specify alias options.
See also: options()
Since: 2.0.8
public optionAliases ( ) : array
return array the options alias names valid for the action where the keys is alias name for option and value is option name.

options() public method

Child classes may override this method to specify possible options. Note that the values setting via options are not available until [[beforeAction()]] is being called.
public options ( string $actionID ) : string[]
$actionID string the action id of the current request
return string[] the names of the options valid for the action

parseDocCommentDetail() protected method

Returns full description from the docblock.
protected parseDocCommentDetail ( Reflector $reflection ) : string
$reflection Reflector
return string

parseDocCommentSummary() protected method

Returns the first line of docblock.
protected parseDocCommentSummary ( Reflector $reflection ) : string
$reflection Reflector
return string

parseDocCommentTags() protected method

Parses the comment block into tags.
protected parseDocCommentTags ( Reflector $reflection ) : array
$reflection Reflector the comment block
return array the parsed tags

prompt() public method

Prompts the user for input and validates it
public prompt ( string $text, array $options = [] ) : string
$text string prompt string
$options array the options to validate the input: - required: whether it is required or not - default: default value if no input is inserted by the user - pattern: regular expression pattern to validate user input - validator: a callable function to validate input. The function must accept two parameters: - $input: the user input to validate - $error: the error value passed by reference if validation failed. An example of how to use the prompt method with a validator function. ```php $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) { if (strlen($input) !== 4) { $error = 'The Pin must be exactly 4 chars!'; return false; } return true; }); ```
return string the user input

runAction() public method

If the action ID is empty, the method will use [[defaultAction]].
See also: createAction
public runAction ( string $id, array $params = [] ) : integer
$id string the ID of the action to be executed.
$params array the parameters (name-value pairs) to be passed to the action.
return integer the status of the action execution. 0 means normal, other values mean abnormal.

select() public method

Gives the user an option to choose from. Giving '?' as an input will show a list of options to choose from and their explanations.
public select ( string $prompt, array $options = [] ) : string
$prompt string the prompt message
$options array Key-value array of options to choose from
return string An option character the user chose

stderr() public method

You may optionally format the string with ANSI codes by passing additional parameters using the constants defined in [[\yii\helpers\Console]]. Example: $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
public stderr ( string $string ) : integer | boolean
$string string the string to print
return integer | boolean Number of bytes printed or false on error

stdout() public method

You may optionally format the string with ANSI codes by passing additional parameters using the constants defined in [[\yii\helpers\Console]]. Example: $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
public stdout ( string $string ) : integer | boolean
$string string the string to print
return integer | boolean Number of bytes printed or false on error

Property Details

$color public_oe property

whether to enable ANSI color in the output. If not set, ANSI color will only be enabled for terminals that support it.
public $color

$help public_oe property

whether to display help information about current command.
Since: 2.0.10
public $help

$interactive public_oe property

whether to run the command interactively.
public $interactive