Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
extension.php
1<?
2
3namespace Bitrix\Main\UI;
4
9
11{
17 public static function load($extNames)
18 {
19 if (!is_array($extNames))
20 {
21 $extNames = [$extNames];
22 }
23
24 foreach ($extNames as $extName)
25 {
26 if (static::register($extName))
27 {
28 \CJSCore::init($extName);
29 }
30 }
31 }
32
38 public static function register($extName)
39 {
40 if (\CJSCore::isExtRegistered($extName))
41 {
42 return true;
43 }
44
45 $extension = static::getConfig($extName);
46 if ($extension !== null)
47 {
48 static::registerAssets($extName, $extension);
49
50 return true;
51 }
52 return \CJSCore::isExtRegistered($extName);
53 }
54
59 public static function registerAssets($id, array $options)
60 {
61 \CJSCore::registerExt($id, $options);
62 }
63
64 protected static function normalizeAssetPath($path, $extensionPath)
65 {
66 if (is_array($path))
67 {
68 $result = [];
69 foreach ($path as $key => $item)
70 {
71 $result[] = static::normalizeAssetPath($item, $extensionPath);
72 }
73
74 return $result;
75 }
76
77 if (is_string($path) && $path !== '')
78 {
79 if (Path::isAbsolute($path))
80 {
81 return $path;
82 }
83
84 return Path::combine($extensionPath, $path);
85 }
86
87 return $path;
88 }
89
95 public static function getConfig($extName)
96 {
97 $extensionPath = static::getPath($extName);
98 if ($extensionPath === null)
99 {
100 return null;
101 }
102
103 $configFile = Application::getDocumentRoot().$extensionPath."/config.php";
104 if (!File::isFileExists($configFile))
105 {
106 return null;
107 }
108
109 $config = include($configFile);
110
111 if (is_array($config))
112 {
113 if (isset($config['js']))
114 {
115 $config['js'] = static::normalizeAssetPath($config['js'], $extensionPath);
116 }
117
118 if (isset($config['css']))
119 {
120 $config['css'] = static::normalizeAssetPath($config['css'], $extensionPath);
121 }
122
123 $langDirectory = Application::getDocumentRoot().$extensionPath.'/lang/';
124
125 if (Directory::isDirectoryExists($langDirectory))
126 {
127 if (isset($config["lang"]))
128 {
129 $config["lang"] = static::normalizeAssetPath($config["lang"], $extensionPath);
130
131 if (is_array($config["lang"]))
132 {
133 $config["lang"][] = $extensionPath."/config.php";
134 }
135 else
136 {
137 $config["lang"] = [$config["lang"], $extensionPath."/config.php"];
138 }
139 }
140 else
141 {
142 $config["lang"] = $extensionPath."/config.php";
143 }
144 }
145
146 if (!isset($config['settings']) || !is_array($config['settings']))
147 {
148 $config['settings'] = [];
149 }
150 }
151
152
153 return is_array($config) ? $config : null;
154 }
155
161 public static function getBundleConfig($extName)
162 {
163 $extensionPath = static::getPath($extName);
164
165 if ($extensionPath === null)
166 {
167 return null;
168 }
169
170 $configFilePath = Application::getDocumentRoot().$extensionPath."/bundle.config.js";
171 $configFile = new File($configFilePath);
172
173 if (!$configFile->isExists())
174 {
175 return null;
176 }
177
178 $fileContent = str_replace("module.exports = ", "", $configFile->getContents());
179 $fileContent = str_replace("};", "}", $fileContent);
180
181 return \CUtil::JsObjectToPhp($fileContent);
182 }
183
184 private static function getPath($extName)
185 {
186 if (!is_string($extName))
187 {
188 return null;
189 }
190
191 $namespaces = explode(".", $extName);
192 if (count($namespaces) < 2)
193 {
194 return null;
195 }
196
197 $path = "js";
198 foreach ($namespaces as $namespace)
199 {
200 if (!preg_match("/^[a-z0-9_\\.\\-]+$/i", $namespace))
201 {
202 return null;
203 }
204
205 $path .= "/".$namespace;
206 }
207
208 $localPath = \getLocalPath($path);
209
210 return is_string($localPath) && !empty($localPath) ? $localPath : null;
211 }
212
218 public static function getHtml($extName)
219 {
220 $isRegistered = static::register($extName);
221
222 if ($isRegistered)
223 {
224 return \CJSCore::getHTML($extName);
225 }
226
227 return null;
228 }
229
234 public static function getAssets($extName)
235 {
236 $assets = ['js' => [], 'css' => []];
237
238 if (is_array($extName))
239 {
240 foreach ($extName as $key => $name)
241 {
242 $currentAssets = static::getAssets($name);
243 $assets['js'] = array_merge($assets['js'], $currentAssets['js']);
244 $assets['css'] = array_merge($assets['css'], $currentAssets['css']);
245 }
246
247 return $assets;
248 }
249
250 if (is_string($extName))
251 {
252 $config = static::getConfig($extName);
253
254 if (empty($config))
255 {
256 $config = \CJSCore::getExtInfo($extName);
257 }
258
259 if (isset($config['rel']))
260 {
261 $relAssets = static::getAssets($config['rel']);
262 $assets['js'] = array_merge($assets['js'], $relAssets['js']);
263 $assets['css'] = array_merge($assets['css'], $relAssets['css']);
264 }
265
266 if (isset($config['js']))
267 {
268 if (is_array($config['js']))
269 {
270 $assets['js'] = array_merge($assets['js'], $config['js']);
271 }
272
273 if (is_string($config['js']) && $config['js'] !== '')
274 {
275 $assets['js'][] = $config['js'];
276 }
277 }
278
279 if (isset($config['css']))
280 {
281 if (is_array($config['css']))
282 {
283 $assets['css'] = array_merge($assets['css'], $config['css']);
284 }
285
286 if (is_string($config['css']) && $config['css'] !== '')
287 {
288 $assets['css'][] = $config['css'];
289 }
290 }
291
292 if (isset($config['post_rel']))
293 {
294 $relAssets = static::getAssets($config['post_rel']);
295 $assets['js'] = array_merge($assets['js'], $relAssets['js']);
296 $assets['css'] = array_merge($assets['css'], $relAssets['css']);
297 }
298 }
299
300 $assets['js'] = array_unique($assets['js']);
301 $assets['css'] = array_unique($assets['css']);
302
303 return $assets;
304 }
305
310 public static function getDependencyList($extName)
311 {
312 return self::getDependencyListRecursive($extName);
313 }
314
320 public static function getResourceList($extName, $option = [])
321 {
322 $skipCoreJS = isset($option['skip_core_js']) && $option['skip_core_js'] === true;
323 $withDependency = !(isset($option['with_dependency']) && $option['with_dependency'] === false);
324 $skipExtensions = $option['skip_extensions'] ?? [];
325 $getResolvedExtensionList = isset($option['get_resolved_extension_list']) && $option['get_resolved_extension_list'] === true;
326
327 \CJSCore::init();
328
329 $alreadyResolved = $skipExtensions;
330 if ($skipCoreJS)
331 {
332 $alreadyResolved[] = 'core';
333 }
334
335 $extensions = [];
336 if ($withDependency)
337 {
338 $extNameList = is_array($extName)? $extName: [$extName];
339 foreach ($extNameList as $extName)
340 {
341 if (in_array($extName, $alreadyResolved))
342 {
343 continue;
344 }
345
346 self::getDependencyListRecursive($extName, true, true, $extensions, $alreadyResolved);
347 }
348 }
349 else
350 {
351 $extNameList = is_array($extName)? $extName: [$extName];
352 foreach ($extNameList as $extName)
353 {
354 if (in_array($extName, $alreadyResolved))
355 {
356 continue;
357 }
358 else
359 {
360 $alreadyResolved[] = $extName;
361 }
362
363 $extensions[] = [$extName, self::getConfig($extName)];
364 }
365 }
366
367 foreach ($extensions as $index => $extension)
368 {
369 if(isset($extension[1]['oninit']) && is_callable($extension[1]['oninit']))
370 {
371 $callbackResult = call_user_func_array(
372 $extension[1]['oninit'],
373 array($extension[1])
374 );
375
376 if(is_array($callbackResult))
377 {
378 foreach($callbackResult as $option => $value)
379 {
380 if(!is_array($value))
381 {
382 $value = array($value);
383 }
384
385 if(!isset($extension[1][$option]))
386 {
387 $extension[1][$option] = array();
388 }
389 elseif(!is_array($extension[1][$option]))
390 {
391 $extension[1][$option] = array($extension[1][$option]);
392 }
393
394 $extensions[$index][1][$option] = array_merge($extension[1][$option], $value);
395 }
396 }
397
398 unset($extensions[$index][1]['oninit']);
399 }
400 }
401
402 $result = [
403 'js' => [],
404 'css' => [],
405 'lang' => [],
406 'lang_additional' => [],
407 'layout' => [],
408 'options' => [],
409 'settings' => [],
410 ];
411
412 if ($getResolvedExtensionList)
413 {
414 $result['resolved_extension'] = array_diff($alreadyResolved, $skipExtensions);
415 }
416
417 $options = array_keys($result);
418 foreach ($extensions as $extension)
419 {
420 $extensionName = $extension[0];
421 $config = $extension[1];
422
423 foreach ($options as $option)
424 {
425 if (is_array($config) && array_key_exists($option, $config))
426 {
427 if ($option === 'settings' && is_array($config[$option]) && !empty($config[$option]))
428 {
429 $result[$option][$extensionName] = $config[$option];
430 }
431 else if (is_array($config[$option]))
432 {
433 $result[$option] = array_merge($result[$option], $config[$option]);
434 }
435 else
436 {
437 $result[$option][] = $config[$option];
438 }
439 }
440 }
441 }
442
443 return $result;
444 }
445
454 private static function getDependencyListRecursive($name, $storeConfig = false, $storeSelf = false, &$resultList = [], &$alreadyResolved = [])
455 {
456 $config = self::getConfig($name);
457 if ($config === null)
458 {
459 $namespaces = explode(".", $name);
460 if (count($namespaces) == 1)
461 {
462 $config = self::getCoreConfigForDependencyList($name, $storeConfig, $resultList, $alreadyResolved);
463 }
464 else
465 {
466 $alreadyResolved[] = $name;
467 }
468 }
469 else
470 {
471 $alreadyResolved[] = $name;
472 }
473
474 if ($config && !empty($config['rel']))
475 {
476 foreach ($config['rel'] as $dependencyName)
477 {
478 if (!in_array($dependencyName, $alreadyResolved))
479 {
480 self::getDependencyListRecursive($dependencyName, $storeConfig, true, $resultList, $alreadyResolved);
481 }
482 }
483 }
484
485 if ($storeSelf)
486 {
487 if ($storeConfig)
488 {
489 $resultList[] = [$name, $config];
490 }
491 else
492 {
493 $resultList[] = $name;
494 }
495 }
496
497 if ($config && !empty($config['post_rel']))
498 {
499 foreach ($config['post_rel'] as $dependencyName)
500 {
501 if (!in_array($dependencyName, $alreadyResolved))
502 {
503 self::getDependencyListRecursive($dependencyName, $storeConfig, true, $resultList, $alreadyResolved);
504 }
505 }
506 }
507
508 if (
509 $storeConfig
510 && ($config && !empty($config['rel']) || $storeSelf)
511 )
512 {
513 $uniqueArray = [];
514 foreach ($resultList as $element)
515 {
516 $uniqueArray[$element[0]] = $element;
517 }
518 $resultList = array_values($uniqueArray);
519 }
520 else
521 {
522 $resultList = array_unique($resultList);
523 }
524
525 return $resultList;
526 }
527
528 private static function getCoreConfigForDependencyList($name, $storeConfig = false, &$resultList = [], &$alreadyResolved = [])
529 {
530 $alreadyResolved[] = $name;
531
532 $config = \CJSCore::getExtInfo($name);
533 if ($config)
534 {
535 if (!$config['skip_core'] && !in_array('core', $alreadyResolved))
536 {
537 $coreConfig = \CJSCore::GetCoreConfig();
538 if ($storeConfig)
539 {
540 array_unshift($resultList, ['core', $coreConfig]);
541 }
542 else
543 {
544 array_unshift($resultList, 'core');
545 }
546
547 $alreadyResolved[] = 'core';
548 }
549 }
550 else
551 {
552 $config = null;
553 }
554
555 return $config;
556 }
557}
static isDirectoryExists($path)
static registerAssets($id, array $options)
Definition extension.php:59
static load($extNames)
Definition extension.php:17
static getDependencyList($extName)
static getAssets($extName)
static getBundleConfig($extName)
static getHtml($extName)
static normalizeAssetPath($path, $extensionPath)
Definition extension.php:64
static getConfig($extName)
Definition extension.php:95
static getResourceList($extName, $option=[])