1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
manager.php
См. документацию.
1<?php
2
3namespace Bitrix\Landing\Assets;
4
5use \Bitrix\Main\Localization\Loc;
6use Bitrix\Main\UI\Extension;
7use Bitrix\Main;
8use Bitrix\Landing;
9use Bitrix\Main\IO\File;
10
11Loc::loadMessages(__FILE__);
12
20{
21 protected const MODE_STANDART = 'STANDART';
22 protected const MODE_WEBPACK = 'WEBPACK';
23
24 protected const REGISTERED_KEY_CODE = 'code';
25 protected const REGISTERED_KEY_LOCATION = 'location';
26
27 private static ?Manager $instance = null;
28
33 protected $mode;
34
39 private bool $isSandbox = false;
40
45 protected $registered = [];
49 protected $resources;
53 protected $builder;
54
59 public static function getInstance(): Manager
60 {
61 if (self::$instance === null)
62 {
63 self::$instance = new static;
64 }
65
66 return self::$instance;
67 }
68
72 public function __construct()
73 {
74 $this->mode = self::MODE_STANDART;
75 $this->resources = new ResourceCollection();
76 }
77
84 public function enableSandbox(): static
85 {
86 $this->isSandbox = true;
87 $this->resources = new ResourceCollection();
88
89 return $this;
90 }
91
95 public function setWebpackMode(): static
96 {
97 $this->mode = self::MODE_WEBPACK;
98
99 return $this;
100 }
101
105 public function setStandartMode(): static
106 {
107 $this->mode = self::MODE_STANDART;
108
109 return $this;
110 }
111
116 public function getMode(): string
117 {
118 return $this->mode;
119 }
120
125 protected function isAssetRegistered($code)
126 {
127 return array_key_exists($code, $this->registered);
128 }
129
135 protected function getRegisteredAssetLocation(string $code)
136 {
137 if ($this->isAssetRegistered($code))
138 {
139 return $this->registered[$code][self::REGISTERED_KEY_LOCATION];
140 }
141
142 return false;
143 }
144
148 protected function markAssetRegistered($code, $location): void
149 {
150 $this->registered[$code] = [
151 self::REGISTERED_KEY_CODE => $code,
152 self::REGISTERED_KEY_LOCATION => $location,
153 ];
154
155 if($code !== 'main.core' && $code !== 'core')
156 {
158 }
159 }
160
161
168 public function addAsset(string|array $code, ?int $location = null): static
169 {
170 // recursive for arrays
171 if (is_array($code))
172 {
173 foreach ($code as $item)
174 {
175 $this->addAsset((string)$item, $location);
176 }
177 }
178 else
179 {
180 $this->addAssetRecursive($code, $location);
181 }
182
183 return $this;
184 }
185
190 protected function addAssetRecursive(string $code, $location = null): void
191 {
193
194 // just once, but if new location more critical - readd
195 if (!$this->isNeedAddAsset($code, $location))
196 {
197 return;
198 }
199
200 // get data from CJSCore
201 if ($ext = \CJSCore::getExtInfo($code))
202 {
203 $asset = $ext;
204 }
205 else if ($ext = Extension::getConfig($code))
206 {
207 $asset = $ext;
208 }
209 // if name - it path
210 else if ($type = self::detectType($code))
211 {
212 $asset = [$type => [$code]];
213 }
214 else
215 {
216 return;
217 }
218
219 $this->markAssetRegistered($code, $location);
220 $this->processAsset($asset, $location);
221 }
222
228 protected function isNeedAddAsset(string $code, int $location): bool
229 {
230 if ($this->isAssetRegistered($code))
231 {
232 return $location < $this->getRegisteredAssetLocation($code);
233 }
234
235 if (
236 !$this->isSandbox
238 )
239 {
240 return false;
241 }
242
243 return true;
244 }
245
246
253 protected function processAsset(array $asset, int $location): void
254 {
255 foreach (Types::getAssetTypes() as $type)
256 {
257 if (!isset($asset[$type]) || empty($asset[$type]))
258 {
259 continue;
260 }
261
262 if (!is_array($asset[$type]))
263 {
264 $asset[$type] = [$asset[$type]];
265 }
266
267 switch ($type)
268 {
270 {
271 foreach ($asset[$type] as $rel)
272 {
273 $this->addAsset($rel, $location);
274 }
275 break;
276 }
277
278 case Types::TYPE_JS:
279 case Types::TYPE_CSS:
280 case Types::TYPE_LANG:
281 {
282 foreach ($asset[$type] as $path)
283 {
284 if (\CMain::isExternalLink($path))
285 {
286 $this->resources->addString($this->createStringFromPath($path, $type));
287 }
288 // todo: check is file exist
289 else if (self::detectType($path))
290 {
291 $this->resources->add($path, $type, $location);
292 }
293 }
294 break;
295 }
296
297 case Types::TYPE_FONT:
298 {
299 // preload fonts add immediately
300 foreach ($asset[$type] as $fontFile)
301 {
302 if (
303 !\CMain::isExternalLink($fontFile)
304 && File::isFileExists(Landing\Manager::getDocRoot() . $fontFile)
305 )
306 {
307 $this->resources->addString($this->createStringFromPath($fontFile, $type));
308 }
309 }
310 break;
311 }
312
313 default:
314 break;
315 }
316 }
317 }
318
325 protected function createStringFromPath(string $path, string $type): string
326 {
327 $externalLink = '';
328
329 switch ($type)
330 {
331 case Types::TYPE_CSS:
332 {
333 $externalLink = "<link href=\"$path\" type=\"text/css\" rel=\"stylesheet\">";
334 break;
335 }
336
337 case Types::TYPE_JS:
338 {
339 $externalLink = "<script src=\"$path\"></script>";
340 break;
341 }
342
343 case Types::TYPE_FONT:
344 {
345 $fontType = self::checkFontLinkType($path);
346 $externalLink = '<link rel="preload" href="' . $path
347 . '" as="font" crossorigin="anonymous" type="' . $fontType . '" crossorigin>';
348 break;
349 }
350
351 default:
352 break;
353 }
354
355 return $externalLink;
356 }
357
358
365 protected static function detectType(string $path): ?string
366 {
367 $path = parse_url($path)['path'];
368 $type = mb_strtolower(mb_substr(strrchr($path, '.'), 1));
369 switch ($type)
370 {
371 case 'js':
372 return Types::TYPE_JS;
373
374 case 'css':
375 return Types::TYPE_CSS;
376
377 case 'php':
378 return Types::TYPE_LANG;
379
380 case 'woff':
381 case 'woff2':
382 return Types::TYPE_FONT;
383
384 default:
385 return null;
386 }
387 }
388
389 protected static function checkFontLinkType(string $path): string
390 {
391 //woff2 must be before woff, because strpos find woff in woff2 ;)
392 $available = [
393 'woff2' => 'font/woff2',
394 'woff' => 'font/woff',
395 'ttf' => 'font/ttf',
396 'eot' => 'application/vnd.ms-fontobject',
397 'svg' => 'image/svg+xml',
398 ];
399
400 $linkType = '';
401 foreach ($available as $type => $value)
402 {
403 if (mb_strpos($path, $type) !== false)
404 {
405 $linkType = $value;
406 break;
407 }
408 }
409
410 return $linkType;
411 }
412
418 public function addString(string $string): void
419 {
420 $this->resources->addString(trim($string));
421 }
422
427 public function setOutput(int $lid = 0): void
428 {
429 if ($this->isSandbox)
430 {
431 return;
432 }
433
434 if ($lid === 0)
435 {
436 trigger_error(
437 "You must to pass ID of current landing to the \Bitrix\Landing\Assets\Manager::setOutput",
438 E_USER_WARNING
439 );
440 }
441 $this->createBuilder();
442 $this->builder->attachToLanding($lid);
443 $this->builder->setOutput();
444 }
445
451 public function getOutput(): array
452 {
453 $this->createBuilder();
454
455 return $this->builder->getOutput();
456 }
457
462 public function getStrings(): array
463 {
464 return $this->resources->getStrings();
465 }
466
471 protected function createBuilder(): void
472 {
473 $this->builder = Builder::createByType($this->resources, $this->mode);
474 }
475
479 public static function rebuildWebpack(): void
480 {
482 }
483
488 public static function rebuildWebpackForLanding($lid = []): void
489 {
491 }
492}
$path
Определения access_edit.php:21
$type
Определения options.php:106
static createByType(ResourceCollection $resources, string $type)
Определения builder.php:55
static verifyLocation($location)
Определения location.php:43
setStandartMode()
Определения manager.php:105
getRegisteredAssetLocation(string $code)
Определения manager.php:135
addString(string $string)
Определения manager.php:418
isAssetRegistered($code)
Определения manager.php:125
static rebuildWebpack()
Определения manager.php:479
addAsset(string|array $code, ?int $location=null)
Определения manager.php:168
processAsset(array $asset, int $location)
Определения manager.php:253
static checkFontLinkType(string $path)
Определения manager.php:389
enableSandbox()
Определения manager.php:84
const MODE_STANDART
Определения manager.php:21
const REGISTERED_KEY_CODE
Определения manager.php:24
isNeedAddAsset(string $code, int $location)
Определения manager.php:228
markAssetRegistered($code, $location)
Определения manager.php:148
createStringFromPath(string $path, string $type)
Определения manager.php:325
const MODE_WEBPACK
Определения manager.php:22
static detectType(string $path)
Определения manager.php:365
addAssetRecursive(string $code, $location=null)
Определения manager.php:190
static getInstance()
Определения manager.php:59
const REGISTERED_KEY_LOCATION
Определения manager.php:25
setOutput(int $lid=0)
Определения manager.php:427
setWebpackMode()
Определения manager.php:95
static rebuildWebpackForLanding($lid=[])
Определения manager.php:488
const TYPE_JS
Определения types.php:9
static getAssetTypes()
Определения types.php:19
const TYPE_FONT
Определения types.php:12
const TYPE_CSS
Определения types.php:8
const KEY_RELATIVE
Определения types.php:7
const TYPE_LANG
Определения types.php:10
static markToRebuild($lid)
Определения webpackfile.php:241
static markAllToRebuild()
Определения webpackfile.php:257
static getDocRoot()
Определения manager.php:180
static isExtensionLoaded($code)
Определения jscore.php:212
static markExtensionLoaded($code)
Определения jscore.php:190
static getExtInfo($ext)
Определения jscore.php:541
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения agent.php:3
$location
Определения options.php:2729