summaryrefslogtreecommitdiff
path: root/framework
diff options
context:
space:
mode:
authorxue <>2005-11-12 18:17:58 +0000
committerxue <>2005-11-12 18:17:58 +0000
commit8a4e5a9b04c479fa027acfba52f4d68cf3bd6679 (patch)
tree461c326e7a2a82a998e329e29b1b28c65d1be7db /framework
parent63386d118321bbff8af2d8c480b89fe226400d53 (diff)
Added asset manager.
Diffstat (limited to 'framework')
-rw-r--r--framework/Web/Services/TPageService.php80
-rw-r--r--framework/Web/TAssetManager.php134
-rw-r--r--framework/Web/UI/TControl.php26
3 files changed, 176 insertions, 64 deletions
diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php
index e4791543..f6609725 100644
--- a/framework/Web/Services/TPageService.php
+++ b/framework/Web/Services/TPageService.php
@@ -38,7 +38,7 @@ class TPageService extends TComponent implements IService
/**
* @var string root path of pages
*/
- private $_pageRootPath;
+ private $_pageBasePath;
/**
* @var string default page
*/
@@ -52,14 +52,6 @@ class TPageService extends TComponent implements IService
*/
private $_pageType;
/**
- * @var string the base URL for accessing published assets
- */
- private $_assetBaseUrl;
- /**
- * @var string the root path for storing published asset files
- */
- private $_assetRootPath;
- /**
* @var array list of initial page property values
*/
private $_properties;
@@ -86,8 +78,8 @@ class TPageService extends TComponent implements IService
{
$this->_application=$application;
- if(($pageRootPath=Prado::getPathOfNamespace($this->_pageRootPath))===null || !is_dir($pageRootPath))
- throw new TConfigurationException('pageservice_pagerootpath_invalid',$this->_pageRootPath);
+ if(($pageBasePath=Prado::getPathOfNamespace($this->_pageBasePath))===null || !is_dir($pageBasePath))
+ throw new TConfigurationException('pageservice_pagebasepath_invalid',$this->_pageBasePath);
$this->_pagePath=$application->getRequest()->getServiceParameter();
if(empty($this->_pagePath))
@@ -99,7 +91,7 @@ class TPageService extends TComponent implements IService
{
$pageConfig=new TPageConfiguration;
$pageConfig->loadXmlElement($config,dirname($application->getConfigurationFile()),null);
- $pageConfig->loadConfigurationFiles($this->_pagePath,$pageRootPath);
+ $pageConfig->loadConfigurationFiles($this->_pagePath,$pageBasePath);
}
else
{
@@ -113,7 +105,7 @@ class TPageService extends TComponent implements IService
// check to see if cache is the latest
$paths=explode('.',$this->_pagePath);
array_pop($paths);
- $configPath=$pageRootPath;
+ $configPath=$pageBasePath;
foreach($paths as $path)
{
if(@filemtime($configPath.'/'.self::CONFIG_FILE)>$timestamp)
@@ -133,7 +125,7 @@ class TPageService extends TComponent implements IService
{
$pageConfig=new TPageConfiguration;
$pageConfig->loadXmlElement($config,dirname($application->getConfigurationFile()),null);
- $pageConfig->loadConfigurationFiles($this->_pagePath,$pageRootPath);
+ $pageConfig->loadConfigurationFiles($this->_pagePath,$pageBasePath);
$cache->set(self::CONFIG_CACHE_PREFIX.$this->_pagePath,array($pageConfig,time()),$this->_cacheExpire<0?0:$this->_cacheExpire);
}
}
@@ -163,7 +155,7 @@ class TPageService extends TComponent implements IService
}
}
- // load modules specified in app config
+ // load modules specified in page directory config
foreach($pageConfig->getModules() as $id=>$moduleConfig)
{
$module=Prado::createComponent($moduleConfig[0]);
@@ -274,61 +266,21 @@ class TPageService extends TComponent implements IService
/**
* @return string root directory (in namespace form) storing pages
*/
- public function getPageRootPath()
+ public function getPageBasePath()
{
- return $this->_pageRootPath;
+ return $this->_pageBasePath;
}
/**
* @param string root directory (in namespace form) storing pages
* @throws TInvalidOperationException if the service is initialized already
*/
- public function setPageRootPath($value)
- {
- if($this->_initialized)
- throw new TInvalidOperationException('pageservice_pagerootpath_unchangeable');
- else
- $this->_pageRootPath=$value;
- }
-
- /**
- * @return string the root directory storing published asset files
- */
- public function getAssetRootPath()
- {
- return $this->_assetRootPath;
- }
-
- /**
- * @param string the root directory storing published asset files
- * @throws TInvalidOperationException if the service is initialized already
- */
- public function setAssetRootPath($value)
- {
- if($this->_initialized)
- throw new TInvalidOperationException('pageservice_assetrootpath_unchangeable');
- else
- $this->_assetRootPath=$value;
- }
-
- /**
- * @return string the base url that the published asset files can be accessed
- */
- public function getAssetBaseUrl()
- {
- return $this->_assetBaseUrl;
- }
-
- /**
- * @param string the base url that the published asset files can be accessed
- * @throws TInvalidOperationException if the service is initialized already
- */
- public function setAssetBaseUrl($value)
+ public function setPageBasePath($value)
{
if($this->_initialized)
- throw new TInvalidOperationException('pageservice_assetbaseurl_unchangeable');
+ throw new TInvalidOperationException('pageservice_pagebasepath_unchangeable');
else
- $this->_assetBaseUrl=$value;
+ $this->_pageBasePath=$value;
}
/**
@@ -347,7 +299,7 @@ class TPageService extends TComponent implements IService
$p=explode('.',$this->_pagePath);
array_pop($p);
array_push($p,$className);
- $path=Prado::getPathOfNamespace($this->_pageRootPath).'/'.implode('/',$p).Prado::CLASS_FILE_EXT;
+ $path=Prado::getPathOfNamespace($this->_pageBasePath).'/'.implode('/',$p).Prado::CLASS_FILE_EXT;
require_once($path);
}
}
@@ -419,7 +371,7 @@ class TPageConfiguration extends TComponent
*/
private $_modules=array(
'template'=>array('System.Web.UI.TTemplateManager',array(),null),
- //'asset'=>array('System.Web.TAssetManager',array(),null)
+ 'asset'=>array('System.Web.TAssetManager',array(),null)
);
/**
* @var array list of parameters
@@ -525,11 +477,11 @@ class TPageConfiguration extends TComponent
* @param string path to the page (dot-connected format)
* @param string root path for pages
*/
- public function loadConfigurationFiles($pagePath,$pageRootPath)
+ public function loadConfigurationFiles($pagePath,$pageBasePath)
{
$paths=explode('.',$pagePath);
$page=array_pop($paths);
- $path=$pageRootPath;
+ $path=$pageBasePath;
foreach($paths as $p)
{
$this->loadFromFile($path.'/'.TPageService::CONFIG_FILE,null);
diff --git a/framework/Web/TAssetManager.php b/framework/Web/TAssetManager.php
new file mode 100644
index 00000000..af4cfd73
--- /dev/null
+++ b/framework/Web/TAssetManager.php
@@ -0,0 +1,134 @@
+<?php
+
+class TAssetManager extends TComponent implements IModule
+{
+ const DEFAULT_BASEPATH='assets';
+ private $_basePath=null;
+ private $_baseUrl=null;
+ /**
+ * @var string module ID
+ */
+ private $_id;
+
+ /**
+ * Initializes the module.
+ * This method is required by IModule and is invoked by application.
+ * @param IApplication application
+ * @param TXmlElement module configuration
+ */
+ public function init($application,$config)
+ {
+ if($this->_basePath===null)
+ $this->_basePath=dirname($application->getRequest()->getPhysicalApplicationPath()).'/'.self::DEFAULT_BASEPATH;
+ if(!is_writable($this->_basePath) || !is_dir($this->_basePath))
+ throw new TConfigurationException('assetmanager_basepath_invalid',$this->_basePath);
+ if($this->_baseUrl===null)
+ $this->_baseUrl=dirname($application->getRequest()->getApplicationPath()).'/'.self::DEFAULT_BASEPATH;
+ }
+
+ /**
+ * @return string id of this module
+ */
+ public function getID()
+ {
+ return $this->_id;
+ }
+
+ /**
+ * @param string id of this module
+ */
+ public function setID($value)
+ {
+ $this->_id=$value;
+ }
+
+ /**
+ * @return string the root directory storing published asset files
+ */
+ public function getBasePath()
+ {
+ return $this->_basePath;
+ }
+
+ /**
+ * @param string the root directory storing published asset files
+ * @throws TInvalidOperationException if the service is initialized already
+ */
+ public function setBasePath($value)
+ {
+ if($this->_initialized)
+ throw new TInvalidOperationException('assetmanager_basepath_unchangeable');
+ else if(is_dir($value))
+ $this->_basePath=realpath($value);
+ else
+ throw new TInvalidDataValueException('assetmanage_basepath_invalid',$value);
+ }
+
+ /**
+ * @return string the base url that the published asset files can be accessed
+ */
+ public function getBaseUrl()
+ {
+ return $this->_baseUrl;
+ }
+
+ /**
+ * @param string the base url that the published asset files can be accessed
+ * @throws TInvalidOperationException if the service is initialized already
+ */
+ public function setBaseUrl($value)
+ {
+ if($this->_initialized)
+ throw new TInvalidOperationException('pageservice_baseurl_unchangeable');
+ else
+ $this->_baseUrl=$value;
+ }
+
+ public function publishDirectory($path,$forceOverwrite=false)
+ {
+ if(($fullpath=realpath($path))!==false && is_dir($fullpath))
+ {
+ $dir=md5($fullpath);
+ if(!is_dir($this->_basePath.'/'.$dir))
+ $this->copyDirectory($this->_basePath.'/'.$dir);
+ return $this->_baseUrl.'/'.$dir;
+ }
+ else
+ throw new TInvalidDataValueException('assetmanager_directory_invalid',$path);
+ }
+
+ protected function copyDirectory($src,$dst)
+ {
+ mkdir($dst);
+ $folder=opendir($src);
+ while($file=readdir($folder))
+ {
+ if($file==='.' || $file==='..')
+ continue;
+ else if(is_file($src.'/'.$file))
+ copy($src.'/'.$file,$dst.'/'.$file);
+ else
+ $this->copyDirectory($src.'/'.$file,$dst.'/'.$file);
+ }
+ closedir($folder);
+ }
+
+ public function publishFile($path,$forceOverwrite=false)
+ {
+ if(($fullpath=realpath($path))!==false && is_file($fullpath))
+ {
+ $dir=md5(dirname($fullpath));
+ $file=$this->_basePath.'/'.$dir.'/'.basename($fullpath);
+ if(!is_file($file))
+ {
+ @mkdir($this->_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/UI/TControl.php b/framework/Web/UI/TControl.php
index a3d3c93a..65993664 100644
--- a/framework/Web/UI/TControl.php
+++ b/framework/Web/UI/TControl.php
@@ -69,6 +69,10 @@ 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
@@ -270,6 +274,28 @@ class TControl extends TComponent
return Prado::getApplication()->getUser();
}
+ 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)
+ {
+ $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 '';
+ }
+
/**
* Returns the id of the control.
* Control ID can be either manually set or automatically generated.