1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Assets; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Yiisoft\Aliases\Aliases; |
9
|
|
|
use Yiisoft\Assets\Exception\InvalidConfigException; |
10
|
|
|
|
11
|
|
|
use function array_key_exists; |
12
|
|
|
use function array_merge; |
13
|
|
|
use function array_shift; |
14
|
|
|
use function array_unshift; |
15
|
|
|
use function in_array; |
16
|
|
|
use function is_array; |
17
|
|
|
use function is_file; |
18
|
|
|
use function is_int; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AssetManager manages asset bundle configuration and loading. |
22
|
|
|
*/ |
23
|
|
|
final class AssetManager |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string[] List of names of allowed asset bundles. |
27
|
|
|
* If the array is empty, then any asset bundles are allowed. Default to empty array. |
28
|
|
|
* |
29
|
|
|
* If the names of allowed asset bundles were specified, only these asset bundles or their dependencies can be |
30
|
|
|
* registered {@see register()} and received {@see getBundle ()}. Also, specifying names allows to export |
31
|
|
|
* {@see export()} asset bundles automatically without first registering them manually. |
32
|
|
|
*/ |
33
|
|
|
private array $allowedBundleNames; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array The asset bundle configurations. This property is provided to customize asset bundles. |
37
|
|
|
* When a bundle is being loaded by {@see getBundle()}, if it has a corresponding configuration |
38
|
|
|
* specified here, the configuration will be applied to the bundle. |
39
|
|
|
* |
40
|
|
|
* The array keys are the asset class bundle names (without leading backslash). |
41
|
|
|
* If a value is false, it means the corresponding asset bundle is disabled and {@see getBundle()} |
42
|
|
|
* should return an instance of the specified asset bundle with empty property values. |
43
|
|
|
*/ |
44
|
|
|
private array $customizedBundles; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array AssetBundle[] list of the registered asset bundles. |
48
|
|
|
* The keys are the bundle names, and the values are the registered {@see AssetBundle} objects. |
49
|
|
|
* |
50
|
|
|
* {@see registerAssetBundle()} |
51
|
|
|
*/ |
52
|
|
|
private array $registeredBundles = []; |
53
|
|
|
|
54
|
|
|
private array $loadedBundles = []; |
55
|
|
|
private array $dummyBundles = []; |
56
|
|
|
private array $cssFiles = []; |
57
|
|
|
private array $jsFiles = []; |
58
|
|
|
private array $jsStrings = []; |
59
|
|
|
private array $jsVar = []; |
60
|
|
|
private ?AssetConverterInterface $converter = null; |
61
|
|
|
private ?AssetPublisherInterface $publisher = null; |
62
|
|
|
private AssetLoaderInterface $loader; |
63
|
|
|
private Aliases $aliases; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Aliases $aliases The aliases instance. |
67
|
|
|
* @param AssetLoaderInterface $loader The loader instance. |
68
|
|
|
* @param array $allowedBundleNames List of names of allowed asset bundles {@see $allowedBundleNames}. |
69
|
|
|
* @param array $customizedBundles The asset bundle configurations {@see $customizedBundles}. |
70
|
|
|
*/ |
71
|
78 |
|
public function __construct( |
72
|
|
|
Aliases $aliases, |
73
|
|
|
AssetLoaderInterface $loader, |
74
|
|
|
array $allowedBundleNames = [], |
75
|
|
|
array $customizedBundles = [] |
76
|
|
|
) { |
77
|
78 |
|
$this->aliases = $aliases; |
78
|
78 |
|
$this->loader = $loader; |
79
|
78 |
|
$this->allowedBundleNames = $allowedBundleNames; |
80
|
78 |
|
$this->customizedBundles = $customizedBundles; |
81
|
78 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns a cloned named asset bundle. |
85
|
|
|
* |
86
|
|
|
* This method will first look for the bundle in {@see $customizedBundles}. |
87
|
|
|
* If not found, it will treat `$name` as the class of the asset bundle and create a new instance of it. |
88
|
|
|
* If `$name` is not a class name, an {@see AssetBundle} instance will be created. |
89
|
|
|
* |
90
|
|
|
* Cloning is used to prevent an asset bundle instance from being modified in a non-context of the asset manager. |
91
|
|
|
* |
92
|
|
|
* @param string $name The class name of the asset bundle (without the leading backslash). |
93
|
|
|
* |
94
|
|
|
* @throws InvalidConfigException For invalid asset bundle configuration. |
95
|
|
|
* |
96
|
|
|
* @return AssetBundle The asset bundle instance. |
97
|
|
|
*/ |
98
|
7 |
|
public function getBundle(string $name): AssetBundle |
99
|
|
|
{ |
100
|
7 |
|
if (!empty($this->allowedBundleNames)) { |
101
|
3 |
|
$this->checkAllowedBundleName($name); |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
$bundle = $this->loadBundle($name); |
105
|
7 |
|
$bundle = $this->publishBundle($bundle); |
106
|
|
|
|
107
|
7 |
|
return clone $bundle; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function getConverter(): ?AssetConverterInterface |
111
|
|
|
{ |
112
|
1 |
|
return $this->converter; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
public function getLoader(): AssetLoaderInterface |
116
|
|
|
{ |
117
|
3 |
|
return $this->loader; |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
public function getPublisher(): ?AssetPublisherInterface |
121
|
|
|
{ |
122
|
4 |
|
return $this->publisher; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return config array CSS AssetBundle. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
16 |
|
public function getCssFiles(): array |
131
|
|
|
{ |
132
|
16 |
|
return $this->cssFiles; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns config array JS AssetBundle. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
25 |
|
public function getJsFiles(): array |
141
|
|
|
{ |
142
|
25 |
|
return $this->jsFiles; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns JS code blocks. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
public function getJsStrings(): array |
151
|
|
|
{ |
152
|
1 |
|
return $this->jsStrings; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns JS variables. |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
1 |
|
public function getJsVar(): array |
161
|
|
|
{ |
162
|
1 |
|
return $this->jsVar; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets the asset converter. |
167
|
|
|
* |
168
|
|
|
* @param AssetConverterInterface $converter The asset converter. This can be either an object implementing the |
169
|
|
|
* {@see AssetConverterInterface}, or a configuration array that can be used to create the asset converter object. |
170
|
|
|
*/ |
171
|
78 |
|
public function setConverter(AssetConverterInterface $converter): void |
172
|
|
|
{ |
173
|
78 |
|
$this->converter = $converter; |
174
|
78 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Sets the asset publisher. |
178
|
|
|
* |
179
|
|
|
* @param AssetPublisherInterface $publisher |
180
|
|
|
*/ |
181
|
78 |
|
public function setPublisher(AssetPublisherInterface $publisher): void |
182
|
|
|
{ |
183
|
78 |
|
$this->publisher = $publisher; |
184
|
78 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Exports registered asset bundles {@see $registeredBundles}. |
188
|
|
|
* |
189
|
|
|
* When using the allowed asset bundles {@see $allowedBundleNames}, the export result will always be the same, |
190
|
|
|
* since the asset bundles are registered before the export. If do not use the allowed asset bundles mode, |
191
|
|
|
* must register {@see register()} all the required asset bundles before exporting. |
192
|
|
|
* |
193
|
|
|
* @param AssetExporterInterface $exporter The exporter instance. |
194
|
|
|
* |
195
|
|
|
* @throws InvalidConfigException If an error occurs during registration when using allowed asset bundles. |
196
|
|
|
* @throws RuntimeException If no asset bundles were registered or an error occurred during the export. |
197
|
|
|
*/ |
198
|
4 |
|
public function export(AssetExporterInterface $exporter): void |
199
|
|
|
{ |
200
|
4 |
|
if (!empty($this->allowedBundleNames)) { |
201
|
2 |
|
$this->register($this->allowedBundleNames); |
202
|
|
|
} |
203
|
|
|
|
204
|
4 |
|
if (empty($this->registeredBundles)) { |
205
|
1 |
|
throw new RuntimeException('Not a single asset bundle was registered.'); |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
$exporter->export($this->registeredBundles); |
209
|
3 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Generates the array configuration of the asset bundles {@see $registeredBundles}. |
213
|
|
|
* |
214
|
|
|
* @param string[] $names |
215
|
|
|
* @param int|null $position |
216
|
|
|
* |
217
|
|
|
* @throws InvalidConfigException |
218
|
|
|
* @throws RuntimeException |
219
|
|
|
*/ |
220
|
78 |
|
public function register(array $names, ?int $position = null): void |
221
|
|
|
{ |
222
|
78 |
|
if (!empty($this->allowedBundleNames)) { |
223
|
5 |
|
foreach ($names as $name) { |
224
|
5 |
|
$this->checkAllowedBundleName($name); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
78 |
|
foreach ($names as $name) { |
229
|
38 |
|
$this->registerAssetBundle($name, $position); |
230
|
34 |
|
$this->registerFiles($name); |
231
|
|
|
} |
232
|
78 |
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns whether the asset bundle is registered. |
236
|
|
|
* |
237
|
|
|
* @param string $name The class name of the asset bundle (without the leading backslash). |
238
|
|
|
* |
239
|
|
|
* @return bool Whether the asset bundle is registered. |
240
|
|
|
*/ |
241
|
3 |
|
public function isRegisteredBundle(string $name): bool |
242
|
|
|
{ |
243
|
3 |
|
return isset($this->registeredBundles[$name]); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Registers a CSS file. |
248
|
|
|
* |
249
|
|
|
* @param string $url The CSS file to be registered. |
250
|
|
|
* @param array $options The HTML attributes for the link tag. |
251
|
|
|
* @param string|null $key The key that identifies the CSS file. |
252
|
|
|
*/ |
253
|
30 |
|
private function registerCssFile(string $url, array $options = [], string $key = null): void |
254
|
|
|
{ |
255
|
30 |
|
$key = $key ?: $url; |
256
|
|
|
|
257
|
30 |
|
$this->cssFiles[$key]['url'] = $url; |
258
|
30 |
|
$this->cssFiles[$key]['attributes'] = $options; |
259
|
30 |
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Registers a JS file. |
263
|
|
|
* |
264
|
|
|
* @param string $url The JS file to be registered. |
265
|
|
|
* @param array $options The HTML attributes for the script tag. The following options are specially handled and |
266
|
|
|
* are not treated as HTML attributes: |
267
|
|
|
* |
268
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
269
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_HEAD} In the head section. |
270
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_BEGIN} At the beginning of the body section. |
271
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_END} At the end of the body section. This is the default value. |
272
|
|
|
* @param string|null $key The key that identifies the JS file. |
273
|
|
|
*/ |
274
|
39 |
|
private function registerJsFile(string $url, array $options = [], string $key = null): void |
275
|
|
|
{ |
276
|
39 |
|
$key = $key ?: $url; |
277
|
|
|
|
278
|
39 |
|
if (!array_key_exists('position', $options)) { |
279
|
33 |
|
$options = array_merge(['position' => 3], $options); |
280
|
|
|
} |
281
|
|
|
|
282
|
39 |
|
$this->jsFiles[$key]['url'] = $url; |
283
|
39 |
|
$this->jsFiles[$key]['attributes'] = $options; |
284
|
39 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Registers a JavaScript code block. |
288
|
|
|
* |
289
|
|
|
* @param string $jsString The JavaScript code block to be registered. |
290
|
|
|
* @param array $options The HTML attributes for the script tag. The following options are specially handled and |
291
|
|
|
* are not treated as HTML attributes: |
292
|
|
|
* |
293
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
294
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_HEAD} In the head section. |
295
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_BEGIN} At the beginning of the body section. |
296
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_END} At the end of the body section. This is the default value. |
297
|
|
|
* @param string|null $key The key that identifies the JS code block. If null, it will use $jsString as the key. |
298
|
|
|
* If two JS code blocks are registered with the same key, the latter will overwrite the former. |
299
|
|
|
*/ |
300
|
4 |
|
private function registerJsString(string $jsString, array $options = [], string $key = null): void |
301
|
|
|
{ |
302
|
4 |
|
$key = $key ?: $jsString; |
303
|
|
|
|
304
|
4 |
|
if (!array_key_exists('position', $options)) { |
305
|
4 |
|
$options = array_merge(['position' => 3], $options); |
306
|
|
|
} |
307
|
|
|
|
308
|
4 |
|
$this->jsStrings[$key]['string'] = $jsString; |
309
|
4 |
|
$this->jsStrings[$key]['attributes'] = $options; |
310
|
4 |
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Registers a JS variable. |
314
|
|
|
* |
315
|
|
|
* @param string $varName The variable name. |
316
|
|
|
* @param array|string $jsVar The JS code block to be registered. |
317
|
|
|
* @param array $options The HTML attributes for the script tag. The following options are specially handled and |
318
|
|
|
* are not treated as HTML attributes: |
319
|
|
|
* |
320
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
321
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_HEAD} In the head section. This is the default value. |
322
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_BEGIN} At the beginning of the body section. |
323
|
|
|
* * {@see \Yiisoft\View\WebView::POSITION_END} At the end of the body section. |
324
|
|
|
*/ |
325
|
4 |
|
private function registerJsVar(string $varName, $jsVar, array $options = []): void |
326
|
|
|
{ |
327
|
4 |
|
if (!array_key_exists('position', $options)) { |
328
|
4 |
|
$options = array_merge(['position' => 1], $options); |
329
|
|
|
} |
330
|
|
|
|
331
|
4 |
|
$this->jsVar[$varName]['variables'] = $jsVar; |
332
|
4 |
|
$this->jsVar[$varName]['attributes'] = $options; |
333
|
4 |
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Converter SASS, SCSS, Stylus and other formats to CSS. |
337
|
|
|
* |
338
|
|
|
* @param AssetBundle $bundle |
339
|
|
|
*/ |
340
|
13 |
|
private function convertCss(AssetBundle $bundle): void |
341
|
|
|
{ |
342
|
13 |
|
foreach ($bundle->css as $i => $css) { |
343
|
12 |
|
if (is_array($css)) { |
344
|
1 |
|
$file = array_shift($css); |
345
|
1 |
|
if (AssetUtil::isRelative($file)) { |
346
|
1 |
|
$css = array_merge($bundle->cssOptions, $css); |
347
|
1 |
|
$baseFile = $this->aliases->get("{$bundle->basePath}/{$file}"); |
348
|
1 |
|
if (is_file($baseFile)) { |
349
|
|
|
/** |
350
|
|
|
* @psalm-suppress PossiblyNullArgument |
351
|
|
|
* @psalm-suppress PossiblyNullReference |
352
|
|
|
*/ |
353
|
1 |
|
array_unshift($css, $this->converter->convert( |
|
|
|
|
354
|
1 |
|
$file, |
355
|
1 |
|
$bundle->basePath, |
|
|
|
|
356
|
1 |
|
$bundle->converterOptions, |
357
|
|
|
)); |
358
|
|
|
|
359
|
1 |
|
$bundle->css[$i] = $css; |
360
|
|
|
} |
361
|
|
|
} |
362
|
12 |
|
} elseif (AssetUtil::isRelative($css)) { |
363
|
12 |
|
$baseCss = $this->aliases->get("{$bundle->basePath}/{$css}"); |
364
|
12 |
|
if (is_file("$baseCss")) { |
365
|
|
|
/** |
366
|
|
|
* @psalm-suppress PossiblyNullArgument |
367
|
|
|
* @psalm-suppress PossiblyNullReference |
368
|
|
|
*/ |
369
|
11 |
|
$bundle->css[$i] = $this->converter->convert( |
370
|
11 |
|
$css, |
371
|
11 |
|
$bundle->basePath, |
372
|
11 |
|
$bundle->converterOptions |
373
|
|
|
); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
13 |
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Convert files from TypeScript and other formats into JavaScript. |
381
|
|
|
* |
382
|
|
|
* @param AssetBundle $bundle |
383
|
|
|
*/ |
384
|
13 |
|
private function convertJs(AssetBundle $bundle): void |
385
|
|
|
{ |
386
|
13 |
|
foreach ($bundle->js as $i => $js) { |
387
|
13 |
|
if (is_array($js)) { |
388
|
1 |
|
$file = array_shift($js); |
389
|
1 |
|
if (AssetUtil::isRelative($file)) { |
390
|
1 |
|
$js = array_merge($bundle->jsOptions, $js); |
391
|
1 |
|
$baseFile = $this->aliases->get("{$bundle->basePath}/{$file}"); |
392
|
1 |
|
if (is_file($baseFile)) { |
393
|
|
|
/** |
394
|
|
|
* @psalm-suppress PossiblyNullArgument |
395
|
|
|
* @psalm-suppress PossiblyNullReference |
396
|
|
|
*/ |
397
|
1 |
|
array_unshift($js, $this->converter->convert( |
398
|
1 |
|
$file, |
399
|
1 |
|
$bundle->basePath, |
|
|
|
|
400
|
1 |
|
$bundle->converterOptions |
401
|
|
|
)); |
402
|
|
|
|
403
|
1 |
|
$bundle->js[$i] = $js; |
404
|
|
|
} |
405
|
|
|
} |
406
|
13 |
|
} elseif (AssetUtil::isRelative($js)) { |
407
|
13 |
|
$baseJs = $this->aliases->get("{$bundle->basePath}/{$js}"); |
408
|
13 |
|
if (is_file($baseJs)) { |
409
|
|
|
/** |
410
|
|
|
* @psalm-suppress PossiblyNullArgument |
411
|
|
|
* @psalm-suppress PossiblyNullReference |
412
|
|
|
*/ |
413
|
12 |
|
$bundle->js[$i] = $this->converter->convert($js, $bundle->basePath); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
} |
417
|
13 |
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Registers the named asset bundle. |
421
|
|
|
* |
422
|
|
|
* All dependent asset bundles will be registered. |
423
|
|
|
* |
424
|
|
|
* @param string $name The class name of the asset bundle (without the leading backslash). |
425
|
|
|
* @param int|null $position If set, this forces a minimum position for javascript files. |
426
|
|
|
* This will adjust depending assets javascript file position or fail if requirement can not be met. |
427
|
|
|
* If this is null, asset bundles position settings will not be changed. |
428
|
|
|
* |
429
|
|
|
* {@see registerJsFile()} For more details on javascript position. |
430
|
|
|
* |
431
|
|
|
* @throws InvalidConfigException If the asset or the asset file paths to be published does not exist. |
432
|
|
|
* @throws RuntimeException If the asset bundle does not exist or a circular dependency is detected. |
433
|
|
|
*/ |
434
|
38 |
|
private function registerAssetBundle(string $name, int $position = null): void |
435
|
|
|
{ |
436
|
38 |
|
if (!isset($this->registeredBundles[$name])) { |
437
|
38 |
|
$bundle = $this->publishBundle($this->loadBundle($name)); |
438
|
|
|
|
439
|
37 |
|
$this->registeredBundles[$name] = false; |
440
|
|
|
|
441
|
37 |
|
$pos = $bundle->jsOptions['position'] ?? null; |
442
|
|
|
|
443
|
37 |
|
foreach ($bundle->depends as $dep) { |
444
|
26 |
|
$this->registerAssetBundle($dep, $pos); |
445
|
|
|
} |
446
|
|
|
|
447
|
36 |
|
$this->registeredBundles[$name] = $bundle; |
448
|
11 |
|
} elseif ($this->registeredBundles[$name] === false) { |
449
|
1 |
|
throw new RuntimeException("A circular dependency is detected for bundle \"{$name}\"."); |
450
|
|
|
} else { |
451
|
10 |
|
$bundle = $this->registeredBundles[$name]; |
452
|
|
|
} |
453
|
|
|
|
454
|
36 |
|
if ($position !== null) { |
455
|
11 |
|
$pos = $bundle->jsOptions['position'] ?? null; |
456
|
|
|
|
457
|
11 |
|
if ($pos === null) { |
458
|
10 |
|
$bundle->jsOptions['position'] = $pos = $position; |
459
|
5 |
|
} elseif ($pos > $position) { |
460
|
4 |
|
throw new RuntimeException( |
461
|
4 |
|
"An asset bundle that depends on \"{$name}\" has a higher JavaScript file " . |
462
|
4 |
|
"position configured than \"{$name}\"." |
463
|
|
|
); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
// update position for all dependencies |
467
|
11 |
|
foreach ($bundle->depends as $dep) { |
468
|
7 |
|
$this->registerAssetBundle($dep, $pos); |
469
|
|
|
} |
470
|
|
|
} |
471
|
36 |
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Register assets from a named bundle and its dependencies. |
475
|
|
|
* |
476
|
|
|
* @param string $bundleName The asset bundle name. |
477
|
|
|
* |
478
|
|
|
* @throws InvalidConfigException If asset files are not found. |
479
|
|
|
*/ |
480
|
34 |
|
private function registerFiles(string $bundleName): void |
481
|
|
|
{ |
482
|
34 |
|
if (!isset($this->registeredBundles[$bundleName])) { |
483
|
|
|
return; |
484
|
|
|
} |
485
|
|
|
|
486
|
34 |
|
$bundle = $this->registeredBundles[$bundleName]; |
487
|
|
|
|
488
|
34 |
|
foreach ($bundle->depends as $dep) { |
489
|
23 |
|
$this->registerFiles($dep); |
490
|
|
|
} |
491
|
|
|
|
492
|
34 |
|
$this->registerAssetFiles($bundle); |
493
|
31 |
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Registers asset files from a bundle considering dependencies. |
497
|
|
|
* |
498
|
|
|
* @param AssetBundle $bundle |
499
|
|
|
* |
500
|
|
|
* @throws InvalidConfigException If asset files are not found. |
501
|
|
|
*/ |
502
|
34 |
|
private function registerAssetFiles(AssetBundle $bundle): void |
503
|
|
|
{ |
504
|
34 |
|
if (isset($bundle->basePath, $bundle->baseUrl) && null !== $this->converter) { |
505
|
13 |
|
$this->convertCss($bundle); |
506
|
13 |
|
$this->convertJs($bundle); |
507
|
|
|
} |
508
|
|
|
|
509
|
34 |
|
foreach ($bundle->js as $js) { |
510
|
32 |
|
if (is_array($js)) { |
511
|
2 |
|
$file = array_shift($js); |
512
|
2 |
|
$options = array_merge($bundle->jsOptions, $js); |
513
|
2 |
|
$this->registerJsFile($this->loader->getAssetUrl($bundle, $file), $options); |
514
|
31 |
|
} elseif ($js !== null) { |
515
|
31 |
|
$this->registerJsFile($this->loader->getAssetUrl($bundle, $js), $bundle->jsOptions); |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|
519
|
31 |
|
foreach ($bundle->jsStrings as $key => $jsString) { |
520
|
4 |
|
$key = is_int($key) ? $jsString : $key; |
521
|
4 |
|
if (is_array($jsString)) { |
522
|
4 |
|
$string = array_shift($jsString); |
523
|
4 |
|
$this->registerJsString($string, $jsString, $key); |
524
|
4 |
|
} elseif ($jsString !== null) { |
525
|
4 |
|
$this->registerJsString($jsString, $bundle->jsOptions, $key); |
526
|
|
|
} |
527
|
|
|
} |
528
|
|
|
|
529
|
31 |
|
foreach ($bundle->jsVar as $key => $jsVar) { |
530
|
4 |
|
$this->registerJsVar($key, $jsVar, $jsVar); |
531
|
|
|
} |
532
|
|
|
|
533
|
31 |
|
foreach ($bundle->css as $css) { |
534
|
20 |
|
if (is_array($css)) { |
535
|
1 |
|
$file = array_shift($css); |
536
|
1 |
|
$options = array_merge($bundle->cssOptions, $css); |
537
|
1 |
|
$this->registerCssFile($this->loader->getAssetUrl($bundle, $file), $options); |
538
|
20 |
|
} elseif ($css !== null) { |
539
|
20 |
|
$this->registerCssFile($this->loader->getAssetUrl($bundle, $css), $bundle->cssOptions); |
540
|
|
|
} |
541
|
|
|
} |
542
|
31 |
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Loads an asset bundle class by name. |
546
|
|
|
* |
547
|
|
|
* @param string $name The asset bundle name. |
548
|
|
|
* |
549
|
|
|
* @throws InvalidConfigException For invalid asset bundle configuration. |
550
|
|
|
* |
551
|
|
|
* @return AssetBundle The asset bundle instance. |
552
|
|
|
*/ |
553
|
40 |
|
private function loadBundle(string $name): AssetBundle |
554
|
|
|
{ |
555
|
40 |
|
if (isset($this->loadedBundles[$name])) { |
556
|
6 |
|
return $this->loadedBundles[$name]; |
557
|
|
|
} |
558
|
|
|
|
559
|
40 |
|
if (!isset($this->customizedBundles[$name])) { |
560
|
35 |
|
return $this->loadedBundles[$name] = $this->loader->loadBundle($name); |
561
|
|
|
} |
562
|
|
|
|
563
|
18 |
|
if ($this->customizedBundles[$name] instanceof AssetBundle) { |
564
|
1 |
|
return $this->loadedBundles[$name] = $this->customizedBundles[$name]; |
565
|
|
|
} |
566
|
|
|
|
567
|
17 |
|
if (is_array($this->customizedBundles[$name])) { |
568
|
15 |
|
return $this->loadedBundles[$name] = $this->loader->loadBundle($name, $this->customizedBundles[$name]); |
569
|
|
|
} |
570
|
|
|
|
571
|
2 |
|
if ($this->customizedBundles[$name] === false) { |
572
|
1 |
|
return $this->dummyBundles[$name] ??= $this->loader->loadBundle($name, (array) (new AssetBundle())); |
573
|
|
|
} |
574
|
|
|
|
575
|
1 |
|
throw new InvalidConfigException("Invalid configuration of the \"{$name}\" asset bundle."); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Publishes a asset bundle. |
580
|
|
|
* |
581
|
|
|
* @param AssetBundle $bundle The asset bundle to publish. |
582
|
|
|
* |
583
|
|
|
* @throws InvalidConfigException If the asset or the asset file paths to be published does not exist. |
584
|
|
|
* |
585
|
|
|
* @return AssetBundle The published asset bundle. |
586
|
|
|
*/ |
587
|
39 |
|
private function publishBundle(AssetBundle $bundle): AssetBundle |
588
|
|
|
{ |
589
|
39 |
|
if (!$bundle->cdn && $this->publisher !== null && !empty($bundle->sourcePath)) { |
590
|
7 |
|
[$bundle->basePath, $bundle->baseUrl] = $this->publisher->publish($bundle); |
591
|
|
|
} |
592
|
|
|
|
593
|
39 |
|
return $bundle; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Checks whether asset bundle are allowed by name {@see $allowedBundleNames}. |
598
|
|
|
* |
599
|
|
|
* @param string $name The asset bundle name to check. |
600
|
|
|
* |
601
|
|
|
* @throws InvalidConfigException For invalid asset bundle configuration. |
602
|
|
|
* @throws RuntimeException If The asset bundle name is not allowed. |
603
|
|
|
*/ |
604
|
5 |
|
public function checkAllowedBundleName(string $name): void |
605
|
|
|
{ |
606
|
5 |
|
if (isset($this->loadedBundles[$name]) || in_array($name, $this->allowedBundleNames, true)) { |
607
|
5 |
|
return; |
608
|
|
|
} |
609
|
|
|
|
610
|
3 |
|
foreach ($this->allowedBundleNames as $bundleName) { |
611
|
3 |
|
if ($this->isAllowedBundleDependencies($name, $this->loadBundle($bundleName))) { |
612
|
1 |
|
return; |
613
|
|
|
} |
614
|
|
|
} |
615
|
|
|
|
616
|
3 |
|
throw new RuntimeException("The \"{$name}\" asset bundle is not allowed."); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Recursively checks whether the asset bundle name is allowed in dependencies. |
621
|
|
|
* |
622
|
|
|
* @param string $name The asset bundle name to check. |
623
|
|
|
* @param AssetBundle $bundle The asset bundle to check. |
624
|
|
|
* |
625
|
|
|
* @throws InvalidConfigException For invalid asset bundle configuration. |
626
|
|
|
* |
627
|
|
|
* @return bool Whether the asset bundle name is allowed in dependencies. |
628
|
|
|
*/ |
629
|
3 |
|
private function isAllowedBundleDependencies(string $name, AssetBundle $bundle): bool |
630
|
|
|
{ |
631
|
3 |
|
foreach ($bundle->depends as $depend) { |
632
|
2 |
|
if ($name === $depend || $this->isAllowedBundleDependencies($name, $this->loadBundle($depend))) { |
633
|
1 |
|
return true; |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
|
637
|
3 |
|
return false; |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.