Class TApplication

Description

TApplication class.

TApplication coordinates modules and services, and serves as a configuration context for all Prado components.

TApplication uses a configuration file to specify the settings of the application, the modules, the services, the parameters, and so on.

TApplication adopts a modular structure. A TApplication instance is a composition of multiple modules. A module is an instance of class implementing IModule interface. Each module accomplishes certain functionalities that are shared by all Prado components in an application. There are default modules and user-defined modules. The latter offers extreme flexibility of extending TApplication in a plug-and-play fashion. Modules cooperate with each other to serve a user request by following a sequence of lifecycles predefined in TApplication.

TApplication has four modes that can be changed by setting Mode property (in the application configuration file).

  • Off mode will prevent the application from serving user requests.
  • Debug mode is mainly used during application development. It ensures the cache is always up-to-date if caching is enabled. It also allows exceptions are displayed with rich context information if they occur.
  • Normal mode is mainly used during production stage. Exception information will only be recorded in system error logs. The cache is ensured to be up-to-date if it is enabled.
  • Performance mode is similar to Normal mode except that it does not ensure the cache is up-to-date.
TApplication dispatches each user request to a particular service which finishes the actual work for the request with the aid from the application modules.

TApplication maintains a lifecycle with the following stages:

  • [construct] : construction of the application instance
  • [initApplication] : load application configuration and instantiate modules and the requested service
  • onBeginRequest : this event happens right after application initialization
  • onAuthentication : this event happens when authentication is needed for the current request
  • onAuthenticationComplete : this event happens right after the authentication is done for the current request
  • onAuthorization : this event happens when authorization is needed for the current request
  • onAuthorizationComplete : this event happens right after the authorization is done for the current request
  • onLoadState : this event happens when application state needs to be loaded
  • onLoadStateComplete : this event happens right after the application state is loaded
  • onPreRunService : this event happens right before the requested service is to run
  • runService : the requested service runs
  • onSaveState : this event happens when application needs to save its state
  • onSaveStateComplete : this event happens right after the application saves its state
  • onPreFlushOutput : this event happens right before the application flushes output to client side.
  • flushOutput : the application flushes output to client side.
  • onEndRequest : this is the last stage a request is being completed
  • [destruct] : destruction of the application instance
Modules and services can attach their methods to one or several of the above events and do appropriate processing when the events are raised. By this way, the application is able to coordinate the activities of modules and services in the above order. To terminate an application before the whole lifecycle completes, call completeRequest.

Examples:

  • Create and run a Prado application:
    1. $application=new TApplication($configFile);
    2. $application->run();

  • since: 3.0
  • version: $Id: TApplication.php 1560 2006-12-04 03:15:22Z xue $
  • author: Qiang Xue <qiang.xue@gmail.com>

Located in /TApplication.php (line 113)

TComponent
   |
   --TApplication
Class Constant Summary
 CONFIGCACHE_FILE = 'config.cache'
 CONFIG_FILE = 'application.xml'
 GLOBAL_FILE = 'global.cache'
 PAGE_SERVICE_ID = 'page'
 RUNTIME_PATH = 'runtime'
 STATE_DEBUG = 'Debug'
 STATE_NORMAL = 'Normal'
 STATE_OFF = 'Off'
 STATE_PERFORMANCE = 'Performance'
Method Summary
TApplication __construct ([string $basePath = 'protected'], [boolean $cacheConfig = true])
void clearGlobalState (string $key)
void completeRequest ()
void flushOutput ()
string getBasePath ()
TGlobalization getGlobalization ([boolean $createIfNotExists = true])
mixed getGlobalState (string $key, [mixed $defaultValue = null])
string getID ()
IModule getModule (mixed $id)
array getModules ()
boolean getRequestCompleted ()
string getRuntimePath ()
string getUniqueID ()
void initApplication (string 0, string 1)
void loadGlobals ()
void onAuthorization ()
void onBeginRequest ()
void onEndRequest ()
void onError (mixed $param)
void onLoadState ()
void onPreRunService ()
void onSaveState ()
void run ()
void runService ()
void saveGlobals ()
void setApplicationStatePersister (IStatePersister $persister)
void setAssetManager (TAssetManager $value)
void setCache (ICache $cache)
void setErrorHandler (TErrorHandler $handler)
void setGlobalization (TGlobalization $glob)
void setGlobalState (string $key, mixed $value, [mixed $defaultValue = null])
void setID (string $value)
void setMode (TApplicationMode $value)
void setModule (string $id, IModule $module)
void setPageService (TPageService $service)
void setRequest (THttpRequest $request)
void setResponse (THttpResponse $response)
void setSecurityManager (TSecurityManager $sm)
void setSession (THttpSession $session)
void setUser (IUser $user)
Methods
Constructor __construct (line 287)

Constructor.

Sets application base path and initializes the application singleton. Application base path refers to the root directory storing application data and code not directly accessible by Web users. By default, the base path is assumed to be the protected directory under the directory containing the current running script.

  • access: public
  • throws: TConfigurationException if configuration file cannot be read or the runtime path is invalid.
