1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
extension.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\UI;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\IO\File;
7use Bitrix\Main\IO\Directory;
8use Bitrix\Main\IO\Path;
9
11{
16 public static function load($extNames)
17 {
18 if (!is_array($extNames))
19 {
20 $extNames = [$extNames];
21 }
22
23 foreach ($extNames as $extName)
24 {
25 if (static::register($extName))
26 {
27 \CJSCore::init($extName);
28 }
29 }
30 }
31
36 public static function register($extName)
37 {
38 if (!is_string($extName))
39 {
40 return false;
41 }
42
43 if (\CJSCore::isExtRegistered($extName))
44 {
45 return true;
46 }
47
48 if (preg_match('/[^a-z0-9_.-]/i', $extName))
49 {
50 return false;
51 }
52
53 $extension = static::getConfig($extName);
54 if ($extension !== null)
55 {
56 static::registerAssets($extName, $extension);
57
58 return true;
59 }
60
61 return false;
62 }
63
68 public static function registerAssets($id, array $options)
69 {
70 \CJSCore::registerExt($id, $options);
71 }
72
73 protected static function normalizeAssetPath($path, $extensionPath)
74 {
75 if (is_array($path))
76 {
77 $result = [];
78 foreach ($path as $item)
79 {
80 $result[] = static::normalizeAssetPath($item, $extensionPath);
81 }
82
83 return $result;
84 }
85
86 if (is_string($path) && $path !== '')
87 {
88 if (Path::isAbsolute($path))
89 {
90 return $path;
91 }
92
93 return Path::combine($extensionPath, $path);
94 }
95
96 return $path;
97 }
98
104 public static function getConfig($extName)
105 {
106 static $cache = [];
107
108 if (!is_string($extName))
109 {
110 return null;
111 }
112
113 if (isset($cache[$extName]) || array_key_exists($extName, $cache))
114 {
115 return $cache[$extName];
116 }
117
118 $extensionPath = static::getPath($extName);
119 if ($extensionPath === null)
120 {
121 $cache[$extName] = null;
122 return null;
123 }
124
125 $configFile = Application::getDocumentRoot() . $extensionPath . "/config.php";
126 if (!File::isFileExists($configFile))
127 {
128 $cache[$extName] = null;
129 return null;
130 }
131
132 $config = include($configFile);
133
134 if (is_array($config))
135 {
136 if (isset($config['js']))
137 {
138 $config['js'] = static::normalizeAssetPath($config['js'], $extensionPath);
139 }
140
141 if (isset($config['css']))
142 {
143 $config['css'] = static::normalizeAssetPath($config['css'], $extensionPath);
144 }
145
146 $langDirectory = Application::getDocumentRoot() . $extensionPath . '/lang/';
147
148 if (Directory::isDirectoryExists($langDirectory))
149 {
150 if (isset($config["lang"]))
151 {
152 $config["lang"] = static::normalizeAssetPath($config["lang"], $extensionPath);
153
154 if (is_array($config["lang"]))
155 {
156 $config["lang"][] = $extensionPath . "/config.php";
157 }
158 else
159 {
160 $config["lang"] = [$config["lang"], $extensionPath . "/config.php"];
161 }
162 }
163 else
164 {
165 $config["lang"] = $extensionPath . "/config.php";
166 }
167 }
168
169 if (!isset($config['settings']) || !is_array($config['settings']))
170 {
171 $config['settings'] = [];
172 }
173 }
174
175 $cache[$extName] = is_array($config) ? $config : null;
176
177 return $cache[$extName];
178 }
179
185 public static function getBundleConfig($extName)
186 {
187 $extensionPath = static::getPath($extName);
188
189 if ($extensionPath === null)
190 {
191 return null;
192 }
193
194 $configFilePath = Application::getDocumentRoot() . $extensionPath . "/bundle.config.js";
195 $configFile = new File($configFilePath);
196
197 if (!$configFile->isExists())
198 {
199 return null;
200 }
201
202 $namespace = '';
203 $configContent = $configFile->getContents();
204 if (is_string($configContent) && !empty($configContent))
205 {
206 preg_match('/namespace:(?:\s+)?[\'"](.*)[\'"]/', $configContent, $matches);
207 if (!empty($matches) && !empty($matches[1]))
208 {
209 $namespace = $matches[1];
210 }
211 }
212
213 return [
214 'namespace' => $namespace,
215 ];
216 }
217
218 private static function getPath($extName)
219 {
220 static $cache = [];
221
222 if (!is_string($extName))
223 {
224 return null;
225 }
226
227 if (isset($cache[$extName]) || array_key_exists($extName, $cache))
228 {
229 return $cache[$extName];
230 }
231
232 $namespaces = explode(".", $extName);
233 if (count($namespaces) < 2)
234 {
235 $cache[$extName] = null;
236 return null;
237 }
238
239 $path = "js";
240 foreach ($namespaces as $namespace)
241 {
242 if (!preg_match("/^[a-z0-9_.\-]+$/i", $namespace))
243 {
244 $cache[$extName] = null;
245 return null;
246 }
247
248 $path .= "/" . $namespace;
249 }
250
251 $localPath = \getLocalPath($path);
252
253 $cache[$extName] = is_string($localPath) && !empty($localPath) ? $localPath : null;
254
255 return $cache[$extName];
256 }
257
262 public static function getHtml($extName)
263 {
264 $isRegistered = static::register($extName);
265
266 if ($isRegistered)
267 {
268 return \CJSCore::getHTML($extName);
269 }
270
271 return null;
272 }
273
278 public static function getAssets($extName)
279 {
280 $assets = ['js' => [], 'css' => []];
281
282 if (is_array($extName))
283 {
284 foreach ($extName as $name)
285 {
286 $currentAssets = static::getAssets($name);
287 $assets['js'] = array_merge($assets['js'], $currentAssets['js']);
288 $assets['css'] = array_merge($assets['css'], $currentAssets['css']);
289 }
290
291 return $assets;
292 }
293
294 if (is_string($extName))
295 {
296 $config = static::getConfig($extName);
297
298 if (empty($config))
299 {
300 $config = \CJSCore::getExtInfo($extName);
301 }
302
303 if (isset($config['rel']))
304 {
305 $relAssets = static::getAssets($config['rel']);
306 $assets['js'] = array_merge($assets['js'], $relAssets['js']);
307 $assets['css'] = array_merge($assets['css'], $relAssets['css']);
308 }
309
310 if (isset($config['js']))
311 {
312 if (is_array($config['js']))
313 {
314 $assets['js'] = array_merge($assets['js'], $config['js']);
315 }
316
317 if (is_string($config['js']) && $config['js'] !== '')
318 {
319 $assets['js'][] = $config['js'];
320 }
321 }
322
323 if (isset($config['css']))
324 {
325 if (is_array($config['css']))
326 {
327 $assets['css'] = array_merge($assets['css'], $config['css']);
328 }
329
330 if (is_string($config['css']) && $config['css'] !== '')
331 {
332 $assets['css'][] = $config['css'];
333 }
334 }
335
336 if (isset($config['post_rel']))
337 {
338 $relAssets = static::getAssets($config['post_rel']);
339 $assets['js'] = array_merge($assets['js'], $relAssets['js']);
340 $assets['css'] = array_merge($assets['css'], $relAssets['css']);
341 }
342 }
343
344 $assets['js'] = array_unique($assets['js']);
345 $assets['css'] = array_unique($assets['css']);
346
347 return $assets;
348 }
349
354 public static function getDependencyList($extName)
355 {
356 return self::getDependencyListRecursive($extName);
357 }
358
364 public static function getResourceList($extName, $option = [])
365 {
366 $skipCoreJS = isset($option['skip_core_js']) && $option['skip_core_js'] === true;
367 $withDependency = !(isset($option['with_dependency']) && $option['with_dependency'] === false);
368 $skipExtensions = $option['skip_extensions'] ?? [];
369 $getResolvedExtensionList = isset($option['get_resolved_extension_list']) && $option['get_resolved_extension_list'] === true;
370
371 \CJSCore::init();
372
373 $alreadyResolved = $skipExtensions;
374 if ($skipCoreJS)
375 {
376 $alreadyResolved[] = 'core';
377 }
378
379 $extensions = [];
380 $extNameList = is_array($extName) ? $extName : [$extName];
381 if ($withDependency)
382 {
383 foreach ($extNameList as $extName)
384 {
385 if (in_array($extName, $alreadyResolved))
386 {
387 continue;
388 }
389
390 self::getDependencyListRecursive($extName, true, true, $extensions, $alreadyResolved);
391 }
392 }
393 else
394 {
395 foreach ($extNameList as $extName)
396 {
397 if (in_array($extName, $alreadyResolved))
398 {
399 continue;
400 }
401 else
402 {
403 $alreadyResolved[] = $extName;
404 }
405
406 $extensions[] = [$extName, self::getConfig($extName)];
407 }
408 }
409
410 foreach ($extensions as $index => $extension)
411 {
412 if (isset($extension[1]['oninit']) && is_callable($extension[1]['oninit']))
413 {
414 $callbackResult = call_user_func_array(
415 $extension[1]['oninit'],
416 [$extension[1]]
417 );
418
419 if (is_array($callbackResult))
420 {
421 foreach ($callbackResult as $option => $value)
422 {
423 if (!is_array($value))
424 {
425 $value = [$value];
426 }
427
428 if (!isset($extension[1][$option]))
429 {
430 $extension[1][$option] = [];
431 }
432 elseif (!is_array($extension[1][$option]))
433 {
434 $extension[1][$option] = [$extension[1][$option]];
435 }
436
437 $extensions[$index][1][$option] = array_merge($extension[1][$option], $value);
438 }
439 }
440
441 unset($extensions[$index][1]['oninit']);
442 }
443 }
444
445 $result = [
446 'js' => [],
447 'css' => [],
448 'lang' => [],
449 'lang_additional' => [],
450 'layout' => [],
451 'options' => [],
452 'settings' => [],
453 ];
454
455 if ($getResolvedExtensionList)
456 {
457 $result['resolved_extension'] = array_diff($alreadyResolved, $skipExtensions);
458 }
459
460 $options = array_keys($result);
461 foreach ($extensions as $extension)
462 {
463 $extensionName = $extension[0];
464 $config = $extension[1];
465
466 foreach ($options as $option)
467 {
468 if (is_array($config) && array_key_exists($option, $config))
469 {
470 if ($option === 'settings' && is_array($config[$option]) && !empty($config[$option]))
471 {
472 $result[$option][$extensionName] = $config[$option];
473 }
474 else
475 {
476 if (is_array($config[$option]))
477 {
478 $result[$option] = array_merge($result[$option], $config[$option]);
479 }
480 else
481 {
483 }
484 }
485 }
486 }
487 }
488
489 return $result;
490 }
491
500 private static function getDependencyListRecursive($name, $storeConfig = false, $storeSelf = false, &$resultList = [], &$alreadyResolved = [])
501 {
503 if ($config === null)
504 {
505 $namespaces = explode(".", $name);
506 if (count($namespaces) == 1)
507 {
508 $config = self::getCoreConfigForDependencyList($name, $storeConfig, $resultList, $alreadyResolved);
509 }
510 else
511 {
512 $alreadyResolved[] = $name;
513 }
514 }
515 else
516 {
517 $alreadyResolved[] = $name;
518 }
519
520 if ($config && !empty($config['rel']))
521 {
522 foreach ($config['rel'] as $dependencyName)
523 {
524 if (!in_array($dependencyName, $alreadyResolved))
525 {
526 self::getDependencyListRecursive($dependencyName, $storeConfig, true, $resultList, $alreadyResolved);
527 }
528 }
529 }
530
531 if ($storeSelf)
532 {
533 if ($storeConfig)
534 {
535 $resultList[] = [$name, $config];
536 }
537 else
538 {
539 $resultList[] = $name;
540 }
541 }
542
543 if ($config && !empty($config['post_rel']))
544 {
545 foreach ($config['post_rel'] as $dependencyName)
546 {
547 if (!in_array($dependencyName, $alreadyResolved))
548 {
549 self::getDependencyListRecursive($dependencyName, $storeConfig, true, $resultList, $alreadyResolved);
550 }
551 }
552 }
553
554 if (
555 $storeConfig
556 && ($config && !empty($config['rel']) || $storeSelf)
557 )
558 {
559 $uniqueArray = [];
560 foreach ($resultList as $element)
561 {
562 $uniqueArray[$element[0]] = $element;
563 }
564 $resultList = array_values($uniqueArray);
565 }
566 else
567 {
568 $resultList = array_unique($resultList);
569 }
570
571 return $resultList;
572 }
573
574 private static function getCoreConfigForDependencyList($name, $storeConfig = false, &$resultList = [], &$alreadyResolved = [])
575 {
576 $alreadyResolved[] = $name;
577
578 $config = \CJSCore::getExtInfo($name);
579 if ($config)
580 {
581 if (!$config['skip_core'] && !in_array('core', $alreadyResolved))
582 {
583 $coreConfig = \CJSCore::GetCoreConfig();
584 if ($storeConfig)
585 {
586 array_unshift($resultList, ['core', $coreConfig]);
587 }
588 else
589 {
590 array_unshift($resultList, 'core');
591 }
592
593 $alreadyResolved[] = 'core';
594 }
595 }
596 else
597 {
598 $config = null;
599 }
600
601 return $config;
602 }
603}
$path
Определения access_edit.php:21
static getDocumentRoot()
Определения application.php:736
static registerAssets($id, array $options)
Определения extension.php:68
static load($extNames)
Определения extension.php:16
static getDependencyList($extName)
Определения extension.php:354
static getAssets($extName)
Определения extension.php:278
static getBundleConfig($extName)
Определения extension.php:185
static getHtml($extName)
Определения extension.php:262
static normalizeAssetPath($path, $extensionPath)
Определения extension.php:73
static getConfig($extName)
Определения extension.php:104
static getResourceList($extName, $option=[])
Определения extension.php:364
static getExtInfo($ext)
Определения jscore.php:541
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
Определения Image.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$option
Определения options.php:1711
$matches
Определения index.php:22
$localPath
Определения template_copy.php:184