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 IModule interface. Each module accomplishes certain functionalities that are shared by all Prado components in an application. There are default modules, composer 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.
TApplicationConfiguration loads the composer.json for each installed composer extension and checks the extra field for a "bootstrap" class for the package. Packages can be specified as a configuration module (without a class) to load the composer extension module. The ID of the module is the name of the package.
TApplication has four modes that can be changed by setting \Prado\setMode 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
- onInitComplete : this event happens right after after module and service initialization. This event is particularly useful for CLI/Shell applications
- 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:
$application=new TApplication($configFile);
$application->run();
Class hierarchy
- \Prado\TApplication implements ISingleton
- \Prado\TComponent
Since: 3.0
public
|
__construct([string $basePath = 'protected' ][, bool $cacheConfig = true ][, string $configType = self::CONFIG_TYPE_XML ]) : mixed
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. |
public
|
applyConfiguration(TApplicationConfiguration $config[, bool $withinService = false ]) : mixed
Applies an application configuration.
|
public
|
clearGlobalState(string $key) : mixed
Clears a global value.
The value cleared will no longer be available in this request and the following requests. |
public
|
completeRequest() : mixed
Completes current request processing.
This method can be used to exit the application lifecycles after finishing the current cycle. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
getGlobalState(string $key[, mixed $defaultValue = null ]) : mixed
Returns a global value.
A global value is one that is persistent across users sessions and requests. |
public
|
|
public
|
|
public
|
|
public
|
getModules() : array<string|int, TModule>
Returns a list of application modules indexed by module IDs.
Modules that have not been loaded yet are returned as null objects. |
public
|
getModulesByType(string $type[, bool $strict = false ]) : array<string|int, mixed>
Returns a list of application modules of a specific class.
Lazy Loading Modules are not loaded, and are null but have an ID Key. When null modules are found, load them with getModule. eg.
|
public
|
|
public
|
getParameters() : TMap
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. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
onAuthentication() : mixed
Raises OnAuthentication event.
This method is invoked when the user request needs to be authenticated. |
public
|
onAuthenticationComplete() : mixed
Raises OnAuthenticationComplete event.
This method is invoked right after the user request is authenticated. |
public
|
onAuthorization() : mixed
Raises OnAuthorization event.
This method is invoked when the user request needs to be authorized. |
public
|
onAuthorizationComplete() : mixed
Raises OnAuthorizationComplete event.
This method is invoked right after the user request is authorized. |
public
|
onBeginRequest() : mixed
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. |
public
|
onEndRequest() : mixed
Raises OnEndRequest event.
This method is invoked when the application completes the processing of the request. |
public
|
onError(mixed $param) : mixed
Raises OnError event.
This method is invoked when an exception is raised during the lifecycles of the application. |
public
|
onInitComplete() : mixed
Raises onInitComplete event.
At the time when this method is invoked, application modules are loaded, user request is resolved and the corresponding service is loaded and initialized. The application is about to start processing the user request. This call is important for CLI/Shell applications that do not have a web service lifecycle stack. This is the first and last event for finalization of any loaded modules in CLI/Shell mode. |
public
|
onLoadState() : mixed
Raises OnLoadState event.
This method is invoked when the application needs to load state (probably stored in session). |
public
|
onLoadStateComplete() : mixed
Raises OnLoadStateComplete event.
This method is invoked right after the application state has been loaded. |
public
|
onPreFlushOutput() : mixed
Raises OnPreFlushOutput event.
This method is invoked right before the application flushes output to client. |
public
|
onPreRunService() : mixed
Raises OnPreRunService event.
This method is invoked right before the service is to be run. |
public
|
onSaveState() : mixed
Raises OnSaveState event.
This method is invoked when the application needs to save state (probably stored in session). |
public
|
onSaveStateComplete() : mixed
Raises OnSaveStateComplete event.
This method is invoked right after the application state has been saved. |
public
|
onSetUser(IUser $user) : mixed
Raises onSetUser event.
Allows modules/components to run handlers when the Application User is set. e.g. A user module could set the $_SERVER['HTTP_ACCEPT_LANGUAGE'] and $_SERVER['HTTP_ACCEPT_CHARSET'] in a cli environment to the user's last web Language and Charset so Emails (and other templates) get language customized. |
public
|
run() : mixed
Executes the lifecycles of the application.
This is the main entry function that leads to the running of the whole Prado application. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
setGlobalState(string $key, mixed $value[, null|mixed $defaultValue = null ][, bool $forceSave = false ]) : mixed
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. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
static
|
singleton([bool $create = true ]) : object|null
Returns the current Prado application. This enables application behaviors to
be used for undefined static function calls via {@see \Prado\TComponent::__callStatic}.
|
public
|
startService(string $serviceID) : mixed
Starts the specified service.
The service instance will be created. Its properties will be initialized and the configurations will be applied, if any. |
protected
|
|
protected
|
initApplication() : mixed
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. Lastly, the onInitComplete event is raised. |
protected
|
|
protected
|
loadGlobals() : mixed
Loads global values from persistent storage.
This method is invoked when \Prado\onLoadState event is raised. After this method, values that are stored in previous requests become available to the current request via getGlobalState. |
protected
|
resolvePaths(string $basePath) : mixed
Resolves application-relevant paths.
This method is invoked by the application constructor to determine the application configuration file, application root path and the runtime path. |
protected
|
saveGlobals() : mixed
Saves global values into persistent storage.
This method is invoked when \Prado\onSaveState event is raised. |
public
mixed
|
CONFIG_FILE_EXT_PHP
File extension for external config files
|
'.php'
|
public
mixed
|
CONFIG_FILE_EXT_XML
File extension for external config files
|
'.xml'
|
public
mixed
|
CONFIG_FILE_PHP
Application configuration file name
|
'application.php'
|
public
mixed
|
CONFIG_FILE_XML
Application configuration file name
|
'application.xml'
|
public
mixed
|
CONFIG_TYPE_PHP
Configuration file type, application.php and config.php
|
'php'
|
public
mixed
|
CONFIG_TYPE_XML
Configuration file type, application.xml and config.xml
|
'xml'
|
public
mixed
|
CONFIGCACHE_FILE
Config cache file
|
'config.cache'
|
public
mixed
|
GLOBAL_FILE
Global data file
|
'global.cache'
|
public
mixed
|
PAGE_SERVICE_ID
Page service ID
|
'page'
|
public
mixed
|
RUNTIME_PATH
Runtime directory name
|
'runtime'
|
\Prado\TComponent::GLOBAL_RAISE_EVENT_LISTENER |