vitemanifest.php
6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* Класс для подключения ресурсов (js/css)
* из сгенерированного manifest.json бандлера Vite
* [https://vitejs.dev]
*
* Copyright (c) 2022 Max Zhurkin <maximzhurkin@gmail.com>
*
* MIT License [http://www.opensource.org/licenses/mit-license.php]
*/
class ViteManifest
{
/**
* Текущий URI
* @var string
*/
protected $uri;
/**
* Правила соотношений URI с точками входа из манифеста
* @var array
*/
protected $rules;
/**
* Распарсенный manifest.json в массив
* @var array
*/
protected $manifest;
/**
* Префикс к ресурсам
*
* @var string
*/
protected $baseUrl;
function __construct()
{
// Устанавливаем текущий урл
$this->uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
}
/**
* Получить полный путь с префиксом baseUrl
*
* @param string $path
*
* @return string
*/
private function getUrl($url)
{
return $this->baseUrl . $url;
}
/**
* Получить данные из точки входа по ключу
*
* @param string $entry
* @param string $key
*
* @return array
*/
private function getEntryData($entry, $key)
{
return isset($this->manifest[$entry][$key])
? $this->manifest[$entry][$key]
: [];
}
/**
* Установить префикс к ресурсам
*
* @param string $baseUrl
*
* @return void
*/
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}
/**
* Добавить правило
*
* @param string $rule
* @param string $entry
*
* @return void
*/
public function addRule($rule, $entry)
{
$this->rules[$rule] = $entry;
}
/**
* Загрузить manifest.json
*
* @param string $file
*
* @return void
*/
public function loadManifest($file)
{
if (file_exists($file)) {
$this->manifest = json_decode(file_get_contents($file), true);
} else {
throw new \Exception('Manifest json file not found');
}
}
/**
* Загрузить правила из файла
*
* @param string $file
*
* @return void
*/
public function loadRules($file)
{
if (file_exists($file)) {
$rules = include($file);
foreach ($rules as $rule => $entry) {
$this->addRule($rule, $entry);
}
} else {
throw new \Exception('Config rules file not found');
}
}
/**
* Получить имя точки входа из манифеста по текущему урлу
*
* @return string
*/
public function getCurrentEntry()
{
foreach ($this->rules as $rule => $entry) {
if (preg_match("~^{$rule}$~", $this->uri)) {
return $entry;
}
}
return false;
}
/**
* Получить имя точки входа для старых браузеров
*
* @param string $entry
*
* @return string
*/
public function getLegacyEntry($entry)
{
$extension = pathinfo($entry, PATHINFO_EXTENSION);
return basename($entry, ".$extension") . '-legacy.' . $extension;
}
/**
* Получить точку входа из манифеста
*
* @param string $entry
*
* @return string
*/
public function getModule($entry)
{
return isset($this->manifest[$entry]['file'])
? $this->getUrl($this->manifest[$entry]['file'])
: '';
}
/**
* Получить polyfills-legacy.js
*
* @return string
*/
public function getPolyfill()
{
foreach ($this->manifest as $entry => $values) {
if (preg_match("~^(.*)legacy-polyfills$~", $entry)) {
return isset($values['file'])
? $this->getUrl($values['file'])
: false;
}
}
return false;
}
/**
* Получить точку входа для старых браузеров
*
* @param string $entry
*
* @return string
*/
public function getModuleLegacy($entry)
{
return $this->getModule($this->getLegacyEntry($entry));
}
/**
* Получить массив импортов точки входа
*
* @param string $entry
*
* @return array
*/
public function getImports($entry)
{
return $this->getEntryData($entry, 'imports');
}
/**
* Получить css точки входа
* @param mixed $entry
*
* @return [type]
*/
public function getCss($entry)
{
return $this->getEntryData($entry, 'css');
}
/**
* Получить дочерние модули точки входа
*
* @param string $entry
*
* @return array
*/
public function getPreloadModules($entry)
{
$modules = [];
foreach ($this->getImports($entry) as $module) {
$modules[] = $this->getModule($module);
}
return $modules;
}
/**
* Получить все стили с дочерними модулями
*
* @param string $entry
*
* @return array
*/
public function getStyles($entry)
{
$styles = [];
$modules = $this->getImports($entry);
// Получаем стили дочерних модулей
foreach ($modules as $module) {
foreach ($this->getCss($module) as $style) {
$styles[] = $this->getUrl($style);
}
}
// Получаем стили точки входа
foreach ($this->getCss($entry) as $style) {
$styles[] = $this->getUrl($style);
}
return $styles;
}
/**
* Получить все ресурсы
*
* @param string $entry
*
* @return array
*/
public function getAssets($entry)
{
return [
'module' => [
'modern' => $this->getModule($entry),
'legacy' => $this->getModuleLegacy($entry),
],
'preloads' => $this->getPreloadModules($entry),
'styles' => $this->getStyles($entry),
'polyfill' => $this->getPolyfill(),
];
}
/**
* Получить все ресурсы по текущей точке входа
*
* @return array
*/
public function getCurrentAssets()
{
return $this->getAssets($this->getCurrentEntry());
}
}