From aa2edcf52ee7438876d826283274bf7e271fad4a Mon Sep 17 00:00:00 2001 From: xue <> Date: Thu, 17 Nov 2005 15:28:20 +0000 Subject: Modified how errors are handled. --- framework/Data/TMemCache.php | 4 +- framework/Data/TSqliteCache.php | 2 +- framework/Exceptions/TErrorHandler.php | 62 +++++++-- framework/Exceptions/TException.php | 5 +- framework/Exceptions/error.en | 34 +++++ framework/Exceptions/messages.en | 16 +++ framework/Security/TAuthManager.php | 2 +- framework/TApplication.php | 126 ++++++++--------- framework/Web/Services/TPageService.php | 20 +-- framework/Web/TAssetManager.php | 2 +- framework/Web/THttpRequest.php | 11 +- framework/Web/THttpResponse.php | 2 +- framework/Web/THttpSession.php | 2 +- framework/Web/UI/TControl.php | 2 +- framework/Web/UI/TTheme.php | 4 +- framework/Web/UI/TThemeManager.php | 4 +- framework/core.php | 231 ++++++-------------------------- framework/prado.php | 6 +- 18 files changed, 235 insertions(+), 300 deletions(-) create mode 100644 framework/Exceptions/error.en (limited to 'framework') diff --git a/framework/Data/TMemCache.php b/framework/Data/TMemCache.php index 4d952551..9786b467 100644 --- a/framework/Data/TMemCache.php +++ b/framework/Data/TMemCache.php @@ -97,7 +97,7 @@ class TMemCache extends TComponent implements IModule, ICache * This method is required by the IModule interface. It makes sure that * UniquePrefix has been set, creates a Memcache instance and connects * to the memcache server. - * @param IApplication Prado application, can be null + * @param TApplication Prado application, can be null * @param TXmlElement configuration for this module, can be null * @throws TConfigurationException if memcache extension is not installed or memcache sever connection fails */ @@ -108,7 +108,7 @@ class TMemCache extends TComponent implements IModule, ICache $this->_cache=new Memcache; if($this->_cache->connect($this->_host,$this->_port)===false) throw new TInvalidConfigurationException('memcache_connection_failed'); - if($application instanceof IApplication) + if($application instanceof TApplication) $this->_prefix=$application->getUniqueID(); $this->_initialized=true; $application->setCache($this); diff --git a/framework/Data/TSqliteCache.php b/framework/Data/TSqliteCache.php index f589f93d..eac22dd2 100644 --- a/framework/Data/TSqliteCache.php +++ b/framework/Data/TSqliteCache.php @@ -103,7 +103,7 @@ class TSqliteCache extends TComponent implements IModule, ICache * property is set, and creates a SQLiteDatabase instance for it. * The database or the cache table does not exist, they will be created. * Expired values are also deleted. - * @param IApplication Prado application, can be null + * @param TApplication Prado application, can be null * @param TXmlElement configuration for this module, can be null * @throws TConfigurationException if sqlite extension is not installed, * DbFile is set invalid, or any error happens during creating database or cache table. diff --git a/framework/Exceptions/TErrorHandler.php b/framework/Exceptions/TErrorHandler.php index f88396ce..47feb54c 100644 --- a/framework/Exceptions/TErrorHandler.php +++ b/framework/Exceptions/TErrorHandler.php @@ -1,26 +1,23 @@ attachEventHandler('Error',array($this,'handle')); - $this->_initialized=true; + $this->_application=$application; + $application->attachEventHandler('Error',array($this,'handleError')); $application->setErrorHandler($this); } @@ -40,9 +37,52 @@ class TErrorHandler extends TComponent implements IErrorHandler $this->_id=$value; } - public function handle($sender,$param) - { echo '...........................'; - echo $param; + public function handleError($sender,$param) + { + $type='Unhandled Exception'; + $this->displayException($param,$type); + } + + protected function displayException($exception,$type) + { + $lines=file($exception->getFile()); + $errorLine=$exception->getLine(); + $beginLine=$errorLine-9>=0?$errorLine-9:0; + $endLine=$errorLine+8<=count($lines)?$errorLine+8:count($lines); + $source=''; + for($i=$beginLine;$i<$endLine;++$i) + { + if($i===$errorLine-1) + { + $line=highlight_string(sprintf("Line %4d: %s",$i+1,$lines[$i]),true); + $source.="
".$line."
"; + } + else + $source.=highlight_string(sprintf("Line %4d: %s",$i+1,$lines[$i]),true); + } + $fields=array( + '%%ErrorType%%', + '%%ErrorMessage%%', + '%%SourceFile%%', + '%%SourceCode%%', + '%%StackTrace%%', + '%%Version%%' + ); + $values=array( + get_class($exception), + htmlspecialchars($exception->getMessage()), + htmlspecialchars($exception->getFile()).' ('.$exception->getLine().')', + $source, + htmlspecialchars($exception->getTraceAsString()), + $_SERVER['SERVER_SOFTWARE'].' PRADO/'.Prado::getVersion() + ); + $languages=Prado::getUserLanguages(); + $errorFile=dirname(__FILE__).'/error.'.$languages[0]; + if(!is_file($errorFile)) + $errorFile=dirname(__FILE__).'/error.en'; + if(($content=@file_get_contents($errorFile))===false) + die("Unable to open error template file '$errorFile'."); + echo str_replace($fields,$values,$content); } } diff --git a/framework/Exceptions/TException.php b/framework/Exceptions/TException.php index 0c51e1f1..0bc963f8 100644 --- a/framework/Exceptions/TException.php +++ b/framework/Exceptions/TException.php @@ -51,8 +51,9 @@ class TException extends Exception protected function translateErrorCode($key) { - // $msgFile=dirname(__FILE__).'/messages.'.Prado::getLocale(); - // if(!is_file($msgFile)) + $languages=Prado::getUserLanguages(); + $msgFile=dirname(__FILE__).'/messages.'.$languages[0]; + if(!is_file($msgFile)) $msgFile=dirname(__FILE__).'/messages.en'; if(($entries=@file($msgFile))===false) return $key; diff --git a/framework/Exceptions/error.en b/framework/Exceptions/error.en new file mode 100644 index 00000000..146ed2e8 --- /dev/null +++ b/framework/Exceptions/error.en @@ -0,0 +1,34 @@ + + +%%ErrorType%% + + + + +

%%ErrorType%%

+

Description

+

%%ErrorMessage%%

+

+

Source File

+

%%SourceFile%%

+
+%%SourceCode%% +
+

Stack Trace

+
+
+%%StackTrace%%
+
+
+
%%Version%%
+ + \ No newline at end of file diff --git a/framework/Exceptions/messages.en b/framework/Exceptions/messages.en index d949d88a..f1f6c9ad 100644 --- a/framework/Exceptions/messages.en +++ b/framework/Exceptions/messages.en @@ -1,3 +1,19 @@ +component_property_undefined = Component property '%s.%s' is not defined. + +application_configfile_inexistent = Application configuration file "%s" does not exist. +application_module_existing = Application module "%s" cannot be registered twice. +application_service_invalid = Service "%s" must implement IService interface. +application_service_unknown = Requested service "%s" is not defined. + +appconfig_aliaspath_invalid = Application configuration uses an invalid file path "%s". +appconfig_aliasid_required = Application configuration element must have an "id" attribute. +appconfig_using_invalid = Application configuration element must have a "namespace" attribute. +appconfig_moduleid_required = Application configuration element must have an "id" attribute. +appconfig_moduletype_required = Application configuration must have a "type" attribute. +appconfig_serviceid_required = Application configuration element must have an "id" attribute. +appconfig_servicetype_required = Application configuration must have a "type" attribute. +appconfig_parameterid_required = Application configuration element must have an "id" attribute. + body_contents_not_allowed = %s: body contents are not allowed. control_id_not_unique = Control ID '%s' is not unique for control type '%s'. control_not_found = Unable to find a control with ID '%s'. diff --git a/framework/Security/TAuthManager.php b/framework/Security/TAuthManager.php index 57288c0f..5e9b188f 100644 --- a/framework/Security/TAuthManager.php +++ b/framework/Security/TAuthManager.php @@ -29,7 +29,7 @@ class TAuthManager extends TComponent implements IModule /** * Initializes this module. * This method is required by the IModule interface. - * @param IApplication Prado application, can be null + * @param TApplication Prado application, can be null * @param TXmlElement configuration for this module, can be null */ public function init($application,$config) diff --git a/framework/TApplication.php b/framework/TApplication.php index 441de82e..5b52fd22 100644 --- a/framework/TApplication.php +++ b/framework/TApplication.php @@ -10,6 +10,26 @@ * @package System */ +/** + * Includes TErrorHandler class + */ +require_once(PRADO_DIR.'/Exceptions/TErrorHandler.php'); +/** + * Includes THttpRequest class + */ +require_once(PRADO_DIR.'/Web/THttpRequest.php'); +/** + * Includes THttpResponse class + */ +require_once(PRADO_DIR.'/Web/THttpResponse.php'); +/** + * Includes THttpSession class + */ +require_once(PRADO_DIR.'/Web/THttpSession.php'); +/** + * Includes TAuthorizationRule class + */ +require_once(PRADO_DIR.'/Security/TAuthorizationRule.php'); /** * Includes TPageService class (default service) */ @@ -48,7 +68,7 @@ require_once(PRADO_DIR.'/Web/Services/TPageService.php'); * @package System * @since 3.0 */ -class TApplication extends TComponent implements IApplication +class TApplication extends TComponent { /** * Default service ID @@ -109,10 +129,6 @@ class TApplication extends TComponent implements IApplication * @var string cache file */ private $_cacheFile; - /** - * @var string user type - */ - private $_userType='System.Security.TUser'; /** * @var TErrorHandler error handler module */ @@ -144,24 +160,28 @@ class TApplication extends TComponent implements IApplication /** * Constructor. - * Loads application configuration and initializes application. - * If a cache is specified and present, it will be used instead of the configuration file. - * If the cache file is specified but is not present, the configuration file - * will be parsed and the result is saved in the cache file. + * Initializes the application singleton. This method ensures that users can + * only create one application instance. * @param string configuration file path (absolute or relative to current running script) - * @param string cache file path + * @param string cache file path. This is optional. If it is present, it will + * be used to store and load parsed application configuration (to improve performance). */ - public function __construct($configFile,$cacheFile='') + public function __construct($configFile,$cacheFile=null) { parent::__construct(); Prado::setApplication($this); - if(($this->_configFile=realpath($configFile))===false) - throw new TIOException('application_configfile_invalid',$configFile); + if(($this->_configFile=realpath($configFile))===false || !is_file($this->_configFile)) + throw new TIOException('application_configfile_inexistent',$configFile); $this->_cacheFile=$cacheFile; + // generates unique ID by hashing the configuration file path + $this->_uniqueID=md5($this->_configFile); + $this->_errorHandler=new TErrorHandler; } /** * Executes the lifecycles of the application. + * This is the main entry function that leads to the running of the whole + * Prado application. */ public function run() { @@ -174,7 +194,7 @@ class TApplication extends TComponent implements IApplication while($this->_step<$n) { $method='on'.self::$_steps[$this->_step]; - $this->$method(null); + $this->$method($this); if($this->_requestCompleted && $this->_step<$n-1) $this->_step=$n-1; else @@ -240,17 +260,12 @@ class TApplication extends TComponent implements IApplication /** * Adds a module to application. * Note, this method does not do module initialization. - * Also, if there is already a module with the same ID, an exception will be raised. * @param string ID of the module * @param IModule module object - * @throws TInvalidOperationException if a module with the same ID already exists */ public function setModule($id,IModule $module) { - if(isset($this->_modules[$id])) - throw new TInvalidOperationException('application_module_existing',$id); - else - $this->_modules[$id]=$module; + $this->_modules[$id]=$module; } /** @@ -270,6 +285,9 @@ class TApplication extends TComponent implements IApplication } /** + * Returns the list of application parameters. + * Since the parameters are returned as a {@link TMap} object, you may use + * the returned result to access, add or remove individual parameters. * @return TMap the list of application parameters */ public function getParameters() @@ -374,7 +392,7 @@ class TApplication extends TComponent implements IApplication } /** - * @return TAuthorizationRuleCollection list of authorization rules that may be applied + * @return TAuthorizationRuleCollection list of authorization rules for the current request */ public function getAuthorizationRules() { @@ -385,22 +403,20 @@ class TApplication extends TComponent implements IApplication /** * Loads configuration and initializes application. - * Configuration file will be read and parsed (if a valid cache version exists, + * Configuration file will be read and parsed (if a valid cached version exists, * it will be used instead). Then, modules are created and initialized; - * The requested service is created and initialized. + * Afterwards, the requested service is created and initialized. * @param string configuration file path (absolute or relative to current executing script) * @param string cache file path, empty if no present or needed - * @throws TConfigurationException if config file is not given, or module is redefined of invalid type, or service not defined or of invalid type + * @throws TConfigurationException if module is redefined of invalid type, or service not defined or of invalid type */ protected function initApplication($configFile,$cacheFile) { - if($cacheFile==='' || @filemtime($cacheFile)loadFromFile($configFile); - if($cacheFile!=='') + if($cacheFile!==null) { if(($fp=fopen($cacheFile,'wb'))!==false) { @@ -408,7 +424,7 @@ class TApplication extends TComponent implements IApplication fclose($fp); } else - syslog(LOG_WARNING,'Prado application config cache file '.$cacheFile.' cannot be created.'); + syslog(LOG_WARNING,'Prado application config cache file "'.$cacheFile.'" cannot be created.'); } } else @@ -416,9 +432,6 @@ class TApplication extends TComponent implements IApplication $config=Prado::unserialize(file_get_contents($cacheFile)); } - // generates unique ID by hashing the configuration file path - $this->_uniqueID=md5(realpath($configFile)); - // set path aliases and using namespaces foreach($config->getAliases() as $alias=>$path) Prado::setPathOfAlias($alias,$path); @@ -448,8 +461,6 @@ class TApplication extends TComponent implements IApplication $this->_modules=array(); foreach($config->getModules() as $id=>$moduleConfig) { - if(isset($this->_modules[$id])) - throw new TConfigurationException('application_module_redefined',$id); $module=Prado::createComponent($moduleConfig[0]); $this->_modules[$id]=$module; foreach($moduleConfig[1] as $name=>$value) @@ -485,7 +496,7 @@ class TApplication extends TComponent implements IApplication if($this->hasEventHandler('Error')) $this->raiseEvent('Error',$this,$param); else - echo $param; + $this->_errorHandler->handleError($this,$param); } /** @@ -651,7 +662,6 @@ class TApplicationConfiguration extends TComponent * @var array list of module configurations */ private $_modules=array( - 'error'=>array('TErrorHandler',array(),null), 'request'=>array('THttpRequest',array(),null), 'response'=>array('THttpResponse',array(),null) ); @@ -669,12 +679,10 @@ class TApplicationConfiguration extends TComponent /** * Parses the application configuration file. * @param string configuration file name - * @throws TConfigurationException if config file cannot be read or any parsing error is found + * @throws TConfigurationException if there is any parsing error */ public function loadFromFile($fname) { - if(!is_file($fname)) - throw new TConfigurationException('application_configuration_inexistent',$fname); $configPath=dirname($fname); $dom=new TXmlDocument; $dom->loadFromFile($fname); @@ -696,18 +704,18 @@ class TApplicationConfiguration extends TComponent else $p=realpath($configPath.'/'.$path); if($p===false || !is_dir($p)) - throw new TConfigurationException('application_alias_path_invalid',$id,$path); + throw new TConfigurationException('appconfig_aliaspath_invalid',$id,$path); $this->_aliases[$id]=$p; } else - throw new TConfigurationException('application_alias_element_invalid'); + throw new TConfigurationException('appconfig_aliasid_required'); } foreach($pathsNode->getElementsByTagName('using') as $usingNode) { if(($namespace=$usingNode->getAttribute('namespace'))!==null) $this->_usings[]=$namespace; else - throw new TConfigurationException('application_using_element_invalid'); + throw new TConfigurationException('appconfig_using_invalid'); } } @@ -718,21 +726,13 @@ class TApplicationConfiguration extends TComponent { $properties=$node->getAttributes(); if(($id=$properties->itemAt('id'))===null) - throw new TConfigurationException('application_module_element_invalid'); + throw new TConfigurationException('appconfig_moduleid_required'); if(($type=$properties->remove('type'))===null && isset($this->_modules[$id]) && $this->_modules[$id][2]===null) - { $type=$this->_modules[$id][0]; - unset($this->_modules[$id]); - } if($type===null) - throw new TConfigurationException('application_module_element_invalid'); - if(isset($this->_modules[$id])) - throw new TConfigurationException('application_module_redefined',$id); - else - { - $node->setParent(null); - $this->_modules[$id]=array($type,$properties->toArray(),$node); - } + throw new TConfigurationException('appconfig_moduletype_required',$id); + $node->setParent(null); + $this->_modules[$id]=array($type,$properties->toArray(),$node); } } @@ -743,21 +743,13 @@ class TApplicationConfiguration extends TComponent { $properties=$node->getAttributes(); if(($id=$properties->itemAt('id'))===null) - throw new TConfigurationException('application_service_element_invalid'); + throw new TConfigurationException('appconfig_serviceid_required'); if(($type=$properties->remove('type'))===null && isset($this->_services[$id]) && $this->_services[$id][2]===null) - { $type=$this->_services[$id][0]; - unset($this->_services[$id]); - } if($type===null) - throw new TConfigurationException('application_service_element_invalid'); - if(isset($this->_services[$id])) - throw new TConfigurationException('application_service_redefined',$id); - else - { - $node->setParent(null); - $this->_services[$id]=array($type,$properties->toArray(),$node); - } + throw new TConfigurationException('appconfig_servicetype_required',$id); + $node->setParent(null); + $this->_services[$id]=array($type,$properties->toArray(),$node); } } @@ -768,7 +760,7 @@ class TApplicationConfiguration extends TComponent { $properties=$node->getAttributes(); if(($id=$properties->remove('id'))===null) - throw new TConfigurationException('application_parameter_element_invalid'); + throw new TConfigurationException('appconfig_parameterid_required'); if(($type=$properties->remove('type'))===null) $this->_parameters[$id]=$node->getValue(); else diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php index 0fb39dfb..6c690046 100644 --- a/framework/Web/Services/TPageService.php +++ b/framework/Web/Services/TPageService.php @@ -64,14 +64,14 @@ class TPageService extends TComponent implements IService */ private $_initialized=false; /** - * @var IApplication application + * @var TApplication application */ private $_application; /** * Initializes the service. * This method is required by IService interface and is invoked by application. - * @param IApplication application + * @param TApplication application * @param TXmlElement service configuration */ public function init($application,$config) @@ -536,20 +536,20 @@ class TPageConfiguration extends TComponent $p=str_replace('\\','/',$p); $path=realpath(preg_match('/^\\/|.:\\//',$p)?$p:$configPath.'/'.$p); if($path===false || !is_dir($path)) - throw new TConfigurationException('pageservice_alias_path_invalid',$fname,$id,$p); + throw new TConfigurationException('pageservice_alias_path_invalid',$configPath,$id,$p); if(isset($this->_aliases[$id])) - throw new TConfigurationException('pageservice_alias_redefined',$fname,$id); + throw new TConfigurationException('pageservice_alias_redefined',$configPath,$id); $this->_aliases[$id]=$path; } else - throw new TConfigurationException('pageservice_alias_element_invalid',$fname); + throw new TConfigurationException('pageservice_alias_element_invalid',$configPath); } foreach($pathsNode->getElementsByTagName('using') as $usingNode) { if(($namespace=$usingNode->getAttribute('namespace'))!==null) $this->_usings[]=$namespace; else - throw new TConfigurationException('pageservice_using_element_invalid',$fname); + throw new TConfigurationException('pageservice_using_element_invalid',$configPath); } } @@ -561,7 +561,7 @@ class TPageConfiguration extends TComponent $properties=$node->getAttributes(); $type=$properties->remove('type'); if(($id=$properties->itemAt('id'))===null) - throw new TConfigurationException('pageservice_module_element_invalid',$fname); + throw new TConfigurationException('pageservice_module_element_invalid',$configPath); if(isset($this->_modules[$id])) { if($type===null) @@ -572,10 +572,10 @@ class TPageConfiguration extends TComponent $elements->add($element); } else - throw new TConfigurationException('pageservice_module_redefined',$fname,$id); + throw new TConfigurationException('pageservice_module_redefined',$configPath,$id); } else if($type===null) - throw new TConfigurationException('pageservice_module_element_invalid',$fname); + throw new TConfigurationException('pageservice_module_element_invalid',$configPath); else { $node->setParent(null); @@ -639,7 +639,7 @@ class TPageConfiguration extends TComponent $type=$properties->remove('type'); $id=$properties->itemAt('id'); if($id===null || $type===null) - throw new TConfigurationException('pageservice_page_element_invalid',$fname); + throw new TConfigurationException('pageservice_page_element_invalid',$configPath); if($id===$page) { $this->_properties=array_merge($this->_properties,$properties->toArray()); diff --git a/framework/Web/TAssetManager.php b/framework/Web/TAssetManager.php index f1dfa15b..b5e1f6ec 100644 --- a/framework/Web/TAssetManager.php +++ b/framework/Web/TAssetManager.php @@ -60,7 +60,7 @@ class TAssetManager extends TComponent implements IModule /** * Initializes the module. * This method is required by IModule and is invoked by application. - * @param IApplication application + * @param TApplication application * @param TXmlElement module configuration */ public function init($application,$config) diff --git a/framework/Web/THttpRequest.php b/framework/Web/THttpRequest.php index 9be0db8a..02bf557f 100644 --- a/framework/Web/THttpRequest.php +++ b/framework/Web/THttpRequest.php @@ -85,7 +85,7 @@ class THttpRequest extends TComponent implements IModule /** * Initializes the module. * This method is required by IModule and is invoked by application. - * @param IApplication application + * @param TApplication application * @param TXmlElement module configuration */ public function init($application,$config) @@ -290,12 +290,15 @@ class THttpRequest extends TComponent implements IModule } /** - * @return string languages user browser supports + * Returns a list of user preferred languages. + * The languages are returned as an array. Each array element + * represents a single language preference. The languages are ordered + * according to user preferences. The first language is the most preferred. + * @return array list of user preferred languages. */ public function getUserLanguages() { - // TBD ask wei about this - return $_SERVER['HTTP_ACCEPT_LANGUAGE']; + return Prado::getUserLanguages(); } /** diff --git a/framework/Web/THttpResponse.php b/framework/Web/THttpResponse.php index 777c6621..6146b4f0 100644 --- a/framework/Web/THttpResponse.php +++ b/framework/Web/THttpResponse.php @@ -60,7 +60,7 @@ class THttpResponse extends TComponent implements IModule, ITextWriter * Initializes the module. * This method is required by IModule and is invoked by application. * It starts output buffer if it is enabled. - * @param IApplication application + * @param TApplication application * @param TXmlElement module configuration */ public function init($application,$config) diff --git a/framework/Web/THttpSession.php b/framework/Web/THttpSession.php index ff3af560..fcecde1f 100644 --- a/framework/Web/THttpSession.php +++ b/framework/Web/THttpSession.php @@ -72,7 +72,7 @@ class THttpSession extends TComponent implements IModule * Initializes the module. * This method is required by IModule. * If AutoStart is true, the session will be started. - * @param IApplication prado application instance + * @param TApplication prado application instance */ public function init($application,$config) { diff --git a/framework/Web/UI/TControl.php b/framework/Web/UI/TControl.php index fbdc81ee..d0e9083e 100644 --- a/framework/Web/UI/TControl.php +++ b/framework/Web/UI/TControl.php @@ -251,7 +251,7 @@ class TControl extends TComponent } /** - * @return IApplication the application object that the current page is using + * @return TApplication the application object that the current page is using */ public function getApplication() { diff --git a/framework/Web/UI/TTheme.php b/framework/Web/UI/TTheme.php index 452c473c..38aded50 100644 --- a/framework/Web/UI/TTheme.php +++ b/framework/Web/UI/TTheme.php @@ -18,14 +18,14 @@ class TThemeManager extends TComponent implements IModule */ private $_themePath=null; /** - * @var IApplication application + * @var TApplication application */ private $_application; /** * Initializes the module. * This method is required by IModule and is invoked by application. - * @param IApplication application + * @param TApplication application * @param TXmlElement module configuration */ public function init($application,$config) diff --git a/framework/Web/UI/TThemeManager.php b/framework/Web/UI/TThemeManager.php index 7b370746..5f347e8f 100644 --- a/framework/Web/UI/TThemeManager.php +++ b/framework/Web/UI/TThemeManager.php @@ -17,14 +17,14 @@ class TThemeManager extends TComponent implements IModule */ private $_themePath=null; /** - * @var IApplication application + * @var TApplication application */ private $_application; /** * Initializes the module. * This method is required by IModule and is invoked by application. - * @param IApplication application + * @param TApplication application * @param TXmlElement module configuration */ public function init($application,$config) diff --git a/framework/core.php b/framework/core.php index 095eea97..4abba7a9 100644 --- a/framework/core.php +++ b/framework/core.php @@ -2,7 +2,7 @@ /** * Prado core interfaces and classes. * - * This file contains and includes the definitions of prado core interfaces and classes. + * This file contains and includes the definitions of Prado core interfaces and classes. * * @author Qiang Xue * @link http://www.pradosoft.com/ @@ -38,160 +38,6 @@ require_once(PRADO_DIR.'/Collections/TMap.php'); */ require_once(PRADO_DIR.'/Data/TXmlDocument.php'); -/** - * IApplication interface. - * - * This interface must be implemented by application classes. - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System - * @since 3.0 - */ -interface IApplication -{ - /** - * Defines Error event. - */ - public function onError($param); - /** - * Defines BeginRequest event. - * @param mixed event parameter - */ - public function onBeginRequest($param); - /** - * Defines Authentication event. - * @param mixed event parameter - */ - public function onAuthentication($param); - /** - * Defines PostAuthentication event. - * @param mixed event parameter - */ - public function onPostAuthentication($param); - /** - * Defines Authorization event. - * @param mixed event parameter - */ - public function onAuthorization($param); - /** - * Defines PostAuthorization event. - * @param mixed event parameter - */ - public function onPostAuthorization($param); - /** - * Defines LoadState event. - * @param mixed event parameter - */ - public function onLoadState($param); - /** - * Defines PostLoadState event. - * @param mixed event parameter - */ - public function onPostLoadState($param); - /** - * Defines PreRunService event. - * @param mixed event parameter - */ - public function onPreRunService($param); - /** - * Defines RunService event. - * @param mixed event parameter - */ - public function onRunService($param); - /** - * Defines PostRunService event. - * @param mixed event parameter - */ - public function onPostRunService($param); - /** - * Defines SaveState event. - * @param mixed event parameter - */ - public function onSaveState($param); - /** - * Defines PostSaveState event. - * @param mixed event parameter - */ - public function onPostSaveState($param); - /** - * Defines EndRequest event. - * @param mixed event parameter - */ - public function onEndRequest($param); - /** - * Runs the application. - */ - public function run(); - /** - * Completes and terminates the current request processing. - */ - public function completeRequest(); - /** - * @return string application ID - */ - public function getID(); - /** - * @param string application ID - */ - public function setID($id); - /** - * @return string a unique ID that can uniquely identify the application from the others - */ - public function getUniqueID(); - /** - * @return IUser application user - */ - public function getUser(); - /** - * @param IUser application user - */ - public function setUser(IUser $user); - /** - * @param string module ID - * @return IModule module corresponding to the ID, null if not found - */ - public function getModule($id); - /** - * Adds a module into application. - * @param string module ID - * @param IModule module to be added - * @throws TInvalidOperationException if module with the same ID already exists - */ - public function setModule($id,IModule $module); - /** - * @return array list of modules - */ - public function getModules(); - /** - * @return TMap list of parameters - */ - public function getParameters(); - /** - * @return IService the currently requested service - */ - public function getService(); - /** - * @return THttpRequest the current user request - */ - public function getRequest(); - /** - * @return THttpResponse the response to the request - */ - public function getResponse(); - /** - * @return THttpSession the user session - */ - public function getSession(); - /** - * @return ICache cache that is available to use - */ - public function getCache(); - /** - * @return IErrorHandler error handler - */ - public function getErrorHandler(); -} /** * IModule interface. @@ -207,7 +53,7 @@ interface IModule { /** * Initializes the module. - * @param IApplication the application object + * @param TApplication the application object * @param TXmlElement the configuration for the module */ public function init($application,$configuration); @@ -235,7 +81,7 @@ interface IService { /** * Initializes the service. - * @param IApplication the application object + * @param TApplication the application object * @param TXmlElement the configuration for the service */ public function init($application,$configuration); @@ -253,11 +99,6 @@ interface IService public function run(); } -interface IErrorHandler -{ - public function handle($sender,$param); -} - /** * ICache interface. * @@ -419,7 +260,7 @@ class PradoBase */ private static $_usings=array(); /** - * @var IApplication the application instance + * @var TApplication the application instance */ private static $_application=null; @@ -460,7 +301,7 @@ class PradoBase { if(self::$_application!==null && ($errorHandler=self::$_application->getErrorHandler())!==null) { - $errorHandler->handle($exception); + $errorHandler->handleError($exception); } else { @@ -475,18 +316,18 @@ class PradoBase * Repeated invocation of this method or the application constructor * will cause the throw of an exception. * This method should only be used by framework developers. - * @param IApplication the application instance + * @param TApplication the application instance * @throws TInvalidOperationException if this method is invoked twice or more. */ - public static function setApplication(IApplication $app) + public static function setApplication($application) { if(self::$_application!==null) throw new TInvalidOperationException('prado_application_singleton_required'); - self::$_application=$app; + self::$_application=$application; } /** - * @return IApplication the application singleton, null if the singleton has not be created yet. + * @return TApplication the application singleton, null if the singleton has not be created yet. */ public static function getApplication() { @@ -670,12 +511,11 @@ class PradoBase /** * Fatal error handler. - * This method is used in places where exceptions usually cannot be raised - * (e.g. magic methods). - * It displays the debug backtrace. + * This method displays an error message together with the current call stack. + * The application will exit after calling this method. * @param string error message */ - function fatalError($msg) + public static function fatalError($msg) { echo '

Fatal Error

'; echo '

'.$msg.'

'; @@ -707,27 +547,32 @@ class PradoBase echo ''; exit(1); } -} -/** - * Includes TErrorHandler class - */ -require_once(PRADO_DIR.'/Exceptions/TErrorHandler.php'); -/** - * Includes THttpRequest class - */ -require_once(PRADO_DIR.'/Web/THttpRequest.php'); // include TUser -/** - * Includes THttpResponse class - */ -require_once(PRADO_DIR.'/Web/THttpResponse.php'); -/** - * Includes THttpSession class - */ -require_once(PRADO_DIR.'/Web/THttpSession.php'); -/** - * Includes TAuthorizationRule class - */ -require_once(PRADO_DIR.'/Security/TAuthorizationRule.php'); + /** + * Returns a list of user preferred languages. + * The languages are returned as an array. Each array element + * represents a single language preference. The languages are ordered + * according to user preferences. The first language is the most preferred. + * @return array list of user preferred languages. + */ + public static function getUserLanguages() + { + static $languages=null; + if($languages===null) + { + $languages=array(); + foreach(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']) as $language) + { + $array=split(';q=',trim($language)); + $languages[trim($array[0])]=isset($array[1])?(float)$array[1]:1.0; + } + arsort($languages); + $languages=array_keys($languages); + if(empty($languages)) + $languages[0]='en'; + } + return $languages; + } +} ?> \ No newline at end of file diff --git a/framework/prado.php b/framework/prado.php index 1f0bd7a0..d4ecb8eb 100644 --- a/framework/prado.php +++ b/framework/prado.php @@ -2,7 +2,11 @@ /** * Prado bootstrap file. * - * This file must be included first in order to run prado applications. + * This file is intended to be included in the entry script of Prado applications. + * It defines Prado class by extending PradoBase, a static class providing globally + * available functionalities to Prado applications. It also sets PHP error and + * exception handler functions, and provides a __autoload function which automatically + * loads a class file if the class is not defined. * * @author Qiang Xue * @link http://www.pradosoft.com/ -- cgit v1.2.3