TApplication __construct ([string $basePath = 'protected'], [boolean $cacheConfig = true])
  • string $basePath: application base path or configuration file path. If the parameter is a file, it is assumed to be the application configuration file, and the directory containing the file is treated as the application base path. If it is a directory, it is assumed to be the application base path, and within that directory, a file named application.xml will be looked for. If found, the file is considered as the application configuration file.
  • boolean $cacheConfig: whether to cache application configuration. Defaults to true.
clearGlobalState (line 417)

Clears a global value.

The value cleared will no longer be available in this request and the following requests.

  • access: public
void clearGlobalState (string $key)
  • string $key: the name of the value to be cleared
completeRequest (line 367)

Completes current request processing.

This method can be used to exit the application lifecycles after finishing the current cycle.

  • access: public
void completeRequest ()
flushOutput (line 1033)

Flushes output to client side.

  • access: public
void flushOutput ()
getApplicationStatePersister (line 713)
  • return: application state persister
  • access: public
IStatePersister getApplicationStatePersister ()
getAssetManager (line 692)
  • return: asset manager
  • access: public
TAssetManager getAssetManager ()
getAuthorizationRules (line 785)
  • return: list of authorization rules for the current request
  • access: public
TAuthorizationRuleCollection getAuthorizationRules ()
getBasePath (line 490)
  • return: configuration path
  • access: public
string getBasePath ()
getCache (line 734)
  • return: the cache module, null if cache module is not installed
  • access: public
ICache getCache ()
getConfigurationFile (line 498)
  • return: configuration file path
  • access: public
string getConfigurationFile ()
getErrorHandler (line 650)
  • return: the error hanlder module
  • access: public
TErrorHandler getErrorHandler ()
getGlobalization (line 767)
  • return: globalization module
  • access: public
TGlobalization getGlobalization ([boolean $createIfNotExists = true])
  • boolean $createIfNotExists: whether to create globalization if it does not exist
getGlobalState (line 388)

Returns a global value.

A global value is one that is persistent across users sessions and requests.

  • return: the global value corresponding to $key
  • access: public
mixed getGlobalState (string $key, [mixed $defaultValue = null])
  • string $key: the name of the value to be returned
  • mixed $defaultValue: the default value. If $key is not found, $defaultValue will be returned
getID (line 450)
  • return: application ID
  • access: public
string getID ()
getMode (line 474)
  • return: application mode. Defaults to TApplicationMode::Debug.
  • access: public
TApplicationMode getMode ()
getModule (line 537)
  • return: the module with the specified ID, null if not found
  • access: public
IModule getModule (mixed $id)
getModules (line 545)
  • return: list of loaded application modules, indexed by module IDs
  • access: public
array getModules ()
getPageService (line 564)
  • return: page service
  • access: public
TPageService getPageService ()
getParameters (line 556)

Returns the list of application parameters.

Since the parameters are returned as a TMap object, you may use the returned result to access, add or remove individual parameters.

  • return: the list of application parameters
  • access: public
TMap getParameters ()
getRequest (line 587)
  • return: the request module
  • access: public
THttpRequest getRequest ()
getRequestCompleted (line 375)
  • return: whether the current request is processed.
  • access: public
boolean getRequestCompleted ()
getResponse (line 608)
  • return: the response module
  • access: public
THttpResponse getResponse ()
getRuntimePath (line 507)

Gets the directory storing application-level persistent data.

  • return: application state path
  • access: public
string getRuntimePath ()
getSecurityManager (line 671)
  • return: the security manager module
  • access: public
TSecurityManager getSecurityManager ()
getService (line 515)
  • return: the currently requested service
  • access: public
IService getService ()
getSession (line 629)
  • return: the session module, null if session module is not installed
  • access: public
THttpSession getSession ()
getUniqueID (line 466)
  • return: an ID that uniquely identifies this Prado application from the others
  • access: public
string getUniqueID ()
getUser (line 750)
  • return: the application user
  • access: public
IUser getUser ()
initApplication (line 801)

Loads configuration and initializes application.

Configuration file will be read and parsed (if a valid cached version exists, it will be used instead). Then, modules are created and initialized; Afterwards, the requested service is created and initialized.

  • access: protected
  • throws: TConfigurationException if module is redefined of invalid type, or service not defined or of invalid type
void initApplication (string 0, string 1)
  • string 0: configuration file path (absolute or relative to current executing script)
  • string 1: cache file path, empty if no present or needed
loadGlobals (line 429)

Loads global values from persistent storage.

This method is invoked when OnLoadState event is raised. After this method, values that are stored in previous requests become available to the current request via getGlobalState.

  • access: protected
void loadGlobals ()
onAuthentication (line 933)

Raises OnAuthentication event.

This method is invoked when the user request needs to be authenticated.

  • access: public
void onAuthentication ()
onAuthenticationComplete (line 942)

Raises OnAuthenticationComplete event.

