Versions
Look up a class, method, property or event

CConsoleApplication

Package system.console
Inheritance class CConsoleApplication » CApplication » CModule » CComponent
Since 1.0
Version $Id: CConsoleApplication.php 1361 2009-08-23 00:29:19Z qiang.xue $
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 

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() CApplication
getCache() CApplication
getCommandPath() CConsoleApplication
getCommandRunner() Returns the command runner. CConsoleApplication
getComponent() Retrieves the named application component. CModule
getComponents() CModule
getCoreMessages() CApplication
getDateFormatter() CApplication
getDb() CApplication
getErrorHandler() 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() CApplication
getLanguage() CApplication
getLocale() CApplication
getMessages() CApplication
getModule() Retrieves the named application module. CModule
getModulePath() CModule
getModules() CModule
getNumberFormatter() CApplication
getParams() CModule
getParentModule() CModule
getRequest() CApplication
getRuntimePath() CApplication
getSecurityManager() CApplication
getStatePersister() CApplication
getTimeZone() Returns the time zone used by this application. CApplication
getUrlManager() CApplication
handleError() Handles PHP execution errors such as warnings, notices. CApplication
handleException() Handles uncaught PHP exceptions. CApplication
hasComponent() 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() CConsoleApplication
setComponent() Puts a component under the management of the module. CModule
setComponents() Sets the application components. CModule
setExtensionPath() CApplication
setGlobalState() Sets a global value. CApplication
setId() CApplication
setImport() Sets the aliases that are used in the module. CModule
setLanguage() Specifies which language the application is targeted to. CApplication
setModulePath() CModule
setModules() Configures the sub-modules of this module. CModule
setParams() CModule
setRuntimePath() 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

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

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

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'.

getCommandRunner() method
public CConsoleCommandRunner getCommandRunner()
{return} CConsoleCommandRunner the command runner.

Returns the command runner.

init() method
protected void init()

Initializes the application by creating the command runner.

processRequest() method
public void processRequest()

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.

Be the first person to leave a comment

Please to leave your comment.