AssetBundle.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\BaseObject;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Url;
  12. /**
  13. * AssetBundle represents a collection of asset files, such as CSS, JS, images.
  14. *
  15. * Each asset bundle has a unique name that globally identifies it among all asset bundles used in an application.
  16. * The name is the [fully qualified class name](https://secure.php.net/manual/en/language.namespaces.rules.php)
  17. * of the class representing it.
  18. *
  19. * An asset bundle can depend on other asset bundles. When registering an asset bundle
  20. * with a view, all its dependent asset bundles will be automatically registered.
  21. *
  22. * For more details and usage information on AssetBundle, see the [guide article on assets](guide:structure-assets).
  23. *
  24. * @author Qiang Xue <qiang.xue@gmail.com>
  25. * @since 2.0
  26. */
  27. class AssetBundle extends BaseObject
  28. {
  29. /**
  30. * @var string the directory that contains the source asset files for this asset bundle.
  31. * A source asset file is a file that is part of your source code repository of your Web application.
  32. *
  33. * You must set this property if the directory containing the source asset files is not Web accessible.
  34. * By setting this property, [[AssetManager]] will publish the source asset files
  35. * to a Web-accessible directory automatically when the asset bundle is registered on a page.
  36. *
  37. * If you do not set this property, it means the source asset files are located under [[basePath]].
  38. *
  39. * You can use either a directory or an alias of the directory.
  40. * @see $publishOptions
  41. */
  42. public $sourcePath;
  43. /**
  44. * @var string the Web-accessible directory that contains the asset files in this bundle.
  45. *
  46. * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
  47. * when it publishes the asset files from [[sourcePath]].
  48. *
  49. * You can use either a directory or an alias of the directory.
  50. */
  51. public $basePath;
  52. /**
  53. * @var string the base URL for the relative asset files listed in [[js]] and [[css]].
  54. *
  55. * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
  56. * when it publishes the asset files from [[sourcePath]].
  57. *
  58. * You can use either a URL or an alias of the URL.
  59. */
  60. public $baseUrl;
  61. /**
  62. * @var array list of bundle class names that this bundle depends on.
  63. *
  64. * For example:
  65. *
  66. * ```php
  67. * public $depends = [
  68. * 'yii\web\YiiAsset',
  69. * 'yii\bootstrap\BootstrapAsset',
  70. * ];
  71. * ```
  72. */
  73. public $depends = [];
  74. /**
  75. * @var array list of JavaScript files that this bundle contains. Each JavaScript file can be
  76. * specified in one of the following formats:
  77. *
  78. * - an absolute URL representing an external asset. For example,
  79. * `http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or
  80. * `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`.
  81. * - a relative path representing a local asset (e.g. `js/main.js`). The actual file path of a local
  82. * asset can be determined by prefixing [[basePath]] to the relative path, and the actual URL
  83. * of the asset can be determined by prefixing [[baseUrl]] to the relative path.
  84. * - an array, with the first entry being the URL or relative path as described before, and a list of key => value pairs
  85. * that will be used to overwrite [[jsOptions]] settings for this entry.
  86. * This functionality is available since version 2.0.7.
  87. *
  88. * Note that only a forward slash "/" should be used as directory separator.
  89. */
  90. public $js = [];
  91. /**
  92. * @var array list of CSS files that this bundle contains. Each CSS file can be specified
  93. * in one of the three formats as explained in [[js]].
  94. *
  95. * Note that only a forward slash "/" should be used as directory separator.
  96. */
  97. public $css = [];
  98. /**
  99. * @var array the options that will be passed to [[View::registerJsFile()]]
  100. * when registering the JS files in this bundle.
  101. */
  102. public $jsOptions = [];
  103. /**
  104. * @var array the options that will be passed to [[View::registerCssFile()]]
  105. * when registering the CSS files in this bundle.
  106. */
  107. public $cssOptions = [];
  108. /**
  109. * @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
  110. * is being published. This property is used only when [[sourcePath]] is set.
  111. */
  112. public $publishOptions = [];
  113. /**
  114. * Registers this asset bundle with a view.
  115. * @param View $view the view to be registered with
  116. * @return static the registered asset bundle instance
  117. */
  118. public static function register($view)
  119. {
  120. return $view->registerAssetBundle(get_called_class());
  121. }
  122. /**
  123. * Initializes the bundle.
  124. * If you override this method, make sure you call the parent implementation in the last.
  125. */
  126. public function init()
  127. {
  128. if ($this->sourcePath !== null) {
  129. $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
  130. }
  131. if ($this->basePath !== null) {
  132. $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
  133. }
  134. if ($this->baseUrl !== null) {
  135. $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
  136. }
  137. }
  138. /**
  139. * Registers the CSS and JS files with the given view.
  140. * @param \yii\web\View $view the view that the asset files are to be registered with.
  141. */
  142. public function registerAssetFiles($view)
  143. {
  144. $manager = $view->getAssetManager();
  145. foreach ($this->js as $js) {
  146. if (is_array($js)) {
  147. $file = array_shift($js);
  148. $options = ArrayHelper::merge($this->jsOptions, $js);
  149. $view->registerJsFile($manager->getAssetUrl($this, $file), $options);
  150. } else {
  151. if ($js !== null) {
  152. $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
  153. }
  154. }
  155. }
  156. foreach ($this->css as $css) {
  157. if (is_array($css)) {
  158. $file = array_shift($css);
  159. $options = ArrayHelper::merge($this->cssOptions, $css);
  160. $view->registerCssFile($manager->getAssetUrl($this, $file), $options);
  161. } else {
  162. if ($css !== null) {
  163. $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Publishes the asset bundle if its source code is not under Web-accessible directory.
  170. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
  171. * CSS or JS files using [[AssetManager::converter|asset converter]].
  172. * @param AssetManager $am the asset manager to perform the asset publishing
  173. */
  174. public function publish($am)
  175. {
  176. if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
  177. list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
  178. }
  179. if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
  180. foreach ($this->js as $i => $js) {
  181. if (is_array($js)) {
  182. $file = array_shift($js);
  183. if (Url::isRelative($file)) {
  184. $js = ArrayHelper::merge($this->jsOptions, $js);
  185. array_unshift($js, $converter->convert($file, $this->basePath));
  186. $this->js[$i] = $js;
  187. }
  188. } elseif (Url::isRelative($js)) {
  189. $this->js[$i] = $converter->convert($js, $this->basePath);
  190. }
  191. }
  192. foreach ($this->css as $i => $css) {
  193. if (is_array($css)) {
  194. $file = array_shift($css);
  195. if (Url::isRelative($file)) {
  196. $css = ArrayHelper::merge($this->cssOptions, $css);
  197. array_unshift($css, $converter->convert($file, $this->basePath));
  198. $this->css[$i] = $css;
  199. }
  200. } elseif (Url::isRelative($css)) {
  201. $this->css[$i] = $converter->convert($css, $this->basePath);
  202. }
  203. }
  204. }
  205. }
  206. }