CConsoleApplication
CConsoleApplication represents a console application.
CConsoleApplication extends
CApplication by providing functionalities
specific to console requests. In particular, it deals with console requests
through a command-based approach:
- A console application consists of one or several possible user commands;
- Each user command is implemented as a class extending CConsoleCommand;
- User specifies which command to run on the command line;
- The command processes the user request with the specified parameters.
The command classes reside in the directory
commandPath.
The name of the class follows the pattern: <command-name>Command, and its
file name is the same the class name. For example, the 'ShellCommand' class defines
a 'shell' command and the class file name is 'ShellCommand.php'.
To run the console application, enter the following on the command line:
php path/to/entry_script.php [param 1] [param 2] ...
You may use the following to see help instructions about a command:
php path/to/entry_script.php help
Property Details
commandMap
public array $commandMap;
mapping from command name to command configurations.
Each command configuration can be either a string or an array.
If the former, the string should be the file path of the command class.
If the latter, the array must contain a 'class' element which specifies
the command's class name or class path alias.
The rest name-value pairs in the array are used to initialize
the corresponding command properties. For example,
array(
'email'=>array(
'class'=>'path.to.Mailer',
'interval'=>3600,
),
'log'=>'path/to/LoggerCommand.php',
)
commandPath
the directory that contains the command classes. Defaults to 'protected/commands'.
commandRunner
Returns the command runner.
Method Details
createCommandRunner()
Creates the command runner instance.
|
public void displayError(integer $code, string $message, string $file, string $line)
|
| $code |
integer |
error code |
| $message |
string |
error message |
| $file |
string |
error file |
| $line |
string |
error line |
Displays the captured PHP error.
This method displays the error in console mode when there is
no active error handler.
|
public void displayException(Exception $exception)
|
| $exception |
Exception |
the uncaught exception |
Displays the uncaught PHP exception.
This method displays the exception in console mode when there is
no active error handler.
getCommandPath()
|
public string getCommandPath()
|
| {return} |
string |
the directory that contains the command classes. Defaults to 'protected/commands'. |
getCommandRunner()
Returns the command runner.
Initializes the application by creating the command runner.
|
public void processRequest()
|
Processes the user request.
This method creates a console command runner to handle the particular user command.
setCommandPath()
|
public void setCommandPath(string $value)
|
| $value |
string |
the directory that contains the command classes. |
If you need to modify the commandPath, put something like the following into protected/config/main.php or console.php:
This would add all commands from the protected/commands/shell directory. The value needs to be something that the PHP realpath() function will accept and must be a directory that exists.