AssetBundle.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Object;
  10. /**
  11. * AssetBundle represents a collection of asset files, such as CSS, JS, images.
  12. *
  13. * Each asset bundle has a unique name that globally identifies it among all asset bundles used in an application.
  14. * The name is the [fully qualified class name](http://php.net/manual/en/language.namespaces.rules.php)
  15. * of the class representing it.
  16. *
  17. * An asset bundle can depend on other asset bundles. When registering an asset bundle
  18. * with a view, all its dependent asset bundles will be automatically registered.
  19. *
  20. * @author Qiang Xue <[email protected]>
  21. * @since 2.0
  22. */
  23. class AssetBundle extends Object
  24. {
  25. /**
  26. * @var string the root directory of the source asset files. A source asset file
  27. * is a file that is part of your source code repository of your Web application.
  28. *
  29. * You must set this property if the directory containing the source asset files
  30. * is not Web accessible (this is usually the case for extensions).
  31. *
  32. * By setting this property, the asset manager will publish the source asset files
  33. * to a Web-accessible directory [[basePath]].
  34. *
  35. * You can use either a directory or an alias of the directory.
  36. */
  37. public $sourcePath;
  38. /**
  39. * @var string the Web-accessible directory that contains the asset files in this bundle.
  40. *
  41. * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
  42. * when it publishes the asset files from [[sourcePath]].
  43. *
  44. * If the bundle contains any assets that are specified in terms of relative file path,
  45. * then this property must be set either manually or automatically (by [[AssetManager]] via
  46. * asset publishing).
  47. *
  48. * You can use either a directory or an alias of the directory.
  49. */
  50. public $basePath;
  51. /**
  52. * @var string the base URL that will be prefixed to the asset files for them to
  53. * be accessed via Web server.
  54. *
  55. * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
  56. * when it publishes the asset files from [[sourcePath]].
  57. *
  58. * If the bundle contains any assets that are specified in terms of relative file path,
  59. * then this property must be set either manually or automatically (by asset manager via
  60. * asset publishing).
  61. *
  62. * You can use either a URL or an alias of the URL.
  63. */
  64. public $baseUrl;
  65. /**
  66. * @var array list of bundle class names that this bundle depends on.
  67. *
  68. * For example:
  69. *
  70. * ```php
  71. * public $depends = [
  72. * 'yii\web\YiiAsset',
  73. * 'yii\bootstrap\BootstrapAsset',
  74. * ];
  75. * ```
  76. */
  77. public $depends = [];
  78. /**
  79. * @var array list of JavaScript files that this bundle contains. Each JavaScript file can
  80. * be either a file path (without leading slash) relative to [[basePath]] or a URL representing
  81. * an external JavaScript file.
  82. *
  83. * Note that only forward slash "/" can be used as directory separator.
  84. */
  85. public $js = [];
  86. /**
  87. * @var array list of CSS files that this bundle contains. Each CSS file can
  88. * be either a file path (without leading slash) relative to [[basePath]] or a URL representing
  89. * an external CSS file.
  90. *
  91. * Note that only forward slash "/" can be used as directory separator.
  92. */
  93. public $css = [];
  94. /**
  95. * @var array the options that will be passed to [[\yii\web\View::registerJsFile()]]
  96. * when registering the JS files in this bundle.
  97. */
  98. public $jsOptions = [];
  99. /**
  100. * @var array the options that will be passed to [[\yii\web\View::registerCssFile()]]
  101. * when registering the CSS files in this bundle.
  102. */
  103. public $cssOptions = [];
  104. /**
  105. * @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
  106. * is being published.
  107. */
  108. public $publishOptions = [];
  109. /**
  110. * @param View $view
  111. * @return AssetBundle the registered asset bundle instance
  112. */
  113. public static function register($view)
  114. {
  115. return $view->registerAssetBundle(get_called_class());
  116. }
  117. /**
  118. * Initializes the bundle.
  119. * If you override this method, make sure you call the parent implementation in the last.
  120. */
  121. public function init()
  122. {
  123. if ($this->sourcePath !== null) {
  124. $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
  125. }
  126. if ($this->basePath !== null) {
  127. $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
  128. }
  129. if ($this->baseUrl !== null) {
  130. $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
  131. }
  132. }
  133. /**
  134. * Registers the CSS and JS files with the given view.
  135. * @param \yii\web\View $view the view that the asset files are to be registered with.
  136. */
  137. public function registerAssetFiles($view)
  138. {
  139. foreach ($this->js as $js) {
  140. if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
  141. $view->registerJsFile($this->baseUrl . '/' . $js, [], $this->jsOptions);
  142. } else {
  143. $view->registerJsFile($js, [], $this->jsOptions);
  144. }
  145. }
  146. foreach ($this->css as $css) {
  147. if (strpos($css, '/') !== 0 && strpos($css, '://') === false) {
  148. $view->registerCssFile($this->baseUrl . '/' . $css, [], $this->cssOptions);
  149. } else {
  150. $view->registerCssFile($css, [], $this->cssOptions);
  151. }
  152. }
  153. }
  154. /**
  155. * Publishes the asset bundle if its source code is not under Web-accessible directory.
  156. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
  157. * CSS or JS files using [[AssetManager::converter|asset converter]].
  158. * @param AssetManager $am the asset manager to perform the asset publishing
  159. */
  160. public function publish($am)
  161. {
  162. if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
  163. list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
  164. }
  165. $converter = $am->getConverter();
  166. foreach ($this->js as $i => $js) {
  167. if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
  168. if (isset($this->basePath, $this->baseUrl)) {
  169. $this->js[$i] = $converter->convert($js, $this->basePath, $this->baseUrl);
  170. } else {
  171. $this->js[$i] = '/' . $js;
  172. }
  173. }
  174. }
  175. foreach ($this->css as $i => $css) {
  176. if (strpos($css, '/') !== 0 && strpos($css, '://') === false) {
  177. if (isset($this->basePath, $this->baseUrl)) {
  178. $this->css[$i] = $converter->convert($css, $this->basePath, $this->baseUrl);
  179. } else {
  180. $this->css[$i] = '/' . $css;
  181. }
  182. }
  183. }
  184. }
  185. }