AssetBundle.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://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://www.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. * @phpstan-import-type RegisterJsFileOptions from View
  28. * @psalm-import-type RegisterJsFileOptions from View
  29. *
  30. * @phpstan-import-type RegisterCssFileOptions from View
  31. * @psalm-import-type RegisterCssFileOptions from View
  32. *
  33. * @phpstan-import-type PublishOptions from AssetManager
  34. * @psalm-import-type PublishOptions from AssetManager
  35. */
  36. class AssetBundle extends BaseObject
  37. {
  38. /**
  39. * @var string|null the directory that contains the source asset files for this asset bundle.
  40. * A source asset file is a file that is part of your source code repository of your Web application.
  41. *
  42. * You must set this property if the directory containing the source asset files is not Web accessible.
  43. * By setting this property, [[AssetManager]] will publish the source asset files
  44. * to a Web-accessible directory automatically when the asset bundle is registered on a page.
  45. *
  46. * If you do not set this property, it means the source asset files are located under [[basePath]].
  47. *
  48. * You can use either a directory or an alias of the directory.
  49. * @see publishOptions
  50. */
  51. public $sourcePath;
  52. /**
  53. * @var string the Web-accessible directory that contains the asset files in this bundle.
  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 directory or an alias of the directory.
  59. */
  60. public $basePath;
  61. /**
  62. * @var string the base URL for the relative asset files listed in [[js]] and [[css]].
  63. *
  64. * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
  65. * when it publishes the asset files from [[sourcePath]].
  66. *
  67. * You can use either a URL or an alias of the URL.
  68. */
  69. public $baseUrl;
  70. /**
  71. * @var array list of bundle class names that this bundle depends on.
  72. *
  73. * For example:
  74. *
  75. * ```php
  76. * public $depends = [
  77. * 'yii\web\YiiAsset',
  78. * 'yii\bootstrap\BootstrapAsset',
  79. * ];
  80. * ```
  81. *
  82. * @phpstan-var class-string[]
  83. * @psalm-var class-string[]
  84. */
  85. public $depends = [];
  86. /**
  87. * @var array list of JavaScript files that this bundle contains. Each JavaScript file can be
  88. * specified in one of the following formats:
  89. *
  90. * - an absolute URL representing an external asset. For example,
  91. * `https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or
  92. * `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`.
  93. * - a relative path representing a local asset (e.g. `js/main.js`). The actual file path of a local
  94. * asset can be determined by prefixing [[basePath]] to the relative path, and the actual URL
  95. * of the asset can be determined by prefixing [[baseUrl]] to the relative path.
  96. * - an array, with the first entry being the URL or relative path as described before, and a list of key => value pairs
  97. * that will be used to overwrite [[jsOptions]] settings for this entry.
  98. * This functionality is available since version 2.0.7.
  99. *
  100. * Note that only a forward slash "/" should be used as directory separator.
  101. *
  102. * @phpstan-var (string|array<int|string, mixed>)[]
  103. * @psalm-var (string|array<int|string, mixed>)[]
  104. */
  105. public $js = [];
  106. /**
  107. * @var array list of CSS files that this bundle contains. Each CSS file can be specified
  108. * in one of the three formats as explained in [[js]].
  109. *
  110. * Note that only a forward slash "/" should be used as directory separator.
  111. *
  112. * @phpstan-var (string|array<int|string, mixed>)[]
  113. * @psalm-var (string|array<int|string, mixed>)[]
  114. */
  115. public $css = [];
  116. /**
  117. * @var array the options that will be passed to [[View::registerJsFile()]]
  118. * when registering the JS files in this bundle.
  119. *
  120. * @phpstan-var RegisterJsFileOptions
  121. * @psalm-var RegisterJsFileOptions
  122. */
  123. public $jsOptions = [];
  124. /**
  125. * @var array the options that will be passed to [[View::registerCssFile()]]
  126. * when registering the CSS files in this bundle.
  127. *
  128. * @phpstan-var RegisterCssFileOptions
  129. * @psalm-var RegisterCssFileOptions
  130. */
  131. public $cssOptions = [];
  132. /**
  133. * @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
  134. * is being published. This property is used only when [[sourcePath]] is set.
  135. *
  136. * @phpstan-var PublishOptions
  137. * @psalm-var PublishOptions
  138. */
  139. public $publishOptions = [];
  140. /**
  141. * Registers this asset bundle with a view.
  142. * @param View $view the view to be registered with
  143. * @return static the registered asset bundle instance
  144. */
  145. public static function register($view)
  146. {
  147. return $view->registerAssetBundle(get_called_class());
  148. }
  149. /**
  150. * Initializes the bundle.
  151. * If you override this method, make sure you call the parent implementation in the last.
  152. */
  153. public function init()
  154. {
  155. if ($this->sourcePath !== null) {
  156. $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
  157. }
  158. if ($this->basePath !== null) {
  159. $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
  160. }
  161. if ($this->baseUrl !== null) {
  162. $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
  163. }
  164. }
  165. /**
  166. * Registers the CSS and JS files with the given view.
  167. * @param \yii\web\View $view the view that the asset files are to be registered with.
  168. */
  169. public function registerAssetFiles($view)
  170. {
  171. $manager = $view->getAssetManager();
  172. foreach ($this->js as $js) {
  173. if (is_array($js)) {
  174. $file = array_shift($js);
  175. $options = ArrayHelper::merge($this->jsOptions, $js);
  176. $view->registerJsFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
  177. } elseif ($js !== null) {
  178. $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
  179. }
  180. }
  181. foreach ($this->css as $css) {
  182. if (is_array($css)) {
  183. $file = array_shift($css);
  184. $options = ArrayHelper::merge($this->cssOptions, $css);
  185. $view->registerCssFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
  186. } elseif ($css !== null) {
  187. $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
  188. }
  189. }
  190. }
  191. /**
  192. * Publishes the asset bundle if its source code is not under Web-accessible directory.
  193. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
  194. * CSS or JS files using [[AssetManager::converter|asset converter]].
  195. * @param AssetManager $am the asset manager to perform the asset publishing
  196. */
  197. public function publish($am)
  198. {
  199. if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
  200. list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
  201. }
  202. if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
  203. foreach ($this->js as $i => $js) {
  204. if (is_array($js)) {
  205. $file = array_shift($js);
  206. if (Url::isRelative($file)) {
  207. $js = ArrayHelper::merge($this->jsOptions, $js);
  208. array_unshift($js, $converter->convert($file, $this->basePath));
  209. $this->js[$i] = $js;
  210. }
  211. } elseif (Url::isRelative($js)) {
  212. $this->js[$i] = $converter->convert($js, $this->basePath);
  213. }
  214. }
  215. foreach ($this->css as $i => $css) {
  216. if (is_array($css)) {
  217. $file = array_shift($css);
  218. if (Url::isRelative($file)) {
  219. $css = ArrayHelper::merge($this->cssOptions, $css);
  220. array_unshift($css, $converter->convert($file, $this->basePath));
  221. $this->css[$i] = $css;
  222. }
  223. } elseif (Url::isRelative($css)) {
  224. $this->css[$i] = $converter->convert($css, $this->basePath);
  225. }
  226. }
  227. }
  228. }
  229. }