CApplication
| Package | system.base |
|---|---|
| Inheritance | abstract class CApplication » CComponent |
| Subclasses | CConsoleApplication, CWebApplication |
| Since | 1.0 |
| Version | $Id: CApplication.php 739 2009-02-24 16:54:19Z qiang.xue $ |
An application serves as the global context that the user request is being processed. It manages a set of application components that provide specific functionalities to the whole application.
The core application components provided by CApplication are the following:
- errorHandler: handles PHP errors and uncaught exceptions. This application component is dynamically loaded when needed.
- securityManager: provides security-related services, such as hashing, encryption. This application component is dynamically loaded when needed.
- statePersister: provides global state persistence method. This application component is dynamically loaded when needed.
- cache: provides caching feature. This application component is disabled by default.
- messages: provides the message source for translating application messages. This application component is dynamically loaded when needed.
- coreMessages: provides the message source for translating Yii framework messages. This application component is dynamically loaded when needed.
CApplication will undergo the following lifecycles when processing a user request:
- load application configuration;
- set up class autoloader and error handling;
- load static application components;
- onBeginRequest: preprocess the user request;
- processRequest: process the user request;
- onEndRequest: postprocess the user request;
Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, the application will switch to its error handling logic and jump to step 6 afterwards.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| basePath | string | the root directory of the application. | CApplication |
| behaviors | array | the behaviors that should be attached to the application. | CApplication |
| cache | CCache | the cache application component. | CApplication |
| charset | string | the charset currently used for the application. | CApplication |
| components | array | the currently loaded components (indexed by their IDs) | CApplication |
| 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 |
| name | string | the application name. | CApplication |
| numberFormatter | CNumberFormatter | the locale-dependent number formatter. | CApplication |
| params | CAttributeCollection | the list of application parameters | CApplication |
| preload | array | the IDs of the application components that should be preloaded. | 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 |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __call() | Calls the named method which is not a class method. | CComponent |
| __construct() | Constructor. | CApplication |
| __get() | Getter magic method. | CApplication |
| __isset() | Checks if a property value is null. | CApplication |
| __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 application with the specified configuration. | CApplication |
| 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. | CApplication |
| displayException() | Displays the uncaught PHP exception. | CApplication |
| 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 | |
| getComponent() | Retrieves the named application component. | CApplication |
| getComponents() | CApplication | |
| 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 | |
| getNumberFormatter() | CApplication | |
| getParams() | CApplication | |
| getRuntimePath() | CApplication | |
| getSecurityManager() | CApplication | |
| getStatePersister() | CApplication | |
| handleError() | Handles PHP execution errors such as warnings, notices. | CApplication |
| handleException() | Handles uncaught PHP exceptions. | CApplication |
| hasComponent() | CApplication | |
| 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 request. | CApplication |
| raiseEvent() | Raises an event. | CComponent |
| run() | Runs the application. | CApplication |
| setBasePath() | Sets the root directory of the application. | CApplication |
| setComponent() | Puts a component under the management of the application. | CApplication |
| setComponents() | Sets the application components. | CApplication |
| setGlobalState() | Sets a global value. | CApplication |
| setId() | CApplication | |
| setImport() | Sets the aliases that are used in the application. | CApplication |
| setLanguage() | Specifies which language the application is targeted to. | CApplication |
| setParams() | CApplication | |
| setRuntimePath() | CApplication |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| init() | Initializes the application. | CApplication |
| initSystemHandlers() | Initializes the class autoloader and error handlers. | CApplication |
| loadGlobalState() | Loads the global state data from persistent storage. | CApplication |
| preloadComponents() | Loads static application components. | CApplication |
| registerCoreComponents() | Registers the core application components. | CApplication |
| saveGlobalState() | Saves the global state data into persistent storage. | CApplication |
Events
| Event | Description | Defined 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
the root directory of the application. Defaults to 'protected'.
the behaviors that should be attached to the application. The behaviors will be attached to the application when init is called. Please refer to CModel::behaviors on how to specify the value of this property.
the cache application component. Null if the component is not enabled.
the charset currently used for the application. Defaults to 'UTF-8'.
the currently loaded components (indexed by their IDs)
the core message translations
the locale-dependent date formatter. The current application locale will be used.
the database connection
the error handler application component.
Returns the root directory that holds all third-party extensions. Note, this property cannot be changed or overridden. It is always 'AppBasePath/extensions'.
a unique identifier for the application.
the language that the user is using and the application should be targeted to. Defaults to the source language.
the locale instance
the application message translations
the application name. Defaults to 'My Application'.
the locale-dependent number formatter. The current application locale will be used.
the list of application parameters
the IDs of the application components that should be preloaded.
the directory that stores runtime files. Defaults to 'protected/runtime'.
the security manager application component.
the language that the application is written in. This mainly refers to the language that the messages and view files are in. Defaults to 'en_us' (US English).
the state persister application component.
Method Details
|
public void __construct(mixed $config=NULL)
| ||
| $config | mixed | application configuration. If a string, it is treated as the path of the file that contains the configuration; If an array or CConfiguration, it is the actual configuration information. Please make sure you specify the basePath property in the configuration, which should point to the directory containing all application logic, template and data. If not, the directory will be defaulted to 'protected'. |
Constructor.
|
public mixed __get(string $name)
| ||
| $name | string | application component or property name |
| {return} | mixed | the named property value |
Getter magic method. This method is overridden to support accessing application components like reading application properties.
|
public boolean __isset(string $name)
| ||
| $name | string | the property name or the event name |
| {return} | boolean | whether the property value is null |
Checks if a property value is null. This method overrides the parent implementation by checking if the named application component is loaded.
|
public void clearGlobalState(string $key)
| ||
| $key | string | the name of the value to be cleared |
Clears a global value.
The value cleared will no longer be available in this request and the following requests.
|
public void configure(mixed $config)
| ||
| $config | mixed | application configuration. If a string, it is treated as the path of the file that contains the configuration; If an array, it is the actual configuration information. Please make sure you specify the basePath property in the configuration, which should point to the root directory containing all application logic, template and data. |
Configures the application with the specified configuration.
|
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 HTML 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 HTML when there is no active error handler.
|
public void end(integer $status=0)
| ||
| $status | integer | exit status (value 0 means normal exit while other values mean abnormal exit). |
Terminates the application. This method replaces PHP's exit() function by calling onEndRequest before exiting.
|
public string findLocalizedFile(string $srcFile, string $srcLanguage=NULL, string $language=NULL)
| ||
| $srcFile | string | the original file |
| $srcLanguage | string | the language that the original file is in. If null, the application source language is used. |
| $language | string | the desired language that the file should be localized to. If null, the application language will be used. |
| {return} | string | the matching localized file. The original file is returned if no localized version is found or if source language is the same as the desired language. |
Returns the localized version of a specified file.
The searching is based on the specified language code. In particular,
a file with the same name will be looked for under the subdirectory
named as the locale ID. For example, given the file "path/to/view.php"
and locale ID "zh_cn", the localized file will be looked for as
"path/to/zh_cn/view.php". If the file is not found, the original file
will be returned.
For consistency, it is recommended that the locale ID is given
in lower case and in the format of LanguageID_RegionID (e.g. "en_us").
|
public string getBasePath()
| ||
| {return} | string | the root directory of the application. Defaults to 'protected'. |
|
public CCache getCache()
| ||
| {return} | CCache | the cache application component. Null if the component is not enabled. |
|
public IApplicationComponent getComponent(string $id)
| ||
| $id | string | application component ID (case-sensitive) |
| {return} | IApplicationComponent | the application component instance, null if the application component is disabled or does not exist. |
Retrieves the named application component.
See Also
|
public array getComponents()
| ||
| {return} | array | the currently loaded components (indexed by their IDs) |
|
public CPhpMessageSource getCoreMessages()
| ||
| {return} | CPhpMessageSource | the core message translations |
|
public CDateFormatter getDateFormatter()
| ||
| {return} | CDateFormatter | the locale-dependent date formatter. The current application locale will be used. |
|
public CDbConnection getDb()
| ||
| {return} | CDbConnection | the database connection |
|
public CErrorHandler getErrorHandler()
| ||
| {return} | CErrorHandler | the error handler application component. |
|
final public string getExtensionPath()
| ||
| {return} | string | the directory that contains all extensions. |
Returns the root directory that holds all third-party extensions. Note, this property cannot be changed or overridden. It is always 'AppBasePath/extensions'.
|
public mixed getGlobalState(string $key, mixed $defaultValue=NULL)
| ||
| $key | string | the name of the value to be returned |
| $defaultValue | mixed | the default value. If the named global value is not found, this will be returned instead. |
| {return} | mixed | the named global value |
Returns a global value.
A global value is one that is persistent across users sessions and requests.
See Also
|
public string getId()
| ||
| {return} | string | a unique identifier for the application. |
|
public string getLanguage()
| ||
| {return} | string | the language that the user is using and the application should be targeted to. Defaults to the source language. |
|
public CLocale getLocale(string $localeID=NULL)
| ||
| $localeID | string | locale ID (e.g. en_US). If null, the application language ID will be used. |
| {return} | CLocale | the locale instance |
|
public CMessageSource getMessages()
| ||
| {return} | CMessageSource | the application message translations |
|
public CNumberFormatter getNumberFormatter()
| ||
| {return} | CNumberFormatter | the locale-dependent number formatter. The current application locale will be used. |
|
public CAttributeCollection getParams()
| ||
| {return} | CAttributeCollection | the list of application parameters |
|
public string getRuntimePath()
| ||
| {return} | string | the directory that stores runtime files. Defaults to 'protected/runtime'. |
|
public CSecurityManager getSecurityManager()
| ||
| {return} | CSecurityManager | the security manager application component. |
|
public CStatePersister getStatePersister()
| ||
| {return} | CStatePersister | the state persister application component. |
|
public void handleError(integer $code, string $message, string $file, integer $line)
| ||
| $code | integer | the level of the error raised |
| $message | string | the error message |
| $file | string | the filename that the error was raised in |
| $line | integer | the line number the error was raised at |
Handles PHP execution errors such as warnings, notices.
This method is implemented as a PHP error handler. It requires
that constant YII_ENABLE_ERROR_HANDLER be defined true.
This method will first raise an onError event.
If the error is not handled by any event handler, it will call
errorHandler to process the error.
The application will be terminated by this method.
|
public void handleException(Exception $exception)
| ||
| $exception | Exception | exception that is not caught |
Handles uncaught PHP exceptions.
This method is implemented as a PHP exception handler. It requires
that constant YII_ENABLE_EXCEPTION_HANDLER be defined true.
This method will first raise an onException event.
If the exception is not handled by any event handler, it will call
errorHandler to process the exception.
The application will be terminated by this method.
|
public boolean hasComponent(string $id)
| ||
| $id | string | application component ID |
| {return} | boolean | whether the named application component exists (including both loaded and disabled.) |
|
protected void init()
|
Initializes the application. This method is invoked right after the application is configured. The default implementation will load static components. If you override this method, make sure the parent implementation is called.
|
protected void initSystemHandlers()
|
Initializes the class autoloader and error handlers.
|
protected void loadGlobalState()
|
Loads the global state data from persistent storage.
See Also
|
public void onBeginRequest(CEvent $event)
| ||
| $event | CEvent | the event parameter |
Raised right BEFORE the application processes the request.
|
public void onEndRequest(CEvent $event)
| ||
| $event | CEvent | the event parameter |
Raised right AFTER the application processes the request.
|
public void onError(CErrorEvent $event)
| ||
| $event | CErrorEvent | event parameter |
Raised when a PHP execution error occurs.
An event handler can set the handled
property of the event parameter to be true to indicate no further error
handling is needed. Otherwise, the errorHandler
application component will continue processing the error.
|
public void onException(CExceptionEvent $event)
| ||
| $event | CExceptionEvent | event parameter |
Raised when an uncaught PHP exception occurs.
An event handler can set the handled
property of the event parameter to be true to indicate no further error
handling is needed. Otherwise, the errorHandler
application component will continue processing the error.
|
protected void preloadComponents()
|
Loads static application components.
|
abstract public void processRequest()
|
Processes the request. This is the place where the actual request processing work is done. Derived classes should override this method.
|
protected void registerCoreComponents()
|
Registers the core application components.
See Also
|
public void run()
|
Runs the application. This method loads static application components. Derived classes usually overrides this method to do more application-specific tasks. Remember to call the parent implementation so that static application components are loaded.
|
protected void saveGlobalState()
|
Saves the global state data into persistent storage.
See Also
|
public void setBasePath(string $path)
| ||
| $path | string | the root directory of the application. |
Sets the root directory of the application. This method can only be invoked at the begin of the constructor.
|
public void setComponent(string $id, IApplicationComponent $component)
| ||
| $id | string | component ID |
| $component | IApplicationComponent | the component |
Puts a component under the management of the application. The component will be initialized (by calling its init() method if it has not done so.
|
public void setComponents(array $components)
| ||
| $components | array | application components(id=>component configuration or instances) |
Sets the application components.
When a configuration is used to specify a component, it should consist of
the component's initial property values (name-value pairs). Additionally,
a component can be enabled (default) or disabled by specifying the 'enabled' value
in the configuration.
If a configuration is specified with an ID that is the same as an existing
component or configuration, the existing one will be replaced silently.
The following is the configuration for two components:
array(
'db'=>array(
'class'=>'CDbConnection',
'connectionString'=>'sqlite:path/to/file.db',
),
'cache'=>array(
'class'=>'CDbCache',
'connectionID'=>'db',
'enabled'=>!YII_DEBUG, // enable caching in non-debug mode
),
)
|
public void setGlobalState(string $key, mixed $value, mixed $defaultValue=NULL)
| ||
| $key | string | the name of the value to be saved |
| $value | mixed | the global value to be saved. It must be serializable. |
| $defaultValue | mixed | the default value. If the named global value is the same as this value, it will be cleared from the current storage. |
Sets a global value.
A global value is one that is persistent across users sessions and requests.
Make sure that the value is serializable and unserializable.
See Also
|
public void setId(string $id)
| ||
| $id | string | a unique identifier for the application. |
|
public void setImport(array $aliases)
| ||
| $aliases | array | list of aliases to be imported |
Sets the aliases that are used in the application.
|
public void setLanguage(string $language)
| ||
| $language | string | the user language (e.g. 'en_US', 'zh_CN'). If it is null, the sourceLanguage will be used. |
Specifies which language the application is targeted to.
This is the language that the application displays to end users.
If set null, it uses the source language.
Unless your application needs to support multiple languages, you should always
set this language to null to maximize the application's performance.
|
public void setParams(mixed $value)
| ||
| $value | mixed | application parameters. This can be either an array or CAttributeCollection object. |
|
public void setRuntimePath(string $path)
| ||
| $path | string | the directory that stores runtime files. |