CAssetManager
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.
Protected Methods
Hide inherited methods
| Method | Description | Defined By |
| hash() |
Generate a CRC32 hash for the directory path. Collisions are higher |
CAssetManager |
Property Details
the root directory storing the published asset files
the base url that the published asset files can be accessed.
Note, the ending slashes are stripped off.
Method Details
|
public string getBasePath()
|
| {return} |
string |
the root directory storing the published asset files |
|
public string getBaseUrl()
|
| {return} |
string |
the base url that the published asset files can be accessed.
Note, the ending slashes are stripped off. |
|
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 |
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 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. |
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. |
Generate a CRC32 hash for the directory path. Collisions are higher
than MD5 but generates a much smaller hash string.
Initializes the application component.
This method is required by IApplicationComponent and is invoked by application.
|
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 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. |
| $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 |
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.
|
public void setBasePath(string $value)
|
| $value |
string |
the root directory storing published asset files |
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 |