Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

179 lines
6.6KB

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