AssetBundle.php 4.87 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 13 14 15 16 17 18 19
use yii\base\Object;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AssetBundle extends Object
{
Qiang Xue committed
20
	/**
Qiang Xue committed
21 22 23 24 25 26 27 28 29 30
	 * @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
31 32 33
	 */
	public $sourcePath;
	/**
Qiang Xue committed
34 35 36 37 38 39 40 41 42 43
	 * @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,
	 * then this property must be set either manually or automatically (by asset manager via
	 * asset publishing).
	 *
	 * You can use either a directory or an alias of the directory.
Qiang Xue committed
44
	 */
Qiang Xue committed
45
	public $basePath;
Qiang Xue committed
46
	/**
Qiang Xue committed
47 48 49 50 51 52 53 54 55 56 57
	 * @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
58 59 60 61
	 */
	public $baseUrl;
	/**
	 * @var array list of JavaScript files that this bundle contains. Each JavaScript file can
Qiang Xue committed
62 63
	 * be either a file path (without leading slash) relative to [[basePath]] or a URL representing
	 * an external JavaScript file.
Qiang Xue committed
64
	 *
Qiang Xue committed
65
	 * Note that only forward slash "/" can be used as directory separator.
Qiang Xue committed
66
	 *
67 68 69
	 * Each JavaScript file may be associated with options. In this case, the array key
	 * should be the JavaScript file path, while the corresponding array value should
	 * be the option array. The options will be passed to [[ViewContent::registerJsFile()]].
Qiang Xue committed
70
	 */
Qiang Xue committed
71
	public $js = array();
72 73
	/**
	 * @var array list of CSS files that this bundle contains. Each CSS file can
Qiang Xue committed
74 75
	 * be either a file path (without leading slash) relative to [[basePath]] or a URL representing
	 * an external CSS file.
76
	 *
Qiang Xue committed
77
	 * Note that only forward slash "/" can be used as directory separator.
78 79 80 81 82
	 *
	 * Each CSS file may be associated with options. In this case, the array key
	 * should be the CSS file path, while the corresponding array value should
	 * be the option array. The options will be passed to [[ViewContent::registerCssFile()]].
	 */
Qiang Xue committed
83
	public $css = array();
Qiang Xue committed
84 85 86
	/**
	 * @var array list of the bundle names that this bundle depends on
	 */
Qiang Xue committed
87 88
	public $depends = array();

Qiang Xue committed
89 90 91
	/**
	 * Initializes the bundle.
	 */
Qiang Xue committed
92
	public function init()
Qiang Xue committed
93
	{
Qiang Xue committed
94 95
		if ($this->sourcePath !== null) {
			$this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
Qiang Xue committed
96
		}
Qiang Xue committed
97 98 99 100 101 102
		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
103 104
	}

Qiang Xue committed
105
	/**
Qiang Xue committed
106 107 108
	 * @param \yii\base\ViewContent $page
	 * @param AssetManager $am
	 * @throws InvalidConfigException
Qiang Xue committed
109
	 */
Qiang Xue committed
110
	public function registerAssets($page, $am)
Qiang Xue committed
111 112
	{
		foreach ($this->depends as $name) {
Qiang Xue committed
113 114 115 116 117
			$page->registerAssetBundle($name);
		}

		if ($this->sourcePath !== null) {
			list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath);
Qiang Xue committed
118
		}
Qiang Xue committed
119

Qiang Xue committed
120
		foreach ($this->js as $js => $options) {
Qiang Xue committed
121
			$js = is_string($options) ? $options : $js;
Qiang Xue committed
122 123
			if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
				if (isset($this->basePath, $this->baseUrl)) {
Qiang Xue committed
124
					$js = $am->processAsset(ltrim($js, '/'), $this->basePath, $this->baseUrl);
Qiang Xue committed
125 126 127
				} else {
					throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
				}
Qiang Xue committed
128
			}
Qiang Xue committed
129
			$page->registerJsFile($js, is_array($options) ? $options : array());
Qiang Xue committed
130 131
		}
		foreach ($this->css as $css => $options) {
Qiang Xue committed
132 133
			$css = is_string($options) ? $options : $css;
			if (strpos($css, '//') !== 0 && strpos($css, '://') === false) {
Qiang Xue committed
134
				if (isset($this->basePath, $this->baseUrl)) {
Qiang Xue committed
135
					$css = $am->processAsset(ltrim($css, '/'), $this->basePath, $this->baseUrl);
Qiang Xue committed
136 137 138
				} else {
					throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
				}
Qiang Xue committed
139
			}
Qiang Xue committed
140
			$page->registerCssFile($css, is_array($options) ? $options : array());
Qiang Xue committed
141 142 143
		}
	}
}