CConsoleCommand represents an executable console command.
It works like CController
by parsing command line options and
dispatching the request to a specific action with appropriate option values.
Users call a console command via the following command format:
yiic CommandName ActionName --Option1=Value1 --Option2=Value2 ...
Child classes mainly needs to implement various action methods whose name
must be prefixed with "action". The parameters to an action method are
considered as options for that specific action. The action specified as CConsoleCommand::$defaultAction
will be invoked when a user does not specify the action name in
his command.
Options are bound to action parameters via parameter names. For example, the
following action method will allow us to run a command with yiic sitemap
--type=News
:
class SitemapCommand extends CConsoleCommand {
public function actionIndex($type) {
....
}
}
Since version 1.1.11 the return value of action methods will be used as
application exit code if it is an integer value.
Methods summary
public
|
|
public
|
#
init( )
Initializes the command object. This method is invoked after a command object
is created and initialized with configurations. You may override this method to
further customize the command before it executes.
Initializes the command object. This method is invoked after a command object
is created and initialized with configurations. You may override this method to
further customize the command before it executes.
Since
1.1.6
|
public
array
|
#
behaviors( )
Returns a list of behaviors that this command should behave as. The return
value should be an array of behavior configurations indexed by behavior names.
Each behavior configuration can be either a string specifying the behavior class
or an array of the following structure:
'behaviorName'=>array(
'class'=>'path.to.BehaviorClass',
'property1'=>'value1',
'property2'=>'value2',
)
Returns a list of behaviors that this command should behave as. The return
value should be an array of behavior configurations indexed by behavior names.
Each behavior configuration can be either a string specifying the behavior class
or an array of the following structure:
'behaviorName'=>array(
'class'=>'path.to.BehaviorClass',
'property1'=>'value1',
'property2'=>'value2',
)
Note, the behavior classes must implement IBehavior or extend from
CBehavior . Behaviors declared in this method will be attached to the
controller when it is instantiated.
For more details about behaviors, see CComponent .
Returns
array the behavior configurations (behavior name=>behavior configuration)
Since
1.1.11
|
public
integer
|
#
run( array $args )
Executes the command. The default implementation will parse the input
parameters and dispatch the command request to an appropriate action with the
corresponding option values
Executes the command. The default implementation will parse the input
parameters and dispatch the command request to an appropriate action with the
corresponding option values
Parameters
- $args
array $args command line parameters for this command.
Returns
integer application exit code, which is returned by the invoked action. 0 if the action
did not return anything. (return value is available since version 1.1.11)
|
protected
boolean
|
#
beforeAction( string $action, array $params )
This method is invoked right before an action is to be executed. You may
override this method to do last-minute preparation for the action.
This method is invoked right before an action is to be executed. You may
override this method to do last-minute preparation for the action.
Parameters
- $action
string $action the action name
- $params
array $params the parameters to be passed to the action method.
Returns
boolean whether the action should be executed.
|
protected
integer
|
#
afterAction( string $action, array $params, integer $exitCode = 0 )
This method is invoked right after an action finishes execution. You may
override this method to do some postprocessing for the action.
This method is invoked right after an action finishes execution. You may
override this method to do some postprocessing for the action.
Parameters
- $action
string $action the action name
- $params
array $params the parameters to be passed to the action method.
- $exitCode
integer $exitCode the application exit code returned by the action method.
Returns
integer application exit code (return value is available since version 1.1.11)
|
protected
array
|
#
resolveRequest( array $args )
Parses the command line arguments and determines which action to perform.
Parses the command line arguments and determines which action to perform.
Parameters
- $args
array $args command line arguments
Returns
array the action name, named options (name=>value), and unnamed options
Since
1.1.5
|
public
string
|
#
getName( )
Returns
string the command name.
|
public
CConsoleCommandRunner
|
|
public
string
|
#
getHelp( )
Provides the command description. This method may be overridden to return the
actual command description.
Provides the command description. This method may be overridden to return the
actual command description.
Returns
string the command description. Defaults to 'Usage: php entry-script.php command-name'.
|
public
array
|
#
getOptionHelp( )
Provides the command option help information. The default implementation will
return all available actions together with their corresponding option
information.
Provides the command option help information. The default implementation will
return all available actions together with their corresponding option
information.
Returns
array the command option help information. Each array element describes the help
information for a single action.
Since
1.1.5
|
public
|
#
usageError( string $message )
Displays a usage error. This method will then terminate the execution of the
current application.
Displays a usage error. This method will then terminate the execution of the
current application.
Parameters
- $message
string $message the error message
|
public
|
#
copyFiles( array $fileList )
Copies a list of files from one place to another.
Copies a list of files from one place to another.
Parameters
- $fileList
array $fileList the list of files to be copied (name=>spec). The array keys are
names displayed during the copy process, and array values are specifications for
files to be copied. Each array value must be an array of the following
structure:
- source: required, the full path of the file/directory to be copied from
- target: required, the full path of the file/directory to be copied to
- callback: optional, the callback to be invoked when copying a file. The
callback function should be declared as follows: <pre> function
foo($source,$params) </pre> where $source parameter is the source file
path, and the content returned by the function will be saved into the target
file.
- params: optional, the parameters to be passed to the callback
See
|
public
array
|
#
buildFileList( string $sourceDir, string $targetDir, string $baseDir = '', array $ignoreFiles = array(), array $renameMap = array() )
Builds the file list of a directory. This method traverses through the
specified directory and builds a list of files and subdirectories that the
directory contains. The result of this function can be passed to CConsoleCommand::copyFiles() .
Builds the file list of a directory. This method traverses through the
specified directory and builds a list of files and subdirectories that the
directory contains. The result of this function can be passed to CConsoleCommand::copyFiles() .
Parameters
- $sourceDir
string $sourceDir the source directory
- $targetDir
string $targetDir the target directory
- $baseDir
string $baseDir base directory
- $ignoreFiles
array $ignoreFiles list of the names of files that should be ignored in list building
process. Argument available since 1.1.11.
- $renameMap
array $renameMap hash array of file names that should be renamed. Example value:
array('1.old.txt'=>'2.new.txt'). Argument available since 1.1.11.
Returns
|
public
|
#
ensureDirectory( string $directory )
Creates all parent directories if they do not exist.
Creates all parent directories if they do not exist.
Parameters
- $directory
string $directory the directory to be checked
|
public
mixed
|
#
renderFile( string $_viewFile_, array $_data_ = null, boolean $_return_ = false )
Renders a view file.
Parameters
- $_viewFile_
string $_viewFile_ view file path
- $_data_
array $_data_ optional data to be extracted as local view variables
- $_return_
boolean $_return_ whether to return the rendering result instead of displaying it
Returns
mixed the rendering result if required. Null otherwise.
|
public
string
|
#
pluralize( string $name )
Converts a word to its plural form.
Converts a word to its plural form.
Parameters
- $name
string $name the word to be pluralized
Returns
string the pluralized word
|
public
mixed
|
#
prompt( string $message, string $default = null )
Reads input via the readline PHP extension if that's available, or fgets() if
readline is not installed.
Reads input via the readline PHP extension if that's available, or fgets() if
readline is not installed.
Parameters
- $message
string $message to echo out before waiting for user input
- $default
string $default the default string to be returned when user does not write anything.
Defaults to null, means that default string is disabled. This parameter is
available since version 1.1.11.
Returns
mixed line read as a string, or false if input has been closed
Since
1.1.9
|
public
boolean
|
#
confirm( string $message, boolean $default = false )
Asks user to confirm by typing y or n.
Asks user to confirm by typing y or n.
Parameters
- $message
string $message to echo out before waiting for user input
- $default
boolean $default this value is returned if no selection is made. This parameter has been
available since version 1.1.11.
Returns
boolean whether user confirmed
Since
1.1.9
|
public
|
#
onBeforeAction( CConsoleCommandEvent $event )
This event is raised before an action is to be executed.
This event is raised before an action is to be executed.
Parameters
Since
1.1.11
|
public
|
#
onAfterAction( CConsoleCommandEvent $event )
This event is raised after an action finishes execution.
This event is raised after an action finishes execution.
Parameters
Since
1.1.11
|