1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
component.php
См. документацию.
1<?php
2
3namespace Bitrix\MobileApp\Janative\Entity;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\ArgumentException;
7use Bitrix\Main\EventManager;
8use Bitrix\Main\IO\Directory;
9use Bitrix\Main\IO\File;
10use Bitrix\Main\IO\FileNotFoundException;
11use Bitrix\Main\IO\Path;
12use Bitrix\Main\Loader;
13use Bitrix\Main\LoaderException;
14use Bitrix\Main\ModuleManager;
15use Bitrix\Main\SystemException;
16use Bitrix\MobileApp\Janative\Manager;
17use Bitrix\MobileApp\Janative\Utils;
18use Bitrix\MobileApp\Mobile;
19use CExtranet;
20use CSite;
21use Exception;
22
23class Component extends Base
24{
25 const VERSION = 2;
26 protected static array $modificationDates = [];
27 protected static array $dependencies = [];
28 private $version = null;
29 public bool $isBundleEnabled = true;
30 private ?Config $bundleConfig = null;
31
38 public function __construct($path = null, $namespace = "bitrix")
39 {
41
42 $path = Path::normalize($path);
43 if (mb_strpos($path, Application::getDocumentRoot()) === 0)
44 {
45 $this->path = $path;
46 }
47 else
48 {
50 }
51
52 if (mb_substr($this->path, -1) != '/') //compatibility fix
53 {
54 $this->path .= '/';
55 }
56
57 $directory = new Directory($this->path);
58 $this->baseFileName = 'component';
59 $path = $directory->getPath() . '/' . $this->baseFileName . '.js';
60 $file = new File($path);
61 $this->name = $directory->getName();
62 $this->namespace = $namespace;
63
64 if (!$directory->isExists() || !$file->isExists())
65 {
66 throw new Exception("Component '{$this->name}' doesn't exists ($path) ");
67 }
68 }
69
70 public function getPath(): string
71 {
72 return str_replace(Application::getDocumentRoot(), '', $this->path);
73 }
74
81 public static function createInstanceByName($name, string $namespace = 'bitrix'): ?Component
82 {
84
85 return Manager::getComponentByName($info['defaultFullname']);
86 }
87
95 public function execute(bool $resultOnly = false)
96 {
97 header('Content-Type: text/javascript;charset=UTF-8');
98 header('BX-Component-Version: ' . $this->getVersion());
99 header('BX-Component: true');
100 if ($resultOnly)
101 {
102 echo Utils::jsonEncode($this->getResult());;
103 }
104 else
105 {
106 echo $this->getContent();
107 }
108 }
109
110 public function getResult(): ?array
111 {
112 $componentFile = new File($this->path . '/component.php');
113 if ($componentFile->isExists())
114 {
115 return include($componentFile->getPath());
116 }
117
118 return [];
119 }
120
121 public function shouldUseBundle(): bool
122 {
124 {
125 return $this->isBundleEnabled && $this->hasBundleConfig();
126 }
127
128 return false;
129 }
130
131 private function getBundleConfig(): Config
132 {
133 if ($this->bundleConfig === null)
134 {
135 $this->bundleConfig = new Config("{$this->path}/dist/deps.bundle.php");
136 }
137
138 return $this->bundleConfig;
139 }
140
141 public function hasBundleConfig(): bool
142 {
143 return $this->getBundleConfig()->exists();
144 }
145
146 private function getBundleDynamicData(): array
147 {
148 return $this->getBundleConfig()->dynamicData;
149 }
150
151 public function getContent(): string
152 {
153 if ($this->shouldUseBundle())
154 {
155 $extensionContent = "";
156 $availableComponents = "";
157
158 foreach ($this->getBundleDynamicData() as $ext)
159 {
160 $extension = Extension::getInstance($ext);
161 $extensionContent .= $extension->getResultExpression();
162 }
163
164 $componentFilePath = "{$this->path}/dist/{$this->baseFileName}.bundle.js";
165 }
166 else
167 {
168 $componentFilePath = "{$this->path}/{$this->baseFileName}.js";
169 $extensionContent = $this->getExtensionsContent();
170 $availableComponents = "this.availableComponents = " . Utils::jsonEncode($this->getComponentListInfo()) . ";";
171 }
172
174 $events = $eventManager->findEventHandlers("mobileapp", "onBeforeComponentContentGet");
175
176 $additionalContent = "";
177 if (!empty($events))
178 {
179 foreach ($events as $event)
180 {
181 $jsCode = ExecuteModuleEventEx($event, [$this]);
182 if (is_string($jsCode))
183 {
184 $additionalContent .= $jsCode;
185 }
186 }
187 }
188
189 $env = $this->getEnvContent();
191
192 $content = "
193 $env
194 $additionalContent
195 $lang
196 $availableComponents
197 $extensionContent
198 ";
199
200 $file = new File($componentFilePath);
201 if ($file->isExists())
202 {
203 $componentCode = $file->getContents();
204 $content .= "\n" . $componentCode;
205 }
206
207 return $content;
208 }
209
210 public function getEnvContent(): string
211 {
212 global $USER;
213
215 $object = Utils::jsonEncode($this->getInfo());
216
217 $isExtranetModuleInstalled = Loader::includeModule('extranet');
218
219 if ($isExtranetModuleInstalled)
220 {
221 $extranetSiteId = CExtranet::getExtranetSiteId();
222 if (!$extranetSiteId)
223 {
224 $isExtranetModuleInstalled = false;
225 }
226 }
227 $isExtranetUser = $isExtranetModuleInstalled && !CExtranet::IsIntranetUser();
228 $siteId = (
229 $isExtranetUser
230 ? $extranetSiteId
231 : SITE_ID
232 );
233
234 $siteDir = SITE_DIR;
235 if ($isExtranetUser)
236 {
237 $res = CSite::getById($siteId);
238 if (
239 ($extranetSiteFields = $res->fetch())
240 && ($extranetSiteFields['ACTIVE'] != 'N')
241 )
242 {
243 $siteDir = $extranetSiteFields['DIR'];
244 }
245 }
246
247 $installedModules = array_reduce(
249 static function ($modulesCollection, $module) {
250 $modulesCollection[$module['ID']] = true;
251
252 return $modulesCollection;
253 },
254 []
255 );
256 $userId = $USER->GetId();
257 $isAdmin = $USER->isAdmin();
258 if (!$isAdmin && Loader::includeModule("bitrix24"))
259 {
260 $isAdmin = \CBitrix24::IsPortalAdmin($userId);
261 }
262 $env = Utils::jsonEncode([
263 'siteId' => $siteId,
264 'isAdmin' => $isAdmin,
265 'languageId' => LANGUAGE_ID,
266 'siteDir' => $siteDir,
267 'userId' => $userId,
268 'extranet' => $isExtranetUser,
269 'isCollaber' => $this->isUserCollaber(),
270 'installedModules' => $installedModules,
271 ]);
272 $file = new File(Application::getDocumentRoot() . "/bitrix/js/mobileapp/platform.js");
273 $export = $file->getContents();
274 $inlineContent = <<<JS
275\n\n//-------- component '$this->name' ----------
276$export
277(()=>
278{
279 this.result = $result;
280 this.component = $object;
281 this.env = $env;
282})();
283
284JS;
285
286 return $inlineContent;
287 }
288
289 private function isUserCollaber(): bool
290 {
291 global $USER;
292 $userId = (int)$USER->GetID();
293
294 if (!Loader::includeModule('extranet'))
295 {
296 return false;
297 }
298
299 $container = class_exists(\Bitrix\Extranet\Service\ServiceContainer::class)
300 ? \Bitrix\Extranet\Service\ServiceContainer::getInstance()
301 : null;
302
303 return $container?->getCollaberService()?->isCollaberById($userId) ?? false;
304 }
305
306 public function getComponentListInfo(): array
307 {
308 $relativeComponents = $this->getComponentDependencies();
309 $componentScope = Manager::getAvailableComponents();
310 if ($relativeComponents !== null)
311 {
312 $relativeComponentsScope = [];
313 foreach ($relativeComponents as $scope)
314 {
315 if (isset($componentScope[$scope]))
316 {
317 $relativeComponentsScope[$scope] = $componentScope[$scope];
318 }
319 }
320
321 $componentScope = $relativeComponentsScope;
322 }
323
324 return array_map(function ($component) {
325 return $component->getInfo();
326 }, $componentScope);
327 }
328
329 public function getInfo(): array
330 {
331 return [
332 'path' => $this->getPath(),
333 'version' => $this->getVersion(),
334 'publicUrl' => $this->getPublicPath(),
335 'resultUrl' => $this->getPublicPath() . '&get_result=Y',
336 ];
337 }
338
339 protected function onBeforeModificationMarkerSave(array &$value)
340 {
341 $deps = $this->getDependencies();
342 foreach ($deps as $ext)
343 {
344 $extension = Extension::getInstance($ext);
345 $value[] = $extension->getModificationMarker();
346 }
347 }
348
349 public function getVersion(): string
350 {
351 if (!$this->version)
352 {
353 $this->version = "1";
354
355 if ($this->shouldUseBundle())
356 {
357 $bundleVersion = new File("{$this->path}/dist/version.bundle.php");
358 if ($bundleVersion->isExists())
359 {
360 $versionDesc = include($bundleVersion->getPath());
361 $this->version = $versionDesc['version'];
362 }
363 }
364 else
365 {
366 $versionFile = new File("{$this->path}/version.php");
367 if ($versionFile->isExists())
368 {
369 $versionDesc = include($versionFile->getPath());
370 $this->version = $versionDesc['version'];
371 $this->version .= '.' . self::VERSION;
372 }
373
374 $this->version .= '_' . $this->getModificationMarker();
375 }
376 }
377
378 return $this->version;
379 }
380
381 public function getPublicPath(): string
382 {
383 $name = ($this->namespace !== "bitrix" ? $this->namespace . ":" : "") . $this->name;
384 $name = urlencode($name);
385
386 return "/mobileapp/jn/$name/?version=" . $this->getVersion();
387 }
388
389 public function getLangMessages()
390 {
391 $langPhrases = parent::getLangMessages();
392 $extensions = $this->getDependencies();
393 foreach ($extensions as $extension)
394 {
395 try
396 {
397 $instance = Extension::getInstance($extension);
398 $extensionPhrases = $instance->getLangMessages();
399 $langPhrases = array_merge($langPhrases, $extensionPhrases);
400 }
401 catch (Exception $e)
402 {
403 //do nothing
404 }
405 }
406
407 return $langPhrases;
408 }
409
410 public function getDependencies()
411 {
412 if ($this->shouldUseBundle())
413 {
414 return (new Config("{$this->path}/dist/deps.bundle.php"))->extensions;
415 }
416
417 return parent::getDependencies();
418 }
419
420 public function getComponentDependencies(): ?array
421 {
422 $componentDependencies = parent::getComponentDependencies();
423 if (is_array($componentDependencies))
424 {
426
427 foreach ($dependencies as $dependency)
428 {
429 $list = (Extension::getInstance($dependency))->getComponentDependencies();
430 if ($list !== null)
431 {
432 $componentDependencies = array_merge($componentDependencies, $list);
433 }
434 }
435
436 return array_unique($componentDependencies);
437 }
438
439 return null;
440 }
441
445 public function resolveDependencies(): ?array
446 {
447 $rootDeps = $this->getDependencyList();
448 $deps = [];
449
450 array_walk($rootDeps, function ($ext) use (&$deps) {
451 $list = (Extension::getInstance($ext))->getDependencies();
452 $deps = array_merge($deps, $list);
453 });
454
455 return array_unique($deps);
456 }
457
458 public function getExtensionsContent($excludeResult = false): string
459 {
460 $content = "\n//extension '{$this->name}'\n";
461 $deps = $this->getDependencies();
462 foreach ($deps as $ext)
463 {
464 try
465 {
466 $extension = Extension::getInstance($ext);
467 $content .= "\n" . $extension->getContent($excludeResult);
468 }
469 catch (SystemException $e)
470 {
471 echo "Janative: error while initialization of '{$ext}' extension\n\n";
472 throw $e;
473 }
474 }
475 $loadedExtensions = "this.loadedExtensions = " . Utils::jsonEncode(array_values($deps), true) . ";\n";
476
477 return $loadedExtensions . $content;
478 }
479
480 public function setVersion(string $version = "1")
481 {
482 $this->version = $version;
483 }
484
485 private function isHotreloadEnabled(): bool
486 {
487 return (defined('JN_HOTRELOAD_ENABLED') && defined('JN_HOTRELOAD_HOST'));
488 }
489}
xml version
Определения yandex.php:67
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getDocumentRoot()
Определения application.php:736
static getInstance()
Определения eventmanager.php:31
static includeModule($moduleName)
Определения loader.php:67
static getInstalledModules()
Определения modulemanager.php:9
getLangDefinitionExpression()
Определения base.php:286
setVersion(string $version="1")
Определения component.php:480
onBeforeModificationMarkerSave(array &$value)
Определения component.php:339
getExtensionsContent($excludeResult=false)
Определения component.php:458
static array $dependencies
Определения component.php:27
execute(bool $resultOnly=false)
Определения component.php:95
static array $modificationDates
Определения component.php:26
__construct($path=null, $namespace="bitrix")
Определения component.php:38
static createInstanceByName($name, string $namespace='bitrix')
Определения component.php:81
static getInstance($identifier)
Определения extension.php:27
static getAvailableComponents()
Определения manager.php:185
static isBundleEnabled()
Определения manager.php:203
static getComponentByName($name)
Определения manager.php:195
static extractEntityDescription($entityIdentifier, string $defaultNamespace="bitrix")
Определения utils.php:16
static jsonEncode($string, $options=JSON_HEX_TAG|JSON_HEX_AMP|JSON_PRETTY_PRINT|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE)
Определения utils.php:47
static Init()
Определения mobile.php:199
$content
Определения commerceml.php:144
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
global $USER
Определения csv_new_run.php:40
const SITE_DIR(!defined('LANG'))
Определения include.php:72
if(!defined('SITE_ID')) $lang
Определения include.php:91
$siteId
Определения ajax.php:8
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения backup.php:924
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
Определения Image.php:9
$event
Определения prolog_after.php:141
$instance
Определения ps_b24_final.php:14
$eventManager
Определения include.php:412
const SITE_ID
Определения sonet_set_content_view.php:12
path
Определения template_copy.php:201