Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

214 lines
8.3KB

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