AssetBundle.php 6.33 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
Qiang Xue committed
12
use yii\base\Object;
Alexander Makarov committed
13
use yii\web\View;
Qiang Xue committed
14 15

/**
16 17
 * AssetBundle represents a collection of asset files, such as CSS, JS, images.
 *
18 19 20
 * Each asset bundle has a unique name that globally identifies it among all asset bundles used in an application.
 * The name is the [fully qualified class name](http://php.net/manual/en/language.namespaces.rules.php)
 * of the class representing it.
21 22 23 24
 *
 * An asset bundle can depend on other asset bundles. When registering an asset bundle
 * with a view, all its dependent asset bundles will be automatically registered.
 *
Qiang Xue committed
25 26 27 28 29
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AssetBundle extends Object
{
Qiang Xue committed
30
	/**
Qiang Xue committed
31 32 33 34 35 36 37 38 39 40
	 * @var string the root directory of the source asset files. A source asset file
	 * is a file that is part of your source code repository of your Web application.
	 *
	 * You must set this property if the directory containing the source asset files
	 * is not Web accessible (this is usually the case for extensions).
	 *
	 * By setting this property, the asset manager will publish the source asset files
	 * to a Web-accessible directory [[basePath]].
	 *
	 * You can use either a directory or an alias of the directory.
Qiang Xue committed
41 42 43
	 */
	public $sourcePath;
	/**
Qiang Xue committed
44 45 46 47 48 49
	 * @var string the Web-accessible directory that contains the asset files in this bundle.
	 *
	 * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
	 * when it publishes the asset files from [[sourcePath]].
	 *
	 * If the bundle contains any assets that are specified in terms of relative file path,
Qiang Xue committed
50
	 * then this property must be set either manually or automatically (by [[AssetManager]] via
Qiang Xue committed
51 52 53
	 * asset publishing).
	 *
	 * You can use either a directory or an alias of the directory.
Qiang Xue committed
54
	 */
Qiang Xue committed
55
	public $basePath;
Qiang Xue committed
56
	/**
Qiang Xue committed
57 58 59 60 61 62 63 64 65 66 67
	 * @var string the base URL that will be prefixed to the asset files for them to
	 * be accessed via Web server.
	 *
	 * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
	 * when it publishes the asset files from [[sourcePath]].
	 *
	 * If the bundle contains any assets that are specified in terms of relative file path,
	 * then this property must be set either manually or automatically (by asset manager via
	 * asset publishing).
	 *
	 * You can use either a URL or an alias of the URL.
Qiang Xue committed
68 69
	 */
	public $baseUrl;
Qiang Xue committed
70
	/**
Alexander Makarov committed
71 72 73 74 75 76 77 78 79 80
	 * @var array list of bundle class names that this bundle depends on.
	 *
	 * For example:
	 *
	 * ```php
	 * public $depends = [
	 *    'yii\web\YiiAsset',
	 *    'yii\bootstrap\BootstrapAsset',
	 * ];
	 * ```
Qiang Xue committed
81
	 */
Alexander Makarov committed
82
	public $depends = [];
Qiang Xue committed
83 84
	/**
	 * @var array list of JavaScript files that this bundle contains. Each JavaScript file can
Qiang Xue committed
85 86
	 * be either a file path (without leading slash) relative to [[basePath]] or a URL representing
	 * an external JavaScript file.
Qiang Xue committed
87
	 *
Qiang Xue committed
88
	 * Note that only forward slash "/" can be used as directory separator.
Qiang Xue committed
89
	 */
Alexander Makarov committed
90
	public $js = [];
91 92
	/**
	 * @var array list of CSS files that this bundle contains. Each CSS file can
Qiang Xue committed
93 94
	 * be either a file path (without leading slash) relative to [[basePath]] or a URL representing
	 * an external CSS file.
95
	 *
Qiang Xue committed
96
	 * Note that only forward slash "/" can be used as directory separator.
97
	 */
Alexander Makarov committed
98
	public $css = [];
Qiang Xue committed
99
	/**
Alexander Makarov committed
100
	 * @var array the options that will be passed to [[\yii\web\View::registerJsFile()]]
Qiang Xue committed
101
	 * when registering the JS files in this bundle.
Qiang Xue committed
102
	 */
