0 follower

CConsoleApplication

Package system.console
Inheritance class CConsoleApplication » CApplication » CModule » CComponent
Since 1.0
Version $Id$
Source Code framework/console/CConsoleApplication.php
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 <command name> [param 1] [param 2] ...


You may use the following to see help instructions about a command:
php path/to/entry_script.php help <command name>

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
basePath string the root directory of the application. CApplication
behaviors array the behaviors that should be attached to the module. CModule
cache CCache the cache application component. CApplication
charset string the charset currently used for the application. CApplication
commandMap array mapping from command name to command configurations. CConsoleApplication
commandPath string the directory that contains the command classes. CConsoleApplication
commandRunner CConsoleCommandRunner Returns the command runner. CConsoleApplication
components array the currently loaded components (indexed by their IDs) CModule
coreMessages CPhpMessageSource the core message translations CApplication
dateFormatter CDateFormatter the locale-dependent date formatter. CApplication
db CDbConnection the database connection CApplication
errorHandler CErrorHandler the error handler application component. CApplication
extensionPath string Returns the root directory that holds all third-party extensions. CApplication
id string a unique identifier for the application. CApplication
language string the language that the user is using and the application should be targeted to. CApplication
locale CLocale the locale instance CApplication
messages CMessageSource the application message translations CApplication
modulePath string the directory that contains the application modules. CModule
modules array the configuration of the currently installed modules (module ID => configuration) CModule
name string the application name. CApplication
numberFormatter CNumberFormatter the locale-dependent number formatter. CApplication
params CAttributeCollection the list of user-defined parameters CModule
parentModule CModule the parent module. CModule
preload array the IDs of the application components that should be preloaded. CModule
request CHttpRequest the request component CApplication
runtimePath string the directory that stores runtime files. CApplication
securityManager CSecurityManager the security manager application component. CApplication
sourceLanguage string the language that the application is written in. CApplication
statePersister CStatePersister the state persister application component. CApplication
timeZone string Returns the time zone used by this application. CApplication
urlManager CUrlManager the URL manager component CApplication

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CApplication
__get() Getter magic method. CModule
__isset() Checks if a property value is null. CModule
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
clearGlobalState() Clears a global value. CApplication
configure() Configures the module with the specified configuration. CModule
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
displayError() Displays the captured PHP error. CConsoleApplication
displayException() Displays the uncaught PHP exception. CConsoleApplication
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
end() Terminates the application. CApplication
findLocalizedFile() Returns the localized version of a specified file. CApplication
getBasePath() Returns the root directory of the application. Defaults to 'protected'. CApplication
getCache() Returns the cache application component. Null if the component is not enabled. CApplication
getCommandPath() Returns the directory that contains the command classes. Defaults to 'protected/commands'. CConsoleApplication
getCommandRunner() Returns the command runner. CConsoleApplication
getComponent() Retrieves the named application component. CModule
getComponents() Returns the currently loaded components (indexed by their IDs) CModule
getCoreMessages() Returns the core message translations CApplication
getDateFormatter() Returns the locale-dependent date formatter. The current application locale will be used. CApplication
getDb() Returns the database connection CApplication
getErrorHandler() Returns the error handler application component. CApplication
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getExtensionPath() Returns the root directory that holds all third-party extensions. CApplication
getGlobalState() Returns a global value. CApplication
getId() Returns a unique identifier for the application. CApplication
getLanguage() Returns the language that the user is using and the application should be targeted to. Defaults to the source language. CApplication
getLocale() Returns the locale instance CApplication
getMessages() Returns the application message translations CApplication
getModule() Retrieves the named application module. CModule
getModulePath() Returns the directory that contains the application modules. Defaults to the 'modules' subdirectory of basePath. CModule
getModules() Returns the configuration of the currently installed modules (module ID => configuration) CModule
getNumberFormatter() Returns the locale-dependent number formatter. The current application locale will be used. CApplication
getParams() Returns the list of user-defined parameters CModule
getParentModule() Returns the parent module. Null if this module does not have a parent. CModule
getRequest() Returns the request component CApplication
getRuntimePath() Returns the directory that stores runtime files. Defaults to 'protected/runtime'. CApplication
getSecurityManager() Returns the security manager application component. CApplication
getStatePersister() Returns the state persister application component. CApplication
getTimeZone() Returns the time zone used by this application. CApplication
getUrlManager() Returns the URL manager component CApplication
handleError() Handles PHP execution errors such as warnings, notices. CApplication
handleException() Handles uncaught PHP exceptions. CApplication
hasComponent() Determines whether the named application component exists (including both loaded and disabled.) CModule
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
onBeginRequest() Raised right BEFORE the application processes the request. CApplication
onEndRequest() Raised right AFTER the application processes the request. CApplication
onError() Raised when a PHP execution error occurs. CApplication
onException() Raised when an uncaught PHP exception occurs. CApplication
processRequest() Processes the user request. CConsoleApplication
raiseEvent() Raises an event. CComponent
run() Runs the application. CApplication
setAliases() Defines the root aliases. CModule
setBasePath() Sets the root directory of the application. CApplication
setCommandPath() Sets the directory that contains the command classes. CConsoleApplication
setComponent() Puts a component under the management of the module. CModule
setComponents() Sets the application components. CModule
setExtensionPath() Sets the directory that contains all third-party extensions. CApplication
setGlobalState() Sets a global value. CApplication
setId() Sets a unique identifier for the application. CApplication
setImport() Sets the aliases that are used in the module. CModule
setLanguage() Specifies which language the application is targeted to. CApplication
setModulePath() Sets the directory that contains the application modules. CModule
setModules() Configures the sub-modules of this module. CModule
setParams() Sets user-defined parameters. This should be in name-value pairs. CModule
setRuntimePath() Sets the directory that stores runtime files. CApplication
setTimeZone() Sets the time zone used by this application. CApplication

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
createCommandRunner() Creates the command runner instance. CConsoleApplication
init() Initializes the application by creating the command runner. CConsoleApplication
initSystemHandlers() Initializes the class autoloader and error handlers. CApplication
loadGlobalState() Loads the global state data from persistent storage. CApplication
preinit() Preinitializes the module. CModule
preloadComponents() Loads static application components. CModule
registerCoreComponents() Registers the core application components. CApplication
saveGlobalState() Saves the global state data into persistent storage. CApplication

