1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\ThemeModule\Engine; |
15
|
|
|
|
16
|
|
|
use ArrayIterator; |
17
|
|
|
use Countable; |
18
|
|
|
use IteratorAggregate; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AssetBag |
22
|
|
|
* |
23
|
|
|
* This class provides an abstracted method of collecting, managing and retrieving page assets. |
24
|
|
|
* Each asset should be assigned a 'weight' which helps determine in what order the assets are loaded into the page. |
25
|
|
|
* - A lighter weight loads before a heavier weight. |
26
|
|
|
* - Assets of the same weight cannot be guaranteed to load in any specific order. |
27
|
|
|
* - Duplicate assets with different weights will be loaded according to the lighter weight. |
28
|
|
|
* - Assets not given a weight are assigned the self::WEIGHT_DEFAULT (100) |
29
|
|
|
* - Core assets are loaded at weights 0, 1, 2, etc. |
30
|
|
|
* @see \Zikula\ThemeModule\EventListener\DefaultPageAssetSetterListener::setDefaultPageAssets() |
31
|
|
|
* @see \Zikula\ThemeModule\Twig\Extension\AssetExtension::pageAddAsset() |
32
|
|
|
*/ |
33
|
|
|
class AssetBag implements IteratorAggregate, Countable |
34
|
|
|
{ |
35
|
|
|
public const WEIGHT_JQUERY = 20; |
36
|
|
|
|
37
|
|
|
public const WEIGHT_JQUERY_UI = 25; |
38
|
|
|
|
39
|
|
|
public const WEIGHT_BOOTSTRAP_JS = 30; |
40
|
|
|
|
41
|
|
|
public const WEIGHT_BOOTSTRAP_ZIKULA = 31; |
42
|
|
|
|
43
|
|
|
public const WEIGHT_ROUTER_JS = 40; |
44
|
|
|
|
45
|
|
|
public const WEIGHT_ROUTES_JS = 41; |
46
|
|
|
|
47
|
|
|
public const WEIGHT_JS_TRANSLATOR = 50; |
48
|
|
|
|
49
|
|
|
public const WEIGHT_DEFAULT = 100; |
50
|
|
|
|
51
|
|
|
public const WEIGHT_JS_TRANSLATIONS = 110; |
52
|
|
|
|
53
|
|
|
public const WEIGHT_THEME_STYLESHEET = 120; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Array format: |
57
|
|
|
* $assets = [value => weight, value => weight, value => weight] |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $assets = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add an array of assets or a single asset (string) to the bag. |
64
|
|
|
* |
65
|
|
|
* @param string|array $asset |
66
|
|
|
*/ |
67
|
|
|
public function add($asset): void |
68
|
|
|
{ |
69
|
|
|
// ensure value is an array |
70
|
|
|
if (!is_array($asset)) { |
71
|
|
|
$asset = [$asset => self::WEIGHT_DEFAULT]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($asset as $source => $weight) { |
75
|
|
|
if (!isset($this->assets[$source]) || (isset($this->assets[$source]) && $this->assets[$source] > $weight)) { |
76
|
|
|
// keep original weight if lighter. set if not set already. |
77
|
|
|
$this->assets[$source] = $weight; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
asort($this->assets); // put array in order by weight |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function remove($var): void |
84
|
|
|
{ |
85
|
|
|
if (!is_array($var)) { |
86
|
|
|
unset($this->assets[$var]); |
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
$source = array_key_first($var); |
91
|
|
|
$weight = $var[$source]; |
92
|
|
|
if (isset($this->assets[$source]) && $weight === $this->assets[$source]) { |
93
|
|
|
unset($this->assets[$source]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function clear(): void |
98
|
|
|
{ |
99
|
|
|
$this->assets = []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function all(): array |
103
|
|
|
{ |
104
|
|
|
return array_keys($this->assets); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function allWithWeight(): array |
108
|
|
|
{ |
109
|
|
|
return $this->assets; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an iterator for parameters. |
114
|
|
|
*/ |
115
|
|
|
public function getIterator(): ArrayIterator |
116
|
|
|
{ |
117
|
|
|
return new ArrayIterator($this->assets); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the number of parameters. |
122
|
|
|
*/ |
123
|
|
|
public function count(): int |
124
|
|
|
{ |
125
|
|
|
return count($this->assets); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|