This method is invoked right after the user request is authenticated.

  • access: public
void onAuthenticationComplete ()
onAuthorization (line 951)

Raises OnAuthorization event.

This method is invoked when the user request needs to be authorized.

  • access: public
void onAuthorization ()
onAuthorizationComplete (line 960)

Raises OnAuthorizationComplete event.

This method is invoked right after the user request is authorized.

  • access: public
void onAuthorizationComplete ()
onBeginRequest (line 924)

Raises OnBeginRequest event.

At the time when this method is invoked, application modules are loaded and initialized, user request is resolved and the corresponding service is loaded and initialized. The application is about to start processing the user request.

  • access: public
void onBeginRequest ()
onEndRequest (line 1042)

Raises OnEndRequest event.

This method is invoked when the application completes the processing of the request.

  • access: public
void onEndRequest ()
onError (line 910)

Raises OnError event.

This method is invoked when an exception is raised during the lifecycles of the application.

  • access: public
void onError (mixed $param)
  • mixed $param: event parameter
onLoadState (line 969)

Raises OnLoadState event.

This method is invoked when the application needs to load state (probably stored in session).

  • access: public
void onLoadState ()
onLoadStateComplete (line 979)

Raises OnLoadStateComplete event.

This method is invoked right after the application state has been loaded.

  • access: public
void onLoadStateComplete ()
onPreFlushOutput (line 1025)

Raises OnPreFlushOutput event.

This method is invoked right before the application flushes output to client.

  • access: public
void onPreFlushOutput ()
onPreRunService (line 988)

Raises OnPreRunService event.

This method is invoked right before the service is to be run.

  • access: public
void onPreRunService ()
onSaveState (line 1006)

Raises OnSaveState event.

This method is invoked when the application needs to save state (probably stored in session).

  • access: public
void onSaveState ()
onSaveStateComplete (line 1016)

Raises OnSaveStateComplete event.

This method is invoked right after the application state has been saved.

  • access: public
void onSaveStateComplete ()
run (line 335)

Executes the lifecycles of the application.

This is the main entry function that leads to the running of the whole Prado application.

  • access: public
void run ()
runService (line 996)

Runs the requested service.

  • access: public
void runService ()
saveGlobals (line 438)

Saves global values into persistent storage.

This method is invoked when OnSaveState event is raised.

  • access: protected
void saveGlobals ()
setApplicationStatePersister (line 726)
  • access: public
void setApplicationStatePersister (IStatePersister $persister)
setAssetManager (line 705)
  • access: public
void setAssetManager (TAssetManager $value)
setCache (line 742)
  • access: public
void setCache (ICache $cache)
  • ICache $cache: the cache module
setErrorHandler (line 663)
  • access: public
void setErrorHandler (TErrorHandler $handler)
setGlobalization (line 777)
  • access: public
void setGlobalization (TGlobalization $glob)
setGlobalState (line 402)

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.

  • access: public
void setGlobalState (string $key, mixed $value, [mixed $defaultValue = null])
  • string $key: the name of the value to be set
  • mixed $value: the global value to be set
  • mixed $defaultValue: the default value. If $key is not found, $defaultValue will be returned
setID (line 458)
  • access: public
void setID (string $value)
  • string $value: application ID
setMode (line 482)
  • access: public
void setMode (TApplicationMode $value)
setModule (line 526)

Adds a module to application.

Note, this method does not do module initialization.

  • access: public
void setModule (string $id, IModule $module)
  • string $id: ID of the module
  • IModule $module: module object
setPageService (line 579)

Registers the page service instance.

This method should only be used by framework developers.

  • access: public
void setPageService (TPageService $service)
setRequest (line 600)
  • access: public
void setRequest (THttpRequest $request)
setResponse (line 621)
  • access: public
void setResponse (THttpResponse $response)
setSecurityManager (line 684)
  • access: public
void setSecurityManager (TSecurityManager $sm)
setSession (line 642)
  • access: public
void setSession (THttpSession $session)
setUser (line 758)
  • access: public
void setUser (IUser $user)
  • IUser $user: the application user

Inherited Methods

Inherited From TComponent

TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__get()
TComponent::__set()
Class Constants
CONFIGCACHE_FILE = 'config.cache' (line 139)

Config cache file

CONFIG_FILE = 'application.xml' (line 131)

Application configuration file name

GLOBAL_FILE = 'global.cache' (line 143)

Global data file

PAGE_SERVICE_ID = 'page' (line 127)

Page service ID

RUNTIME_PATH = 'runtime' (line 135)

Runtime directory name

STATE_DEBUG = 'Debug' (line 120)
STATE_NORMAL = 'Normal' (line 121)
STATE_OFF = 'Off' (line 119)

possible application mode.

  • deprecated: deprecated since version 3.0.4 (use TApplicationMode constants instead)
STATE_PERFORMANCE = 'Performance' (line 122)

Documentation generated on Sun, 14 Jan 2007 21:41:03 -0500 by phpDocumentor 1.3.0RC4