Alexander Makarov committed
103
	public $jsOptions = [];
Qiang Xue committed
104
	/**
Alexander Makarov committed
105
	 * @var array the options that will be passed to [[\yii\web\View::registerCssFile()]]
Qiang Xue committed
106 107
	 * when registering the CSS files in this bundle.
	 */
Alexander Makarov committed
108
	public $cssOptions = [];
Qiang Xue committed
109 110 111 112
	/**
	 * @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
	 * is being published.
	 */
Alexander Makarov committed
113
	public $publishOptions = [];
Qiang Xue committed
114

115 116 117 118 119 120 121 122 123
	/**
	 * @param View $view
	 * @return AssetBundle the registered asset bundle instance
	 */
	public static function register($view)
	{
		return $view->registerAssetBundle(get_called_class());
	}

Qiang Xue committed
124 125
	/**
	 * Initializes the bundle.
Qiang Xue committed
126
	 * If you override this method, make sure you call the parent implementation in the last.
Qiang Xue committed
127
	 */
Qiang Xue committed
128
	public function init()
Qiang Xue committed
129
	{
Qiang Xue committed
130 131
		if ($this->sourcePath !== null) {
			$this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
Qiang Xue committed
132
		}
Qiang Xue committed
133 134 135 136 137 138
		if ($this->basePath !== null) {
			$this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
		}
		if ($this->baseUrl !== null) {
			$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
		}
Qiang Xue committed
139 140
	}

Qiang Xue committed
141
	/**
142
	 * Registers the CSS and JS files with the given view.
Alexander Makarov committed
143
	 * @param \yii\web\View $view the view that the asset files are to be registered with.
Qiang Xue committed
144
	 */
145
	public function registerAssetFiles($view)
Qiang Xue committed
146
	{
Qiang Xue committed
147
		foreach ($this->js as $js) {
148
			if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
Qiang Xue committed
149
				$view->registerJsFile($this->baseUrl . '/' . $js, [], $this->jsOptions);
150
			} else {
Qiang Xue committed
151
				$view->registerJsFile($js, [], $this->jsOptions);
Qiang Xue committed
152
			}
Qiang Xue committed
153 154
		}
		foreach ($this->css as $css) {
155
			if (strpos($css, '/') !== 0 && strpos($css, '://') === false) {
Qiang Xue committed
156
				$view->registerCssFile($this->baseUrl . '/' . $css, [], $this->cssOptions);
157
			} else {
Qiang Xue committed
158
				$view->registerCssFile($css, [], $this->cssOptions);
Qiang Xue committed
159
			}
Qiang Xue committed
160 161
		}
	}
Qiang Xue committed
162

Qiang Xue committed
163 164
	/**
	 * Publishes the asset bundle if its source code is not under Web-accessible directory.
165 166
	 * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
	 * CSS or JS files using [[AssetManager::converter|asset converter]].
Qiang Xue committed
167 168 169 170
	 * @param AssetManager $am the asset manager to perform the asset publishing
	 */
	public function publish($am)
	{
171
		if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
Qiang Xue committed
172 173 174 175
			list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
		}
		$converter = $am->getConverter();
		foreach ($this->js as $i => $js) {
Qiang Xue committed
176 177
			if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
				if (isset($this->basePath, $this->baseUrl)) {
Qiang Xue committed
178
					$this->js[$i] = $converter->convert($js, $this->basePath, $this->baseUrl);
Qiang Xue committed
179
				} else {
180
					$this->js[$i] = '/' . $js;
Qiang Xue committed
181
				}
Qiang Xue committed
182 183
			}
		}
Qiang Xue committed
184
		foreach ($this->css as $i => $css) {
185
			if (strpos($css, '/') !== 0 && strpos($css, '://') === false) {
Qiang Xue committed
186
				if (isset($this->basePath, $this->baseUrl)) {
Qiang Xue committed
187
					$this->css[$i] = $converter->convert($css, $this->basePath, $this->baseUrl);
Qiang Xue committed
188
				} else {
189
					$this->css[$i] = '/' . $css;
Qiang Xue committed
190
				}
Qiang Xue committed
191
			}
Qiang Xue committed
192 193
		}
	}
Zander Baldwin committed
194
}