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