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