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.

119 lines
3.3 KiB

12 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
12 years ago
use Yii;
12 years ago
use yii\base\Object;
/**
* Each asset bundle should be declared with the following structure:
*
* ~~~
* array(
* 'basePath' => '...',
* 'baseUrl' => '...', // if missing, the bundle will be published to the "www/assets" folder
* 'js' => array(
* 'js/main.js',
* 'js/menu.js',
* 'js/base.js' => self::POS_HEAD,
* 'css' => array(
* 'css/main.css',
* 'css/menu.css',
* ),
* 'depends' => array(
* 'jquery',
* 'yii',
* 'yii/treeview',
* ),
* )
* ~~~
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AssetBundle extends Object
{
12 years ago
/**
* @var string the root directory of the asset files. If this is not set,
* the assets are considered to be located under a Web-accessible folder already
* and no asset publishing will be performed.
*/
12 years ago
public $basePath;
12 years ago
/**
* @var string the base URL that will be prefixed to the asset files.
* This property must be set if [[basePath]] is not set.
* When this property is not set, it will be initialized as the base URL
* that the assets are published to.
*/
public $baseUrl;
/**
* @var array list of JavaScript files that this bundle contains. Each JavaScript file can
* be specified in one of the three formats:
*
* - a relative path: a path relative to [[basePath]] if [[basePath]] is set,
* or a URL relative to [[baseUrl]] if [[basePath]] is not set;
* - an absolute URL;
* - a path alias that can be resolved into a relative path or an absolute URL.
*
* Note that you should not use backward slashes "\" to specify JavaScript files.
*
* A JavaScript file can be associated with the options: // todo
*/
12 years ago
public $js = array();
public $css = array();
12 years ago
/**
* @var array list of the bundle names that this bundle depends on
*/
12 years ago
public $depends = array();
12 years ago
public function init()
12 years ago
{
12 years ago
if ($this->baseUrl !== null) {
$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
}
if ($this->basePath !== null) {
$this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
}
12 years ago
}
12 years ago
/**
* @param \yii\base\ViewContent $content
*/
12 years ago
public function registerAssets($content)
12 years ago
{
foreach ($this->depends as $name) {
$content->registerAssetBundle($name);
}
foreach ($this->js as $js => $options) {
12 years ago
$js = is_string($options) ? $options : $js;
if (strpos($js, '//') !== 0 && strpos($js, '://') === false) {
$js = $this->baseUrl . '/' . ltrim($js, '/');
12 years ago
}
12 years ago
$content->registerJsFile($js, is_array($options) ? $options : array());
12 years ago
}
foreach ($this->css as $css => $options) {
12 years ago
$css = is_string($options) ? $options : $css;
if (strpos($css, '//') !== 0 && strpos($css, '://') === false) {
$css = $this->baseUrl . '/' . ltrim($css, '/');
12 years ago
}
12 years ago
$content->registerCssFile($css, is_array($options) ? $options : array());
12 years ago
}
}
12 years ago
/**
* @param \yii\web\AssetManager $assetManager
*/
public function publish($assetManager)
{
12 years ago
if ($this->basePath !== null) {
$baseUrl = $assetManager->publish($this->basePath);
if ($this->baseUrl === null) {
$this->baseUrl = $baseUrl;
}
12 years ago
}
}
12 years ago
}