Yii Framework v1.1.10 绫诲弬鑰

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
CAssetManager 鏄竴涓簲鐢ㄧ粍浠讹紝瀹冨彲浠ョ鐞嗙鏈夋枃浠讹紙绉颁箣涓 assets锛夊苟涓旇鐢ㄦ埛鍙互璁块棶鍒般

瀹炵幇杩欎釜鍔熻兘鐨勬柟寮忓氨鏄鍒惰繖浜涙枃浠跺埌涓涓猈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

灞炴ц缁

basePath 灞炴
public string getBasePath()
public void setBasePath(string $value)

the root directory storing the published asset files. Defaults to 'WebRoot/assets'.

baseUrl 灞炴
public string getBaseUrl()
public void setBaseUrl(string $value)

the base url that the published asset files can be accessed. Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'.

excludeFiles 灞炴 锛堝彲鐢ㄨ嚜 v1.1.6锛
public array $excludeFiles;

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.

linkAssets 灞炴 锛堝彲鐢ㄨ嚜 v1.1.5锛
public boolean $linkAssets;

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

newDirMode 灞炴 锛堝彲鐢ㄨ嚜 v1.1.8锛
public integer $newDirMode;

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.

newFileMode 灞炴 锛堝彲鐢ㄨ嚜 v1.1.8锛
public integer $newFileMode;

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.

鏂规硶璇︾粏

getBasePath() 鏂规硶
public string getBasePath()
{return} string the root directory storing the published asset files. Defaults to 'WebRoot/assets'.
婧愮爜锛 framework/web/CAssetManager.php#96 (鏄剧ず)
public function getBasePath()
{
    if(
$this->_basePath===null)
    {
        
$request=Yii::app()->getRequest();
        
$this->setBasePath(dirname($request->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
    }
    return 
$this->_basePath;
}

getBaseUrl() 鏂规硶
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'.
婧愮爜锛 framework/web/CAssetManager.php#124 (鏄剧ず)
public function getBaseUrl()
{
    if(
$this->_baseUrl===null)
    {
        
$request=Yii::app()->getRequest();
        
$this->setBaseUrl($request->getBaseUrl().'/'.self::DEFAULT_BASEPATH);
    }
    return 
$this->_baseUrl;
}

getPublishedPath() 鏂规硶
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
婧愮爜锛 framework/web/CAssetManager.php#254 (鏄剧ず)
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.

getPublishedUrl() 鏂规硶
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.
婧愮爜锛 framework/web/CAssetManager.php#279 (鏄剧ず)
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.

hash() 鏂规硶
protected string hash(string $path)
$path string string to be hashed.
{return} string hashed string.
婧愮爜锛 framework/web/CAssetManager.php#300 (鏄剧ず)
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.

publish() 鏂规硶
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
婧愮爜锛 framework/web/CAssetManager.php#178 (鏄剧ず)
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.



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

setBasePath() 鏂规硶
public void setBasePath(string $value)
$value string the root directory storing published asset files
婧愮爜锛 framework/web/CAssetManager.php#111 (鏄剧ず)
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.

setBaseUrl() 鏂规硶
public void setBaseUrl(string $value)
$value string the base url that the published asset files can be accessed
婧愮爜锛 framework/web/CAssetManager.php#137 (鏄剧ず)
public function setBaseUrl($value)
{
    
$this->_baseUrl=rtrim($value,'/');
}

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.