CAssetManager
鍖 | system.web |
---|---|
缁ф壙 | class CAssetManager » CApplicationComponent » CComponent |
瀹炵幇 | IApplicationComponent |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CAssetManager.php 3449 2011-11-20 20:42:45Z alexander.makarow $ |
婧愮爜 | framework/web/CAssetManager.php |
瀹炵幇杩欎釜鍔熻兘鐨勬柟寮忓氨鏄鍒惰繖浜涙枃浠跺埌涓涓猈eb鍙闂洰褰曚笅, 骞朵笖杩斿洖杩欎釜鐩綍鎵瀵瑰簲鐨勫彲璁块棶URL銆
濡傛灉瑕佸彂甯冧竴涓猘sset锛屽彧闇瑕佽皟鐢 publish().
Web鍙闂洰褰曠敱 basePath 鎸囧畾銆 榛樿鏄痑ssets鐩綍銆 鍦ㄨ繖涓洰褰曚笅锛屽偍瀛樼潃鏁翠釜搴旂敤鐨勮繖绫绘枃浠躲 灞炴 baseUrl 鎸囧悜 basePath 鐨勫彲璁块棶URL銆
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
basePath | string | the root directory storing the published asset files. | CAssetManager |
baseUrl | string | the base url that the published asset files can be accessed. | CAssetManager |
behaviors | array | 杩欎釜搴旂敤缁勪欢闄勫姞鐨勮涓恒 杩欐琛屼负灏嗗湪搴旂敤缁勪欢璋冪敤init鏃堕檮鍔犲湪搴旂敤缁勪欢涓娿 璇峰弬鐓CModel::behaviors濡備綍鎸囧畾姝ゅ睘鎬у笺 | CApplicationComponent |
excludeFiles | array | list of directories and files which should be excluded from the publishing process. | CAssetManager |
isInitialized | boolean | 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 | CApplicationComponent |
linkAssets | boolean | whether to use symbolic link to publish asset files. | CAssetManager |
newDirMode | integer | the permission to be set for newly generated asset directories. | CAssetManager |
newFileMode | integer | the permission to be set for newly generated asset files. | CAssetManager |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
__get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
__isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
__set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
__unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
getBasePath() | 杩斿洖the root directory storing the published asset files. Defaults to 'WebRoot/assets'. | CAssetManager |
getBaseUrl() | 杩斿洖the base url that the published asset files can be accessed. Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'. | CAssetManager |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getIsInitialized() | 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 | CApplicationComponent |
getPublishedPath() | Returns the published path of a file path. | CAssetManager |
getPublishedUrl() | Returns the URL of a published file path. | CAssetManager |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
init() | 鍒濆鍖栧簲鐢ㄧ粍浠躲 | CApplicationComponent |
publish() | Publishes a file or a directory. | CAssetManager |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
setBasePath() | Sets the root directory storing published asset files. | CAssetManager |
setBaseUrl() | 璁剧疆the base url that the published asset files can be accessed | CAssetManager |
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
hash() | Generate a CRC32 hash for the directory path. Collisions are higher | CAssetManager |
灞炴ц缁
the root directory storing the published asset files. Defaults to 'WebRoot/assets'.
the base url that the published asset files can be accessed. Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'.
list of directories and files which should be excluded from the publishing process. Defaults to exclude '.svn' files only. This option has no effect if linkAssets is enabled.
whether to use symbolic link to publish asset files. Defaults to false, meaning
asset files are copied to public folders. Using symbolic links has the benefit that the published
assets will always be consistent with the source assets. This is especially useful during development.
However, there are special requirements for hosting environments in order to use symbolic links.
In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater.
The latter requires PHP 5.3 or greater.
Moreover, some Web servers need to be properly configured so that the linked assets are accessible
to Web users. For example, for Apache Web server, the following configuration directive should be added
for the Web folder:
Options FollowSymLinks
the permission to be set for newly generated asset directories. This value will be used by PHP chmod function. Defaults to 0777, meaning the directory can be read, written and executed by all users.
the permission to be set for newly generated asset files. This value will be used by PHP chmod function. Defaults to 0666, meaning the file is read-writable by all users.
鏂规硶璇︾粏
public string getBasePath()
| ||
{return} | string | the root directory storing the published asset files. Defaults to 'WebRoot/assets'. |
public function getBasePath()
{
if($this->_basePath===null)
{
$request=Yii::app()->getRequest();
$this->setBasePath(dirname($request->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
}
return $this->_basePath;
}
public string getBaseUrl()
| ||
{return} | string | the base url that the published asset files can be accessed. Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'. |
public function getBaseUrl()
{
if($this->_baseUrl===null)
{
$request=Yii::app()->getRequest();
$this->setBaseUrl($request->getBaseUrl().'/'.self::DEFAULT_BASEPATH);
}
return $this->_baseUrl;
}
public string getPublishedPath(string $path, boolean $hashByName=false)
| ||
$path | string | directory or file path being published |
$hashByName | boolean | whether the published directory should be named as the hashed basename. If false, the name will be the hash taken from dirname of the path being published and path mtime. Defaults to false. Set true if the path being published is shared among different extensions. |
{return} | string | the published file path. False if the file or directory does not exist |
public function getPublishedPath($path,$hashByName=false)
{
if(($path=realpath($path))!==false)
{
$base=$this->getBasePath().DIRECTORY_SEPARATOR;
if(is_file($path))
return $base . $this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)) . DIRECTORY_SEPARATOR . basename($path);
else
return $base . $this->hash($hashByName ? basename($path) : $path.filemtime($path));
}
else
return false;
}
Returns the published path of a file path. This method does not perform any publishing. It merely tells you if the file or directory is published, where it will go.
public string getPublishedUrl(string $path, boolean $hashByName=false)
| ||
$path | string | directory or file path being published |
$hashByName | boolean | whether the published directory should be named as the hashed basename. If false, the name will be the hash taken from dirname of the path being published and path mtime. Defaults to false. Set true if the path being published is shared among different extensions. |
{return} | string | the published URL for the file or directory. False if the file or directory does not exist. |
public function getPublishedUrl($path,$hashByName=false)
{
if(isset($this->_published[$path]))
return $this->_published[$path];
if(($path=realpath($path))!==false)
{
if(is_file($path))
return $this->getBaseUrl().'/'.$this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)).'/'.basename($path);
else
return $this->getBaseUrl().'/'.$this->hash($hashByName ? basename($path) : $path.filemtime($path));
}
else
return false;
}
Returns the URL of a published file path. This method does not perform any publishing. It merely tells you if the file path is published, what the URL will be to access it.
protected string hash(string $path)
| ||
$path | string | string to be hashed. |
{return} | string | hashed string. |
protected function hash($path)
{
return sprintf('%x',crc32($path.Yii::getVersion()));
}
Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.
public string publish(string $path, boolean $hashByName=false, integer $level=-1, boolean $forceCopy=false)
| ||
$path | string | the asset (file or directory) to be published |
$hashByName | boolean | whether the published directory should be named as the hashed basename. If false, the name will be the hash taken from dirname of the path being published and path mtime. Defaults to false. Set true if the path being published is shared among different extensions. |
$level | integer | level of recursive copying when the asset is a directory. Level -1 means publishing all subdirectories and files; Level 0 means publishing only the files DIRECTLY under the directory; level N means copying those directories that are within N levels. |
$forceCopy | boolean | whether we should copy the asset file or directory even if it is already published before. This parameter is set true mainly during development stage when the original assets are being constantly changed. The consequence is that the performance is degraded, which is not a concern during development, however. This parameter has been available since version 1.1.2. |
{return} | string | an absolute URL to the published asset |
public function publish($path,$hashByName=false,$level=-1,$forceCopy=false)
{
if(isset($this->_published[$path]))
return $this->_published[$path];
else if(($src=realpath($path))!==false)
{
if(is_file($src))
{
$dir=$this->hash($hashByName ? basename($src) : dirname($src).filemtime($src));
$fileName=basename($src);
$dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
$dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName;
if($this->linkAssets)
{
if(!is_file($dstFile))
{
if(!is_dir($dstDir))
{
mkdir($dstDir);
@chmod($dstDir, $this->newDirMode);
}
symlink($src,$dstFile);
}
}
else if(@filemtime($dstFile)<@filemtime($src))
{
if(!is_dir($dstDir))
{
mkdir($dstDir);
@chmod($dstDir, $this->newDirMode);
}
copy($src,$dstFile);
@chmod($dstFile, $this->newFileMode);
}
return $this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName";
}
else if(is_dir($src))
{
$dir=$this->hash($hashByName ? basename($src) : $src.filemtime($src));
$dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
if($this->linkAssets)
{
if(!is_dir($dstDir))
symlink($src,$dstDir);
}
else if(!is_dir($dstDir) || $forceCopy)
{
CFileHelper::copyDirectory($src,$dstDir,array(
'exclude'=>$this->excludeFiles,
'level'=>$level,
'newDirMode'=>$this->newDirMode,
'newFileMode'=>$this->newFileMode,
));
}
return $this->_published[$path]=$this->getBaseUrl().'/'.$dir;
}
}
throw new CException(Yii::t('yii','The asset "{asset}" to be published does not exist.',
array('{asset}'=>$path)));
}
Publishes a file or a directory. This method will copy the specified asset to a web accessible directory and return the URL for accessing the published asset.
- If the asset is a file, its file modification time will be checked to avoid unnecessary file copying;
- If the asset is a directory, all files and subdirectories under it will be published recursively. Note, in case $forceCopy is false the method only checks the existence of the target directory to avoid repetitive copying.
Note: On rare scenario, a race condition can develop that will lead to a one-time-manifestation of a non-critical problem in the creation of the directory that holds the published assets. This problem can be avoided altogether by 'requesting' in advance all the resources that are supposed to trigger a 'publish()' call, and doing that in the application deployment phase, before system goes live. See more in the following discussion: http://code.google.com/p/yii/issues/detail?id=2579
public void setBasePath(string $value)
| ||
$value | string | the root directory storing published asset files |
public function setBasePath($value)
{
if(($basePath=realpath($value))!==false && is_dir($basePath) && is_writable($basePath))
$this->_basePath=$basePath;
else
throw new CException(Yii::t('yii','CAssetManager.basePath "{path}" is invalid. Please make sure the directory exists and is writable by the Web server process.',
array('{path}'=>$value)));
}
Sets the root directory storing published asset files.
public void setBaseUrl(string $value)
| ||
$value | string | the base url that the published asset files can be accessed |
public function setBaseUrl($value)
{
$this->_baseUrl=rtrim($value,'/');
}