0 follower

CAssetManager

Package system.web
Inheritance class CAssetManager » CApplicationComponent » CComponent
Implements IApplicationComponent
Since 1.0
Version $Id$
Source Code framework/web/CAssetManager.php
CAssetManager is a Web application component that manages private files (called assets) and makes them accessible by Web clients.

It achieves this goal by copying assets to a Web-accessible directory and returns the corresponding URL for accessing them.

To publish an asset, simply call publish().

The Web-accessible directory holding the published files is specified by basePath, which defaults to the "assets" directory under the directory containing the application entry script file. The property baseUrl refers to the URL for accessing the basePath.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
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 the behaviors that should be attached to this component. CApplicationComponent
isInitialized boolean whether this application component has been initialized (i.e., init() is invoked. CApplicationComponent

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
getBasePath() Returns the root directory storing the published asset files CAssetManager
getBaseUrl() Returns the base url that the published asset files can be accessed. Note, the ending slashes are stripped off. CAssetManager
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks whether this application component has been initialized (i.e., init() is invoked.) CApplicationComponent
getPublishedPath() Returns the published path of a file path. CAssetManager
getPublishedUrl() Returns the URL of a published file path. CAssetManager
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. CAssetManager
publish() Publishes a file or a directory. CAssetManager
raiseEvent() Raises an event. CComponent
setBasePath() Sets the root directory storing published asset files. CAssetManager
setBaseUrl() Sets the base url that the published asset files can be accessed CAssetManager

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
hash() Generate a CRC32 hash for the directory path. Collisions are higher CAssetManager

Property Details

basePath property
public string getBasePath()
public void setBasePath(string $value)

the root directory storing the published asset files

baseUrl property
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.

Method Details

getBasePath() method
public string getBasePath()
{return} string the root directory storing the published asset files
Source Code: framework/web/CAssetManager.php#67 (show)
public function getBasePath()
{
    return 
$this->_basePath;
}

getBaseUrl() method
public string getBaseUrl()
{return} string the base url that the published asset files can be accessed. Note, the ending slashes are stripped off.
Source Code: framework/web/CAssetManager.php#90 (show)
public function getBaseUrl()
{
    return 
$this->_baseUrl;
}

getPublishedPath() method
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 hashed dirname of the path being published. 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
Source Code: framework/web/CAssetManager.php#177 (show)
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)) . DIRECTORY_SEPARATOR basename($path);
        else
            return 
$base $this->hash($hashByName basename($path) : $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() method
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 hashed dirname of the path being published. 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.
Source Code: framework/web/CAssetManager.php#202 (show)
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)).'/'.basename($path);
        else
            return 
$this->getBaseUrl().'/'.$this->hash($hashByName basename($path) : $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() method
protected string hash(string $path)
$path string string to be hashed.
{return} string hashed string.
Source Code: framework/web/CAssetManager.php#223 (show)
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.

init() method
public void init()
Source Code: framework/web/CAssetManager.php#54 (show)
public function init()
{
    
parent::init();
    
$request=Yii::app()->getRequest();
    if(
$this->getBasePath()===null)
        
$this->setBasePath(dirname($request->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
    if(
$this->getBaseUrl()===null)
        
$this->setBaseUrl($request->getBaseUrl().'/'.self::DEFAULT_BASEPATH);
}

Initializes the application component. This method is required by IApplicationComponent and is invoked by application.

publish() method
public string publish(string $path, boolean $hashByName=false, integer $level=-1)
$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 hashed dirname of the path being published. 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.
{return} string an absolute URL to the published asset
Source Code: framework/web/CAssetManager.php#126 (show)
public function publish($path,$hashByName=false,$level=-1)
{
    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));
            
$fileName=basename($src);
            
$dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
            
$dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName;

            if(@
filemtime($dstFile)<@filemtime($src))
            {
                if(!
is_dir($dstDir))
                {
                    
mkdir($dstDir);
                    @
chmod($dstDir,0777);
                }
                
copy($src,$dstFile);
            }

            return 
$this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName";
        }
        else if(
is_dir($src))
        {
            
$dir=$this->hash($hashByName basename($src) : $src);
            
$dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;

            if(!
is_dir($dstDir))
                
CFileHelper::copyDirectory($src,$dstDir,array('exclude'=>array('.svn'),'level'=>$level));

            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 this case the method only checks the existence of the target directory to avoid repetitive copying.

setBasePath() method
public void setBasePath(string $value)
$value string the root directory storing published asset files
Source Code: framework/web/CAssetManager.php#77 (show)
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() method
public void setBaseUrl(string $value)
$value string the base url that the published asset files can be accessed
Source Code: framework/web/CAssetManager.php#98 (show)
public function setBaseUrl($value)
{
    
$this->_baseUrl=rtrim($value,'/');
}