From 4c9067bf7ae89b446bb0e236ab20c5b4fa3ee3e3 Mon Sep 17 00:00:00 2001 From: xue <> Date: Tue, 15 Nov 2005 15:34:38 +0000 Subject: Added a new template/theme syntax for URL support. --- framework/Web/TAssetManager.php | 50 +++++++++++++---------------------- framework/Web/THttpRequest.php | 2 +- framework/Web/UI/TControl.php | 25 +++--------------- framework/Web/UI/TTemplate.php | 42 ++++++++++++++++++++++++----- framework/Web/UI/TTemplateManager.php | 6 ++--- framework/Web/UI/TThemeManager.php | 44 ++++++++++++------------------ 6 files changed, 77 insertions(+), 92 deletions(-) (limited to 'framework/Web') diff --git a/framework/Web/TAssetManager.php b/framework/Web/TAssetManager.php index 4bc52eed..b12bc4df 100644 --- a/framework/Web/TAssetManager.php +++ b/framework/Web/TAssetManager.php @@ -25,7 +25,7 @@ * By default, TAssetManager will not publish a file or directory if it already * exists in the publishing directory. You may require a timestamp checking by * setting CheckTimestamp to true (which is false by default). You may also require - * so when calling {@link publishFile} or {@link publishDirectory). This is usually + * so when calling {@link publishFilePath}. This is usually * very useful during development. In production sites, the timestamp checking * should be turned off to improve performance. * @@ -151,24 +151,36 @@ class TAssetManager extends TComponent implements IModule } /** - * Publishes a directory (recursively). + * Publishes a file or a directory (recursively). * This method will copy the content in a directory (recursively) to * a web accessible directory and returns the URL for the directory. * @param string the path to be published * @param boolean whether to use file modify time to ensure every published file is latest * @return string an absolute URL to the published directory */ - public function publishDirectory($path,$checkTimestamp=false) + public function publishFilePath($path,$checkTimestamp=false) { - if(($fullpath=realpath($path))!==false && is_dir($fullpath)) + if(($fullpath=realpath($path))===false) + throw new TInvalidDataValueException('assetmanager_filepath_invalid',$path); + else if(is_file($fullpath)) + { + $dir=md5(dirname($fullpath)); + $file=$this->_basePath.'/'.$dir.'/'.basename($fullpath); + + if(!is_file($file) || (($checkTimestamp || $this->_checkTimestamp) && filemtime($file)_basePath.'/'.$dir); + copy($fullpath,$file); + } + return $this->_baseUrl.'/'.$dir.'/'.basename($fullpath); + } + else { $dir=md5($fullpath); if(!is_dir($this->_basePath.'/'.$dir) || $checkTimestamp || $this->_checkTimestamp) $this->copyDirectory($fullpath,$this->_basePath.'/'.$dir); return $this->_baseUrl.'/'.$dir; } - else - throw new TInvalidDataValueException('assetmanager_directory_invalid',$path); } /** @@ -196,32 +208,6 @@ class TAssetManager extends TComponent implements IModule } closedir($folder); } - - /** - * Publishes a file. - * This method will copy a file to a web accessible directory and - * returns the URL for the file. - * @param string the file to be published - * @param boolean whether to use file modify time to ensure the published file is latest - * @return string an absolute URL to the published file - */ - public function publishFile($path,$checkTimestamp=false) - { - if(($fullpath=realpath($path))!==false && is_file($fullpath)) - { - $dir=md5(dirname($fullpath)); - $file=$this->_basePath.'/'.$dir.'/'.basename($fullpath); - - if(!is_file($file) || (($checkTimestamp || $this->_checkTimestamp) && filemtime($file)_basePath.'/'.$dir); - copy($fullpath,$file); - } - return $this->_baseUrl.'/'.$dir.'/'.basename($fullpath); - } - else - throw new TInvalidDataValueException('assetmanager_file_invalid',$path); - } } ?> \ No newline at end of file diff --git a/framework/Web/THttpRequest.php b/framework/Web/THttpRequest.php index 24606c56..f87111ba 100644 --- a/framework/Web/THttpRequest.php +++ b/framework/Web/THttpRequest.php @@ -219,7 +219,7 @@ class THttpRequest extends TComponent implements IModule */ public function getPhysicalApplicationPath() { - return $_SERVER['SCRIPT_FILENAME']; + return strtr($_SERVER['SCRIPT_FILENAME'],'\\','/'); } /** diff --git a/framework/Web/UI/TControl.php b/framework/Web/UI/TControl.php index 21c633c7..fbdc81ee 100644 --- a/framework/Web/UI/TControl.php +++ b/framework/Web/UI/TControl.php @@ -69,10 +69,6 @@ class TControl extends TComponent * prefix to an ID automatically generated */ const AUTOMATIC_ID_PREFIX='ctl'; - /** - * default control asset path - */ - const ASSET_PATH='assets'; /** * the stage of lifecycles that the control is currently at @@ -302,26 +298,11 @@ class TControl extends TComponent return Prado::getApplication()->getUser(); } - public function publishFile($file) - { - return $this->getService()->getAssetManager()->publishFile($file); - } - - public function publishDirectory($directory) - { - return $this->getService()->getAssetManager()->publishDirectory($directory); - } - - public function getAsset($assetName) + public function getAsset($assetPath) { $class=new ReflectionClass(get_class($this)); - $assetFile=dirname($class->getFileName()).'/'.self::ASSET_PATH.'/'.$assetName; - if(is_file($assetFile)) - return $this->publishFile($assetFile); - else if(is_dir($assetFile)) - return $this->publishDirectory($assetFile); - else - return ''; + $assetPath=dirname($class->getFileName()).'/'.$assetPath; + return $this->getService()->getAssetManager()->publishFilePath($assetPath); } /** diff --git a/framework/Web/UI/TTemplate.php b/framework/Web/UI/TTemplate.php index 0e36f2f4..7683bc1b 100644 --- a/framework/Web/UI/TTemplate.php +++ b/framework/Web/UI/TTemplate.php @@ -58,14 +58,19 @@ class TTemplate extends TComponent implements ITemplate * @var array list of directive settings */ private $_directive=array(); + /** + * @var string context path + */ + private $_contextPath; /** * Constructor. * The template will be parsed after construction. * @param string the template string */ - public function __construct($template) + public function __construct($template,$contextPath) { + $this->_contextPath=$contextPath; $this->parse($template); } @@ -87,6 +92,7 @@ class TTemplate extends TComponent implements ITemplate public function instantiateIn($tplControl) { $page=$tplControl->getPage(); + $assetManager=$page->getService()->getAssetManager(); $controls=array(); foreach($this->_tpl as $key=>$object) { @@ -145,18 +151,28 @@ class TTemplate extends TComponent implements ITemplate $component->$setter($value); else if($value[0]===0) $component->bindProperty($name,$value[1]); - else + else if($value[0]===1) $component->$setter($component->evaluateExpression($value[1])); + else // url + { + $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]); + $component->$setter($url); + } } else throw new TTemplateRuntimeException('property_read_only',get_class($component).'.'.$name); } else if($component->getAllowCustomAttributes()) { - if(is_array($value) && $value[0]===1) - $value=$component->evaluateExpression($value[1]); - else - throw new TTemplateRuntimeException('template_attribute_unbindable',$name); + if(is_array($value)) + { + if($value[0]===1) + $value=$component->evaluateExpression($value[1]); + else if($value[0]===2) + $value=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]); + else + throw new TTemplateRuntimeException('template_attribute_unbindable',$name); + } $component->getAttributes()->add($name,$value); } else @@ -168,8 +184,13 @@ class TTemplate extends TComponent implements ITemplate $component->setSubProperty($name,$value); else if($value[0]===0) $component->bindProperty($name,$value[1]); - else + else if($value[0]===1) $component->setSubProperty($component->evaluateExpression($value[1])); + else + { + $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]); + $component->$setter($url); + } } } $parent=isset($controls[$object[0]])?$controls[$object[0]]:$tplControl; @@ -194,6 +215,11 @@ class TTemplate extends TComponent implements ITemplate $component->$setter($value); else if($value[0]===1) $component->$setter($component->evaluateExpression($value[1])); + else if($value[0]===2) + { + $url=$assetManager->publishFilePath($this->_contextPath.'/'.$value[1]); + $component->$setter($url); + } else throw new TTemplateRuntimeException('template_component_property_unbindable',get_class($component),$name); } @@ -498,6 +524,8 @@ class TTemplate extends TComponent implements ITemplate $attributes[$name]=array(0,substr($value,3,strlen($value)-5)); else if($value[2]==='=') // a dynamic initialization $attributes[$name]=array(1,substr($value,3,strlen($value)-5)); + else if($value[2]==='~') // a URL + $attributes[$name]=array(2,trim(substr($value,3,strlen($value)-5))); else $attributes[$name]=substr($value,2,strlen($value)-4); } diff --git a/framework/Web/UI/TTemplateManager.php b/framework/Web/UI/TTemplateManager.php index 843d2460..2703500a 100644 --- a/framework/Web/UI/TTemplateManager.php +++ b/framework/Web/UI/TTemplateManager.php @@ -40,10 +40,10 @@ class TTemplateManager extends TComponent implements IModule public function getTemplateByFileName($fileName) { - if(is_file($fileName)) + if(($fileName=realpath($fileName))!==false && is_file($fileName)) { if(($cache=$this->_application->getCache())===null) - return new TTemplate(file_get_contents($fileName)); + return new TTemplate(file_get_contents($fileName),dirname($fileName)); else { $array=$cache->get(self::TEMPLATE_CACHE_PREFIX.$fileName); @@ -53,7 +53,7 @@ class TTemplateManager extends TComponent implements IModule if(filemtime($fileName)<$timestamp) return $template; } - $template=new TTemplate(file_get_contents($fileName)); + $template=new TTemplate(file_get_contents($fileName),dirname($fileName)); $cache->set(self::TEMPLATE_CACHE_PREFIX.$fileName,array($template,time())); return $template; } diff --git a/framework/Web/UI/TThemeManager.php b/framework/Web/UI/TThemeManager.php index 019ccc85..7b370746 100644 --- a/framework/Web/UI/TThemeManager.php +++ b/framework/Web/UI/TThemeManager.php @@ -31,7 +31,7 @@ class TThemeManager extends TComponent implements IModule { $this->_application=$application; if($this->_themePath===null) - $this->_themePath=dirname($application->getConfigurationFile()).'/'.self::DEFAULT_THEME_PATH; + $this->_themePath=dirname($application->getRequest()->getPhysicalApplicationPath()).'/'.self::DEFAULT_THEME_PATH; $this->_initialized=true; } @@ -111,14 +111,21 @@ class TThemeManager extends TComponent implements IModule class TTheme extends TTemplate { - const ASSET_PATH='assets'; private $_themePath; - private $_themeFile; + private $_themeUrl; private $_skins=array(); public function __construct($content,$themePath) { - $this->_themePath=$themePath; + $this->_themePath=strtr($themePath,'\\','/'); + $basePath=dirname(Prado::getApplication()->getRequest()->getPhysicalApplicationPath()); + if(($pos=strpos($this->_themePath,$basePath))===false) + throw new TConfigurationException('theme_themepath_invalid',$themePath); + else + { + $baseUrl=dirname(Prado::getApplication()->getRequest()->getApplicationPath()); + $this->_themeUrl=$baseUrl.'/'.strtr(substr($this->_themePath,strlen($basePath)),'\\','/'); + } $theme=&$this->parse($content); foreach($theme as $skin) { @@ -150,7 +157,12 @@ class TTheme extends TTemplate foreach($this->_skins[$type][$id] as $name=>$value) { if(is_array($value)) - $value=$this->evaluateExpression($value[1]); + { + if($value[0]===1) + $value=$this->evaluateExpression($value[1]); + else if($value[0]===2) + $value=$this->_themeUrl.'/'.$value[1]; + } if(strpos($name,'.')===false) // is simple property or custom attribute { if($control->hasProperty($name)) @@ -176,28 +188,6 @@ class TTheme extends TTemplate else return false; } - - public function publishFile($file) - { - return Prado::getApplication()->getService()->getAssetManager()->publishFile($file); - } - - public function publishDirectory($directory) - { - return Prado::getApplication()->getService()->getAssetManager()->publishDirectory($directory); - } - - public function getAsset($assetName) - { - $assetFile=$this->_themePath.'/'.self::ASSET_PATH.'/'.$assetName; - if(is_file($assetFile)) - return $this->publishFile($assetFile); - else if(is_dir($assetFile)) - return $this->publishDirectory($assetFile); - else - return ''; - } - } -- cgit v1.2.3