Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
11 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
12 years ago
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
12 years ago
namespace yii\web;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\FileHelper;
13 years ago
/**
* AssetManager manages asset bundles and asset publishing.
13 years ago
*
* @property AssetConverterInterface $converter The asset converter. Note that the type of this property differs in
* getter and setter. See [[getConverter()]] and [[setConverter()]] for details.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
12 years ago
* @since 2.0
13 years ago
*/
12 years ago
class AssetManager extends Component
13 years ago
{
/**
* @var array list of available asset bundles. The keys are the class names of the asset bundles,
* and the values are either the configuration arrays for creating the [[AssetBundle]] objects
* or the corresponding asset bundle instances.
12 years ago
*/
public $bundles = array();
12 years ago
/**
* @return string the root directory storing the published asset files.
*/
public $basePath = '@webroot/assets';
12 years ago
/**
* @return string the base URL through which the published asset files can be accessed.
13 years ago
*/
public $baseUrl = '@web/assets';
13 years ago
/**
* @var boolean whether to use symbolic link to publish asset files. Defaults to false, meaning
12 years ago
* asset files are copied to [[basePath]]. Using symbolic links has the benefit that the published
* assets will always be consistent with the source assets and there is no copy operation required.
* This is especially useful during development.
13 years ago
*
* 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.
*
* 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:
*
12 years ago
* ~~~
* Options FollowSymLinks
* ~~~
13 years ago
*/
12 years ago
public $linkAssets = false;
13 years ago
/**
* @var integer the permission to be set for newly published asset files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
13 years ago
*/
public $fileMode;
13 years ago
/**
* @var integer the permission to be set for newly generated asset directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
13 years ago
*/
public $dirMode = 0775;
13 years ago
/**
* Initializes the component.
* @throws InvalidConfigException if [[basePath]] is invalid
13 years ago
*/
12 years ago
public function init()
13 years ago
{
12 years ago
parent::init();
$this->basePath = Yii::getAlias($this->basePath);
if (!is_dir($this->basePath)) {
throw new InvalidConfigException("The directory does not exist: {$this->basePath}");
} elseif (!is_writable($this->basePath)) {
throw new InvalidConfigException("The directory is not writable by the Web process: {$this->basePath}");
} else {
12 years ago
$this->basePath = realpath($this->basePath);
13 years ago
}
12 years ago
$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
13 years ago
}
/**
* Returns the named asset bundle.
12 years ago
*
* This method will first look for the bundle in [[bundles]]. If not found,
* it will treat `$name` as the class of the asset bundle and create a new instance of it.
12 years ago
*
* @param string $name the class name of the asset bundle
* @return AssetBundle the asset bundle instance
* @throws InvalidConfigException if $name does not refer to a valid asset bundle
13 years ago
*/
12 years ago
public function getBundle($name)
13 years ago
{
if (isset($this->bundles[$name])) {
if (is_array($this->bundles[$name])) {
$this->bundles[$name] = Yii::createObject(array_merge(array('class' => $name), $this->bundles[$name]));
} elseif (!$this->bundles[$name] instanceof AssetBundle) {
throw new InvalidConfigException("Invalid asset bundle: $name");
12 years ago
}
} else {
$this->bundles[$name] = Yii::createObject($name);
12 years ago
}
return $this->bundles[$name];
13 years ago
}
private $_converter;
13 years ago
/**
* Returns the asset converter.
* @return AssetConverterInterface the asset converter.
12 years ago
*/
public function getConverter()
12 years ago
{
if ($this->_converter === null) {
$this->_converter = Yii::createObject(AssetConverter::className());
} elseif (is_array($this->_converter) || is_string($this->_converter)) {
$this->_converter = Yii::createObject($this->_converter);
}
return $this->_converter;
}
/**
* Sets the asset converter.
* @param array|AssetConverterInterface $value the asset converter. This can be either
* an object implementing the [[AssetConverterInterface]], or a configuration
* array that can be used to create the asset converter object.
*/
public function setConverter($value)
{
$this->_converter = $value;
12 years ago
}
/**
* @var array published assets
*/
private $_published = array();
/**
13 years ago
* Publishes a file or a directory.
*
* This method will copy the specified file or directory to [[basePath]] so that
* it can be accessed via the Web server.
*
* 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 (which is very expensive).
13 years ago
*
12 years ago
* By default, when publishing a directory, subdirectories and files whose name starts with a dot "."
* will NOT be published. If you want to change this behavior, you may specify the "beforeCopy" option
* as explained in the `$options` parameter.
*
13 years ago
* 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
*
* @param string $path the asset (file or directory) to be published
12 years ago
* @param array $options the options to be applied when publishing a directory.
* The following options are supported:
*
* - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file.
* This option is used only when publishing a directory. If the callback returns false, the copy
* operation for the sub-directory or file will be cancelled.
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
* file to be copied from, while `$to` is the copy target.
* - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied.
* This option is used only when publishing a directory. The signature of the callback is similar to that
* of `beforeCopy`.
* - forceCopy: boolean, whether the directory being published should be copied even if
* it is found in the target directory. This option is used only when publishing a directory.
* You may want to set this to be true during the development stage to make sure the published
* directory is always up-to-date. Do not set this to true on production servers as it will
* significantly degrade the performance.
12 years ago
* @return array the path (directory or file path) and the URL that the asset is published as.
* @throws InvalidParamException if the asset to be published does not exist.
13 years ago
*/
12 years ago
public function publish($path, $options = array())
13 years ago
{
12 years ago
if (isset($this->_published[$path])) {
13 years ago
return $this->_published[$path];
12 years ago
}
13 years ago
12 years ago
$src = realpath($path);
if ($src === false) {
throw new InvalidParamException("The file or directory to be published does not exist: $path");
}
if (is_file($src)) {
$dir = $this->hash(dirname($src) . filemtime($src));
12 years ago
$fileName = basename($src);
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
$dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName;
13 years ago
12 years ago
if (!is_dir($dstDir)) {
FileHelper::createDirectory($dstDir, $this->dirMode, true);
12 years ago
}
13 years ago
12 years ago
if ($this->linkAssets) {
if (!is_file($dstFile)) {
symlink($src, $dstFile);
}
12 years ago
} elseif (@filemtime($dstFile) < @filemtime($src)) {
12 years ago
copy($src, $dstFile);
if ($this->fileMode !== null) {
@chmod($dstFile, $this->fileMode);
}
12 years ago
}
12 years ago
return $this->_published[$path] = array($dstFile, $this->baseUrl . "/$dir/$fileName");
12 years ago
} else {
$dir = $this->hash($src . filemtime($src));
12 years ago
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
if ($this->linkAssets) {
if (!is_dir($dstDir)) {
symlink($src, $dstDir);
}
12 years ago
} elseif (!is_dir($dstDir) || !empty($options['forceCopy'])) {
$opts = array(
'dirMode' => $this->dirMode,
'fileMode' => $this->fileMode,
12 years ago
);
if (isset($options['beforeCopy'])) {
$opts['beforeCopy'] = $options['beforeCopy'];
12 years ago
} else {
$opts['beforeCopy'] = function ($from, $to) {
return strncmp(basename($from), '.', 1) !== 0;
};
12 years ago
}
if (isset($options['afterCopy'])) {
$opts['afterCopy'] = $options['afterCopy'];
}
FileHelper::copyDirectory($src, $dstDir, $opts);
13 years ago
}
12 years ago
12 years ago
return $this->_published[$path] = array($dstDir, $this->baseUrl . '/' . $dir);
13 years ago
}
}
/**
* 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.
* @param string $path directory or file path being published
* @return string the published file path. False if the file or directory does not exist
*/
public function getPublishedPath($path)
13 years ago
{
12 years ago
if (isset($this->_published[$path])) {
return $this->_published[$path][0];
}
12 years ago
if (($path = realpath($path)) !== false) {
12 years ago
$base = $this->basePath . DIRECTORY_SEPARATOR;
12 years ago
if (is_file($path)) {
return $base . $this->hash(dirname($path) . filemtime($path)) . DIRECTORY_SEPARATOR . basename($path);
12 years ago
} else {
return $base . $this->hash($path . filemtime($path));
12 years ago
}
} else {
13 years ago
return false;
12 years ago
}
13 years ago
}
/**
* 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.
* @param string $path directory or file path being published
* @return string the published URL for the file or directory. False if the file or directory does not exist.
*/
public function getPublishedUrl($path)
13 years ago
{
12 years ago
if (isset($this->_published[$path])) {
12 years ago
return $this->_published[$path][1];
13 years ago
}
12 years ago
if (($path = realpath($path)) !== false) {
if (is_file($path)) {
return $this->baseUrl . '/' . $this->hash(dirname($path) . filemtime($path)) . '/' . basename($path);
12 years ago
} else {
return $this->baseUrl . '/' . $this->hash($path . filemtime($path));
12 years ago
}
} else {
13 years ago
return false;
12 years ago
}
13 years ago
}
/**
* Generate a CRC32 hash for the directory path. Collisions are higher
* than MD5 but generates a much smaller hash string.
* @param string $path string to be hashed.
* @return string hashed string.
*/
protected function hash($path)
{
12 years ago
return sprintf('%x', crc32($path . Yii::getVersion()));
13 years ago
}
}