Events

Hide inherited events

EventDescriptionDefined By
onBeginRequest Raised right BEFORE the application processes the request. CApplication
onEndRequest Raised right AFTER the application processes the request. CApplication
onException Raised when an uncaught PHP exception occurs. CApplication
onError Raised when a PHP execution error occurs. CApplication

Property Details

commandMap property
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 property
public string getCommandPath()
public void setCommandPath(string $value)

the directory that contains the command classes. Defaults to 'protected/commands'.

commandRunner property read-only

Returns the command runner.

Method Details

createCommandRunner() method
protected CConsoleCommandRunner createCommandRunner()
{return} CConsoleCommandRunner the command runner
Source Code: framework/console/CConsoleApplication.php#95 (show)
protected function createCommandRunner()
{
    return new 
CConsoleCommandRunner;
}

Creates the command runner instance.

displayError() method
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
Source Code: framework/console/CConsoleApplication.php#109 (show)
public function displayError($code,$message,$file,$line)
{
    echo 
"PHP Error[$code]: $message\n";
    echo 
"in file $file at line $line\n";
    
debug_print_backtrace();
}

Displays the captured PHP error. This method displays the error in console mode when there is no active error handler.

displayException() method
public void displayException(Exception $exception)
$exception Exception the uncaught exception
Source Code: framework/console/CConsoleApplication.php#122 (show)
public function displayException($exception)
{
    echo 
$exception;
}

Displays the uncaught PHP exception. This method displays the exception in console mode when there is no active error handler.

getCommandPath() method
public string getCommandPath()
{return} string the directory that contains the command classes. Defaults to 'protected/commands'.
Source Code: framework/console/CConsoleApplication.php#130 (show)
public function getCommandPath()
{
    if(
$this->_commandPath===null)
        
$this->setCommandPath($this->getBasePath().DIRECTORY_SEPARATOR.'commands');
    return 
$this->_commandPath;
}

getCommandRunner() method
public CConsoleCommandRunner getCommandRunner()
{return} CConsoleCommandRunner the command runner.
Source Code: framework/console/CConsoleApplication.php#152 (show)
public function getCommandRunner()
{
    return 
$this->_runner;
}

Returns the command runner.

init() method
protected void init()
Source Code: framework/console/CConsoleApplication.php#72 (show)
protected function init()
{
    
parent::init();
    if(!isset(
$_SERVER['argv'])) // || strncasecmp(php_sapi_name(),'cli',3))
        
die('This script must be run from the command line.');
    
$this->_runner=$this->createCommandRunner();
    
$this->_runner->commands=$this->commandMap;
    
$this->_runner->addCommands($this->getCommandPath());
}

Initializes the application by creating the command runner.

processRequest() method
public void processRequest()
Source Code: framework/console/CConsoleApplication.php#86 (show)
public function processRequest()
{
    
$this->_runner->run($_SERVER['argv']);
}

Processes the user request. This method creates a console command runner to handle the particular user command.

setCommandPath() method
public void setCommandPath(string $value)
$value string the directory that contains the command classes.
Source Code: framework/console/CConsoleApplication.php#141 (show)
public function setCommandPath($value)
{
    if((
$this->_commandPath=realpath($value))===false || !is_dir($this->_commandPath))
        throw new 
CException(Yii::t('yii','The command path "{path}" is not a valid directory.',
            array(
'{path}'=>$value)));
}