From bc1a7e1e3a67537189d30013ea6d5fa4bf98e8e4 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 19 Nov 2005 02:42:35 +0000 Subject: --- framework/Exceptions/TErrorHandler.php | 8 +--- framework/Exceptions/TException.php | 4 +- framework/Exceptions/messages.txt | 2 +- framework/Web/TAssetManager.php | 17 +++++++- framework/Web/THttpRequest.php | 2 +- framework/Web/UI/TThemeManager.php | 75 ++++++++++++++++++++++------------ framework/core.php | 17 +++++++- 7 files changed, 86 insertions(+), 39 deletions(-) (limited to 'framework') diff --git a/framework/Exceptions/TErrorHandler.php b/framework/Exceptions/TErrorHandler.php index b5919e5d..52ed9935 100644 --- a/framework/Exceptions/TErrorHandler.php +++ b/framework/Exceptions/TErrorHandler.php @@ -92,9 +92,7 @@ class TErrorHandler extends TComponent implements IModule if($this->_templatePath===null) $this->_templatePath=dirname(__FILE__); $base=$this->_templatePath.'/'.self::ERROR_FILE_NAME; - $lang=array_shift(explode('-',array_shift(Prado::getUserLanguages()))); - if(!empty($lang) && !ctype_alpha($lang)) - $lang=''; + $lang=Prado::getPreferredLanguage(); if(is_file("$base$statusCode-$lang.html")) $errorFile="$base$statusCode-$lang.html"; else if(is_file("$base$statusCode.html")) @@ -175,9 +173,7 @@ class TErrorHandler extends TComponent implements IModule $_SERVER['SERVER_SOFTWARE'].' PRADO/'.Prado::getVersion(), strftime('%Y-%m-%d %H:%m',time()) ); - $lang=array_shift(explode('-',array_shift(Prado::getUserLanguages()))); - if(!empty($lang) && !ctype_alpha($lang)) - $lang=''; + $lang=Prado::getPreferredLanguage(); $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.html'; if(!is_file($exceptionFile)) $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'.html'; diff --git a/framework/Exceptions/TException.php b/framework/Exceptions/TException.php index d920f373..84ff1b89 100644 --- a/framework/Exceptions/TException.php +++ b/framework/Exceptions/TException.php @@ -51,9 +51,7 @@ class TException extends Exception protected function translateErrorMessage($key) { - $lang=array_shift(explode('-',array_shift(Prado::getUserLanguages()))); - if(!empty($lang) && !ctype_alpha($lang)) - $lang=''; + $lang=Prado::getPreferredLanguage(); $msgFile=dirname(__FILE__).'/messages-'.$lang.'.txt'; if(!is_file($msgFile)) $msgFile=dirname(__FILE__).'/messages.txt'; diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index b71b733f..e808938c 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -39,7 +39,7 @@ httpsession_gcprobability_invalid = THttpSession.GCProbability must be an integ httpsession_transid_unchangeable = THttpSession.UseTransparentSessionID cannot be modified after the session is started. httpsession_maxlifetime_unchangeable = THttpSession.Timeout cannot be modified after the session is started. -assetmanager_basepath_invalid = TAssetManager.BasePath '%s' is invalid. Make sure the directory exists and is writable by the Web server process. +assetmanager_basepath_invalid = TAssetManager.BasePath '%s' is invalid. Make sure it is in namespace form and points to a directory writable by the Web server process. assetmanager_basepath_unchangeable = TAssetManager.BasePath cannot be modified after the module is initialized. assetmanager_baseurl_unchangeable = TAssetManager.BaseUrl cannot be modified after the module is initialized. diff --git a/framework/Web/TAssetManager.php b/framework/Web/TAssetManager.php index b48fed79..fa9e649e 100644 --- a/framework/Web/TAssetManager.php +++ b/framework/Web/TAssetManager.php @@ -29,6 +29,13 @@ * with the function {@link publishFilePath}. This is usually * very useful during development. * + * TAssetManager may be configured in application configuration file within + * page service element as follows, + * + * where {@link getBasePath BasePath} and {@link getBaseUrl BaseUrl} are + * configurable properties of TAssetManager. Make sure that BasePath is a namespace + * pointing to a valid directory writable by the Web server process. + * * @author Qiang Xue * @version $Revision: $ $Date: $ * @package System.Web @@ -104,6 +111,8 @@ class TAssetManager extends TComponent implements IModule } /** + * Sets the root directory storing published asset files. + * The directory must be in namespace format. * @param string the root directory storing published asset files * @throws TInvalidOperationException if the service is initialized already */ @@ -111,8 +120,12 @@ class TAssetManager extends TComponent implements IModule { if($this->_initialized) throw new TInvalidOperationException('assetmanager_basepath_unchangeable'); - else if(($this->_basePath=realpath($value))===false) - throw new TInvalidDataValueException('assetmanage_basepath_invalid',$value); + else + { + $this->_basePath=Prado::getPathOfAlias($value); + if($this->_basePath===null || !is_dir($this->_basePath) || !is_writable($this->_basePath)) + throw new TInvalidDataValueException('assetmanage_basepath_invalid',$value); + } } /** diff --git a/framework/Web/THttpRequest.php b/framework/Web/THttpRequest.php index dd1bd662..59e65a7e 100644 --- a/framework/Web/THttpRequest.php +++ b/framework/Web/THttpRequest.php @@ -212,7 +212,7 @@ class THttpRequest extends TComponent implements IModule } /** - * @return string application entry script file path + * @return string application entry script file path (processed w/ realpath()) */ public function getPhysicalApplicationPath() { diff --git a/framework/Web/UI/TThemeManager.php b/framework/Web/UI/TThemeManager.php index f98563e0..b51aecac 100644 --- a/framework/Web/UI/TThemeManager.php +++ b/framework/Web/UI/TThemeManager.php @@ -1,9 +1,13 @@ _application=$application; - if($this->_themePath===null) - $this->_themePath=dirname($application->getRequest()->getPhysicalApplicationPath()).'/'.self::DEFAULT_THEME_PATH; - + if($this->_basePath===null) + $this->_basePath=dirname($application->getRequest()->getPhysicalApplicationPath()).'/'.self::DEFAULT_BASEPATH; + if(($basePath=realpath($this->_basePath))===false || !is_dir($basePath)) + throw new TConfigurationException('thememanager_basepath_invalid',$this->_basePath); + $this->_basePath=$basePath; $this->_initialized=true; $application->getService()->setThemeManager($this); } @@ -55,9 +62,8 @@ class TThemeManager extends TComponent implements IModule public function getTheme($name) { - $themePath=realpath($this->_themePath.'/'.$name); - if($themePath===false || !is_dir($this->_themePath)) - throw new TConfigurationException('thememanager_themepath_invalid',$themePath); + if(($themePath=realpath($this->_basePath.'/'.$name))===false || !is_dir($themePath)) + throw new TConfigurationException('thememanager_theme_inexistent',$name); if(($cache=$this->_application->getCache())!==null) { $array=$cache->get(self::THEME_CACHE_PREFIX.$themePath); @@ -68,7 +74,7 @@ class TThemeManager extends TComponent implements IModule if($this->_application->getMode()!=='Performance') { if(($dir=opendir($themePath))===false) - throw new TIOException('thememanager_themepath_invalid',$themePath); + throw new TIOException('thememanager_theme_inexistent',$name); while(($file=readdir($dir))!==false) { if(basename($file,'.skin')!==$file && filemtime($themePath.'/'.$file)>$timestamp) @@ -83,33 +89,49 @@ class TThemeManager extends TComponent implements IModule return $theme; } } - // not cached, so we parse all skin files + // not cached, so we collect all skin files $content=''; if(($dir=opendir($themePath))===false) - throw new TIOException('thememanager_themepath_invalid',$themePath); + throw new TIOException('thememanager_theme_inexistent',$name); while(($file=readdir($dir))!==false) { if(basename($file,'.skin')!==$file) $content.=file_get_contents($themePath.'/'.$file); } closedir($dir); - $theme=new TTheme($content,$themePath); + + $theme=new TTheme($content,$themePath,$this->_baseUrl); if($cache!==null) $cache->set(self::THEME_CACHE_PREFIX.$themePath,array($theme,time())); return $theme; } - public function getThemePath() + public function getBasePath() { - return $this->_themePath; + return $this->_basePath; } - public function setThemePath($value) + public function setBasePath($value) { if($this->_initialized) - throw new TInvalidOperationException('thememanager_themepath_unchangeable'); + throw new TInvalidOperationException('thememanager_basepath_unchangeable'); else - $this->_themePath=$value; + { + $this->_basePath=Prado::getPathOfAlias($value); + if($this->_basePath===null || !is_dir($this->_basePath)) + throw new TInvalidDataValueException('thememanager_basepath_invalid',$value); + $this->_basePath=$value; + } + } + + public function getBaseUrl() + { + return $this->_baseUrl; + } + + public function setBaseUrl($value) + { + $this->_baseUrl=$value; } } @@ -119,17 +141,20 @@ class TTheme extends TTemplate private $_themeUrl; private $_skins=array(); - public function __construct($content,$themePath) + public function __construct($content,$themePath,$baseUrl) { - $this->_themePath=realpath($themePath); - $basePath=dirname(Prado::getApplication()->getRequest()->getPhysicalApplicationPath()); - if($this->_themePath===false || ($pos=strpos($this->_themePath,$basePath))===false) - throw new TConfigurationException('theme_themepath_invalid',$themePath); - else + $this->_themePath=$themePath; + if($baseUrl===null) { - $baseUrl=dirname(Prado::getApplication()->getRequest()->getApplicationPath()); - $this->_themeUrl=$baseUrl.'/'.strtr(substr($this->_themePath,strlen($basePath)),'\\','/'); + $appPath=dirname(Prado::getApplication()->getRequest()->getPhysicalApplicationPath()); + if(strpos($themePath,$appPath)===false) + throw new TConfigurationException('theme_baseurl_required'); + $appUrl=dirname(Prado::getApplication()->getRequest()->getApplicationPath()); + $this->_themeUrl=$appUrl.'/'.strtr(substr($theme,strlen($basePath)),'\\','/'); } + else + $this->_themeUrl=$baseUrl.'/'.basename($themePath); + $theme=&$this->parse($content); foreach($theme as $skin) { diff --git a/framework/core.php b/framework/core.php index 4abba7a9..efaf7f86 100644 --- a/framework/core.php +++ b/framework/core.php @@ -553,7 +553,7 @@ class PradoBase * 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. + * @return array list of user preferred languages. */ public static function getUserLanguages() { @@ -573,6 +573,21 @@ class PradoBase } return $languages; } + + public static function getPreferredLanguage() + { + static $language=null; + if($language===null) + { + $langs=Prado::getUserLanguages(); + $lang=explode('-',$langs[0]); + if(empty($lang[0]) || !ctype_alpha($lang[0])) + $language='en'; + else + $language=$lang[0]; + } + return $language; + } } ?> \ No newline at end of file -- cgit v1.2.3