PHP Class Prado\TApplication

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 {@link 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 {@link setMode 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 {@link completeRequest}. Examples: - Create and run a Prado application: $application=new TApplication($configFile); $application->run();
Since: 3.0
Author: Qiang Xue ([email protected])
Inheritance: extends TComponent
Mostrar archivo Open project: pradosoft/prado

Public Methods

Method Description
__construct ( $basePath = 'protected', $cacheConfig = true, $configType = self::CONFIG_TYPE_XML ) Constructor.
applyConfiguration ( $config, $withinService = false ) Applies an application configuration.
clearGlobalState ( $key ) Clears a global value.
completeRequest ( ) Completes current request processing.
flushOutput ( $continueBuffering = true ) Flushes output to client side.
getApplicationStatePersister ( ) : prado\IStatePersister
getAssetManager ( ) : TAssetManager
getAuthorizationRules ( ) : TAuthorizationRuleCollection
getBasePath ( ) : string
getCache ( ) : ICache
getConfigurationFile ( ) : string
getConfigurationFileExt ( ) : string
getConfigurationFileName ( ) : string
getConfigurationType ( ) : string
getErrorHandler ( ) : TErrorHandler
getGlobalState ( $key, $defaultValue = null ) : mixed Returns a global value.
getGlobalization ( $createIfNotExists = true ) : TGlobalization
getID ( ) : string
getMode ( ) : TApplicationMode
getModule ( $id ) : prado\IModule
getModules ( ) : array Returns a list of application modules indexed by module IDs.
getPageServiceID ( ) : string
getParameters ( ) : TMap Returns the list of application parameters.
getRequest ( ) : THttpRequest
getRequestCompleted ( ) : boolean
getResponse ( ) : THttpResponse
getRuntimePath ( ) : string
getSecurityManager ( ) : TSecurityManager
getService ( ) : prado\IService
getSession ( ) : THttpSession
getUniqueID ( ) : string
getUser ( ) : IUser
onAuthentication ( ) Raises OnAuthentication event.
onAuthenticationComplete ( ) Raises OnAuthenticationComplete event.
onAuthorization ( ) Raises OnAuthorization event.
onAuthorizationComplete ( ) Raises OnAuthorizationComplete event.
onBeginRequest ( ) Raises OnBeginRequest event.
onEndRequest ( ) Raises OnEndRequest event.
onError ( $param ) Raises OnError event.
onLoadState ( ) Raises OnLoadState event.
onLoadStateComplete ( ) Raises OnLoadStateComplete event.
onPreFlushOutput ( ) Raises OnPreFlushOutput event.
onPreRunService ( ) Raises OnPreRunService event.
onSaveState ( ) Raises OnSaveState event.
onSaveStateComplete ( ) Raises OnSaveStateComplete event.
run ( ) Executes the lifecycles of the application.
runService ( ) Runs the requested service.
setApplicationStatePersister ( prado\IStatePersister $persister )
setAssetManager ( TAssetManager $value )
setBasePath ( $value )
setCache ( Prado\Caching\ICache $cache )
setConfigurationFile ( $value )
setConfigurationType ( $value )
setErrorHandler ( TErrorHandler $handler )
setGlobalState ( $key, $value, $defaultValue = null, $forceSave = false ) Sets a global value.
setGlobalization ( TGlobalization $glob )
setID ( $value )
setMode ( $value )
setModule ( $id, prado\IModule $module = null ) Adds a module to application.
setPageServiceID ( $value )
setRequest ( THttpRequest $request )
setResponse ( THttpResponse $response )
setRuntimePath ( $value )
setSecurityManager ( TSecurityManager $sm )
setService ( $value )
setSession ( THttpSession $session )
setUser ( Prado\Security\IUser $user )
startService ( $serviceID ) Starts the specified service.

Protected Methods

Method Description
getApplicationConfigurationClass ( )
initApplication ( ) Loads configuration and initializes application.
internalLoadModule ( $id, $force = false )
loadGlobals ( ) Loads global values from persistent storage.
resolvePaths ( $basePath ) Resolves application-relevant paths.
saveGlobals ( ) Saves global values into persistent storage.

Method Details

__construct() public method

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.
public __construct ( $basePath = 'protected', $cacheConfig = true, $configType = self::CONFIG_TYPE_XML )

applyConfiguration() public method

Applies an application configuration.
public applyConfiguration ( $config, $withinService = false )

clearGlobalState() public method

The value cleared will no longer be available in this request and the following requests.
public clearGlobalState ( $key )

completeRequest() public method

This method can be used to exit the application lifecycles after finishing the current cycle.
public completeRequest ( )

flushOutput() public method

Flushes output to client side.
public flushOutput ( $continueBuffering = true )

getApplicationConfigurationClass() protected method

getApplicationStatePersister() public method

public getApplicationStatePersister ( ) : prado\IStatePersister
return prado\IStatePersister application state persister

getAssetManager() public method

public getAssetManager ( ) : TAssetManager
return Prado\Web\TAssetManager asset manager

getAuthorizationRules() public method

public getAuthorizationRules ( ) : TAuthorizationRuleCollection
return TAuthorizationRuleCollection list of authorization rules for the current request

getBasePath() public method

public getBasePath ( ) : string
return string the directory containing the application configuration file (absolute path)

getCache() public method

public getCache ( ) : ICache
return ICache the cache module, null if cache module is not installed

getConfigurationFile() public method

public getConfigurationFile ( ) : string
return string the application configuration file (absolute path)

getConfigurationFileExt() public method

public getConfigurationFileExt ( ) : string
return string the application configuration type. default is 'xml'

getConfigurationFileName() public method

public getConfigurationFileName ( ) : string
return string the default configuration file name

getConfigurationType() public method

public getConfigurationType ( ) : string
return string the application configuration file (absolute path)

getErrorHandler() public method

public getErrorHandler ( ) : TErrorHandler
return Prado\Exceptions\TErrorHandler the error handler module

getGlobalState() public method

A global value is one that is persistent across users sessions and requests.
public getGlobalState ( $key, $defaultValue = null ) : mixed
return mixed the global value corresponding to $key

getGlobalization() public method

public getGlobalization ( $createIfNotExists = true ) : TGlobalization
return Prado\I18N\TGlobalization globalization module

getID() public method

public getID ( ) : string
return string application ID

getMode() public method

public getMode ( ) : TApplicationMode
return TApplicationMode application mode. Defaults to TApplicationMode::Debug.

getModule() public method

public getModule ( $id ) : prado\IModule
return prado\IModule the module with the specified ID, null if not found

getModules() public method

Modules that have not been loaded yet are returned as null objects.
public getModules ( ) : array
return array list of loaded application modules, indexed by module IDs

getPageServiceID() public method

public getPageServiceID ( ) : string
return string page service ID

getParameters() public method

Since the parameters are returned as a {@link \Prado\Collections\TMap} object, you may use the returned result to access, add or remove individual parameters.
public getParameters ( ) : TMap
return Prado\Collections\TMap the list of application parameters

getRequest() public method

public getRequest ( ) : THttpRequest
return Prado\Web\THttpRequest the request module

getRequestCompleted() public method

public getRequestCompleted ( ) : boolean
return boolean whether the current request is processed.

getResponse() public method

public getResponse ( ) : THttpResponse
return Prado\Web\THttpResponse the response module

getRuntimePath() public method

public getRuntimePath ( ) : string
return string the directory storing cache data and application-level persistent data. (absolute path)

getSecurityManager() public method

public getSecurityManager ( ) : TSecurityManager
return Prado\Security\TSecurityManager the security manager module

getService() public method

public getService ( ) : prado\IService
return prado\IService the currently requested service

getSession() public method

public getSession ( ) : THttpSession
return Prado\Web\THttpSession the session module, null if session module is not installed

getUniqueID() public method

public getUniqueID ( ) : string
return string an ID that uniquely identifies this Prado application from the others

getUser() public method

public getUser ( ) : IUser
return IUser the application user

initApplication() protected method

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.
protected initApplication ( )

internalLoadModule() protected method

protected internalLoadModule ( $id, $force = false )

loadGlobals() protected method

This method is invoked when {@link onLoadState OnLoadState} event is raised. After this method, values that are stored in previous requests become available to the current request via {@link getGlobalState}.
protected loadGlobals ( )

onAuthentication() public method

This method is invoked when the user request needs to be authenticated.
public onAuthentication ( )

onAuthenticationComplete() public method

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

onAuthorization() public method

This method is invoked when the user request needs to be authorized.
public onAuthorization ( )

onAuthorizationComplete() public method

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

onBeginRequest() public method

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.
public onBeginRequest ( )

onEndRequest() public method

This method is invoked when the application completes the processing of the request.
public onEndRequest ( )

onError() public method

This method is invoked when an exception is raised during the lifecycles of the application.
public onError ( $param )

onLoadState() public method

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

onLoadStateComplete() public method

This method is invoked right after the application state has been loaded.
public onLoadStateComplete ( )

onPreFlushOutput() public method

This method is invoked right before the application flushes output to client.
public onPreFlushOutput ( )

onPreRunService() public method

This method is invoked right before the service is to be run.
public onPreRunService ( )

onSaveState() public method

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

onSaveStateComplete() public method

This method is invoked right after the application state has been saved.
public onSaveStateComplete ( )

resolvePaths() protected method

This method is invoked by the application constructor to determine the application configuration file, application root path and the runtime path.
See also: setBasePath
See also: setRuntimePath
See also: setConfigurationFile
protected resolvePaths ( $basePath )

run() public method

This is the main entry function that leads to the running of the whole Prado application.
public run ( )

runService() public method

Runs the requested service.
public runService ( )

saveGlobals() protected method

This method is invoked when {@link onSaveState OnSaveState} event is raised.
protected saveGlobals ( )

setApplicationStatePersister() public method

public setApplicationStatePersister ( prado\IStatePersister $persister )
$persister prado\IStatePersister

setAssetManager() public method

public setAssetManager ( TAssetManager $value )
$value Prado\Web\TAssetManager

setBasePath() public method

public setBasePath ( $value )

setCache() public method

public setCache ( Prado\Caching\ICache $cache )
$cache Prado\Caching\ICache

setConfigurationFile() public method

public setConfigurationFile ( $value )

setConfigurationType() public method

public setConfigurationType ( $value )

setErrorHandler() public method

public setErrorHandler ( TErrorHandler $handler )
$handler Prado\Exceptions\TErrorHandler

setGlobalState() public method

A global value is one that is persistent across users sessions and requests. Make sure that the value is serializable and unserializable.
public setGlobalState ( $key, $value, $defaultValue = null, $forceSave = false )

setGlobalization() public method

public setGlobalization ( TGlobalization $glob )
$glob Prado\I18N\TGlobalization

setID() public method

public setID ( $value )

setMode() public method

public setMode ( $value )

setModule() public method

Note, this method does not do module initialization.
public setModule ( $id, prado\IModule $module = null )
$module prado\IModule

setPageServiceID() public method

public setPageServiceID ( $value )

setRequest() public method

public setRequest ( THttpRequest $request )
$request Prado\Web\THttpRequest

setResponse() public method

public setResponse ( THttpResponse $response )
$response Prado\Web\THttpResponse

setRuntimePath() public method

public setRuntimePath ( $value )

setSecurityManager() public method

public setSecurityManager ( TSecurityManager $sm )
$sm Prado\Security\TSecurityManager

setService() public method

public setService ( $value )

setSession() public method

public setSession ( THttpSession $session )
$session Prado\Web\THttpSession

setUser() public method

public setUser ( Prado\Security\IUser $user )
$user Prado\Security\IUser

startService() public method

The service instance will be created. Its properties will be initialized and the configurations will be applied, if any.
public startService ( $serviceID )