1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Asset; |
16
|
|
|
|
17
|
|
|
use Quantum\Exceptions\AssetException; |
18
|
|
|
use Quantum\Exceptions\LangException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AssetManager |
22
|
|
|
* @package Quantum\Libraries\Asset |
23
|
|
|
*/ |
24
|
|
|
class AssetManager |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* CSS assets store |
29
|
|
|
*/ |
30
|
|
|
const CSS_STORE = 1; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* JS assets store |
34
|
|
|
*/ |
35
|
|
|
const JS_STORE = 2; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Asset store |
39
|
|
|
* @var Asset[] |
40
|
|
|
*/ |
41
|
|
|
private $store = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Published assets |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $published = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Asset instance |
51
|
|
|
* @var AssetManager|null |
52
|
|
|
*/ |
53
|
|
|
private static $instance = null; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* AssetManager instance |
57
|
|
|
* @return AssetManager|null |
58
|
|
|
*/ |
59
|
|
|
public static function getInstance(): ?AssetManager |
60
|
|
|
{ |
61
|
|
|
if (self::$instance == null) { |
62
|
|
|
self::$instance = new self(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return self::$instance; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Register assets |
70
|
|
|
* @param Asset[] $assets |
71
|
|
|
* @throws AssetException |
72
|
|
|
* @throws LangException |
73
|
|
|
*/ |
74
|
|
|
public function register(array $assets) |
75
|
|
|
{ |
76
|
|
|
foreach ($assets as $asset) { |
77
|
|
|
$this->registerAsset($asset); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Register single asset |
83
|
|
|
* @param Asset $asset |
84
|
|
|
* @throws AssetException |
85
|
|
|
* @throws LangException |
86
|
|
|
*/ |
87
|
|
|
public function registerAsset(Asset $asset) |
88
|
|
|
{ |
89
|
|
|
if ($asset->getName()) { |
90
|
|
|
foreach ($this->store as $storedAsset) { |
91
|
|
|
if ($storedAsset->getName() == $asset->getName()) { |
92
|
|
|
throw AssetException::nameInUse($asset->getName()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->store[] = $asset; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Dumps the assets |
102
|
|
|
* @param int $type |
103
|
|
|
* @throws AssetException |
104
|
|
|
* @throws LangException |
105
|
|
|
*/ |
106
|
|
|
public function dump(int $type) |
107
|
|
|
{ |
108
|
|
|
$this->publish(); |
109
|
|
|
|
110
|
|
|
if (count($this->published[$type])) { |
111
|
|
|
foreach ($this->published[$type] as $asset) { |
112
|
|
|
echo $asset->tag() . PHP_EOL; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Gets the asset by name |
119
|
|
|
* @param string $name |
120
|
|
|
* @return Asset|null |
121
|
|
|
*/ |
122
|
|
|
public function get(string $name): ?Asset |
123
|
|
|
{ |
124
|
|
|
foreach ($this->store as $asset) { |
125
|
|
|
if ($asset->getName() == $name) { |
126
|
|
|
return $asset; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Asset url |
135
|
|
|
* @param string $path |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function url(string $path): string |
139
|
|
|
{ |
140
|
|
|
if (!parse_url($path, PHP_URL_HOST)) { |
141
|
|
|
return base_url() . '/assets/' . $path; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $path; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Publishes assets |
149
|
|
|
* @throws AssetException |
150
|
|
|
* @throws LangException |
151
|
|
|
*/ |
152
|
|
|
private function publish() |
153
|
|
|
{ |
154
|
|
|
if (empty($this->published)) { |
155
|
|
|
if (!empty($this->store)) { |
156
|
|
|
$this->setPriorityAssets(); |
157
|
|
|
$this->setRegularAssets(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
ksort($this->published[self::CSS_STORE]); |
161
|
|
|
ksort($this->published[self::JS_STORE]); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets assets with ordered position |
167
|
|
|
* @throws AssetException |
168
|
|
|
* @throws LangException |
169
|
|
|
*/ |
170
|
|
|
private function setPriorityAssets() |
171
|
|
|
{ |
172
|
|
|
foreach ($this->store as $asset) { |
173
|
|
|
if ($asset->getPosition() != -1) { |
174
|
|
|
if (isset($this->published[$asset->getType()][$asset->getPosition()])) { |
175
|
|
|
throw AssetException::positionInUse($asset->getPosition(), $asset->getPath()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->published[$asset->getType()][$asset->getPosition()] = $asset; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Sets assets without ordered position |
185
|
|
|
*/ |
186
|
|
|
private function setRegularAssets() |
187
|
|
|
{ |
188
|
|
|
foreach ($this->store as $asset) { |
189
|
|
|
if ($asset->getPosition() == -1) { |
190
|
|
|
$this->setPosition($asset, 0); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets the Position |
197
|
|
|
* @param Asset $asset |
198
|
|
|
* @param int $index |
199
|
|
|
*/ |
200
|
|
|
private function setPosition(Asset $asset, int $index) |
201
|
|
|
{ |
202
|
|
|
if (isset($this->published[$asset->getType()][$index])) { |
203
|
|
|
$this->setPosition($asset, $index + 1); |
204
|
|
|
} else { |
205
|
|
|
$this->published[$asset->getType()][$index] = $asset; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|