Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
component.php
1<?
2
4
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 $isBundleEnabled = false;
30
36 public function __construct($path = null, $namespace = "bitrix")
37 {
38 Mobile::Init();
39
41 if (mb_strpos($path, Application::getDocumentRoot()) === 0)
42 {
43 $this->path = $path;
44 }
45 else
46 {
47 $this->path = Application::getDocumentRoot() . $path;
48 }
49
50 if (mb_substr($this->path, -1) != '/') //compatibility fix
51 {
52 $this->path .= '/';
53 }
54
55 $directory = new Directory($this->path);
56 $this->isBundleEnabled = isset($this->getConfig()["packer"]) ?? false;
57 $this->baseFileName = 'component';
58 $path = $directory->getPath() . '/'.$this->baseFileName.'.js';
59 $file = new File($path);
60 $this->name = $directory->getName();
61 $this->namespace = $namespace;
62
63 if (!$directory->isExists() || !$file->isExists())
64 {
65 throw new Exception("Component '{$this->name}' doesn't exists ($path) ");
66 }
67 }
68
69 public function getPath(): string
70 {
71 return str_replace(Application::getDocumentRoot(), '', $this->path);
72 }
73
80 public static function createInstanceByName($name, string $namespace = 'bitrix'): ?Component
81 {
82 $info = Utils::extractEntityDescription($name, $namespace);
83 return Manager::getComponentByName($info['defaultFullname']);
84 }
85
93 public function execute(bool $resultOnly = false)
94 {
95 header('Content-Type: text/javascript;charset=UTF-8');
96 header('BX-Component-Version: ' . $this->getVersion());
97 header('BX-Component: true');
98 if ($resultOnly)
99 {
100 echo Utils::jsonEncode($this->getResult());;
101 }
102 else
103 {
104 echo $this->getContent();
105 }
106 }
107
108
109 public function getResult(): ?array
110 {
111 $componentFile = new File($this->path . '/component.php');
112 if ($componentFile->isExists())
113 {
114 return include($componentFile->getPath());
115 }
116
117 return [];
118 }
119
120 public function getContent(): string
121 {
122 $env = $this->getEnvContent();
123 $lang = $this->getLangDefinitionExpression();
124 $componentFilePath = "{$this->path}/{$this->baseFileName}.js";
125 $extensionContent = "";
126 $availableComponents = "";
127 if ($this->isBundleEnabled)
128 {
129 $bundleConfig = new Config("{$this->path}/dist/deps.bundle.php");
130 foreach ($bundleConfig->dynamicData as $ext)
131 {
132 $extension = new Extension($ext);
133 $extensionContent .= $extension->getResultExpression();
134 }
135 $componentFilePath = "{$this->path}/dist/{$this->baseFileName}.bundle.js";
136 }
137 else
138 {
139 $extensionContent = $this->getExtensionsContent();
140 $availableComponents = "this.availableComponents = ".Utils::jsonEncode( $this->getComponentListInfo()).";";
141 }
142
143 $eventManager = EventManager::getInstance();
144 $events = $eventManager->findEventHandlers("mobileapp", "onBeforeComponentContentGet");
145
146 $additionalContent = "";
147 if (count($events) > 0)
148 {
149 foreach ($events as $event)
150 {
151 $jsCode = ExecuteModuleEventEx($event, [$this]);
152 if (is_string($jsCode)) {
153 $additionalContent .= $jsCode;
154 }
155
156 }
157 }
158
159 $content = "
160 $env
161 $additionalContent
162 $lang
163 $availableComponents
164 $extensionContent
165 ";
166
167 $file = new File($componentFilePath);
168 if ($file->isExists())
169 {
170 $componentCode = $file->getContents();
171 $content .= "\n" . $componentCode;
172 }
173
174 return $content;
175 }
176
177 public function getEnvContent(): string {
178 global $USER;
179
180 $result = Utils::jsonEncode($this->getResult());
181 $object = Utils::jsonEncode($this->getInfo());
182
183 $isExtranetModuleInstalled = Loader::includeModule('extranet');
184
185 if ($isExtranetModuleInstalled)
186 {
187 $extranetSiteId = CExtranet::getExtranetSiteId();
188 if (!$extranetSiteId)
189 {
190 $isExtranetModuleInstalled = false;
191 }
192 }
193 $isExtranetUser = $isExtranetModuleInstalled && !CExtranet::IsIntranetUser();
194 $siteId = (
195 $isExtranetUser
196 ? $extranetSiteId
197 : SITE_ID
198 );
199
200
201 $siteDir = SITE_DIR;
202 if ($isExtranetUser)
203 {
204 $res = CSite::getById($siteId);
205 if (
206 ($extranetSiteFields = $res->fetch())
207 && ($extranetSiteFields['ACTIVE'] != 'N')
208 )
209 {
210 $siteDir = $extranetSiteFields['DIR'];
211 }
212 }
213
214
215 $env = Utils::jsonEncode([
216 'siteId' => $siteId,
217 'languageId' => LANGUAGE_ID,
218 'siteDir' => $siteDir,
219 'userId' => $USER->GetId(),
220 'extranet' => $isExtranetUser
221 ]);
222 $file = new File(Application::getDocumentRoot()."/bitrix/js/mobileapp/platform.js");
223 $export = $file->getContents();
224 $inlineContent = <<<JS
225\n\n//-------- component '$this->name' ----------
226$export
227(()=>
228{
229 this.result = $result;
230 this.component = $object;
231 this.env = $env;
232})();
233
234JS;
235
236 return $inlineContent;
237 }
238
239 public function getComponentListInfo(): array {
240 $relativeComponents = $this->getComponentDependencies();
241 $componentScope = Manager::getAvailableComponents();
242 if ($relativeComponents !== null) {
243 $relativeComponentsScope = [];
244 foreach ($relativeComponents as $scope)
245 {
246 if (isset($componentScope[$scope])) {
247 $relativeComponentsScope[$scope] = $componentScope[$scope];
248 }
249 }
250
251 $componentScope = $relativeComponentsScope;
252 }
253
254 return array_map(function ($component) {
255 return $component->getInfo();
256 }, $componentScope);
257 }
258
259 public function getInfo(): array
260 {
261 return [
262 'path' => $this->getPath(),
263 'version' => $this->getVersion(),
264 'publicUrl' => $this->getPublicPath(),
265 'resultUrl' => $this->getPublicPath() . '&get_result=Y'
266 ];
267 }
268
269 protected function onBeforeModificationMarkerSave(array &$value)
270 {
271 $deps = $this->getDependencies();
272 foreach ($deps as $ext)
273 {
274 $extension = new Extension($ext);
275 $value[] = $extension->getModificationMarker();
276 }
277 }
278
279 public function getVersion(): string
280 {
281 $config = $this->getConfig();
282 if (!$this->version)
283 {
284 $this->version = "1";
285 if ( $this->isBundleEnabled )
286 {
287 $bundleVersion = new File("{$this->path}/dist/version.bundle.php");
288 if ($bundleVersion->isExists())
289 {
290 $versionDesc = include($bundleVersion->getPath());
291 $this->version = $versionDesc['version'];
292 }
293 }
294 else
295 {
296 $versionFile = new File("{$this->path}/version.php");
297 if ($versionFile->isExists())
298 {
299 $versionDesc = include($versionFile->getPath());
300 $this->version = $versionDesc['version'];
301 $this->version .= '.' . self::VERSION;
302 }
303
304 $this->version .= '_' . $this->getModificationMarker();
305 }
306 }
307
308 return $this->version;
309 }
310
311 public function getPublicPath(): string
312 {
313 $name = ($this->namespace !== "bitrix" ? $this->namespace . ":" : "") . $this->name;
314 $name = urlencode($name);
315 return "/mobileapp/jn/$name/?version=" . $this->getVersion();
316 }
317
318 public function getLangMessages()
319 {
320 $langPhrases = parent::getLangMessages();
321 $extensions = $this->getDependencies();
322 foreach ($extensions as $extension)
323 {
324 try {
325 $instance = new Extension($extension);
326 $extensionPhrases = $instance->getLangMessages();
327 $langPhrases = array_merge($langPhrases, $extensionPhrases);
328 }
329 catch (Exception $e)
330 {
331 //do nothing
332 }
333 }
334
335 return $langPhrases;
336 }
337
338 public function getDependencies()
339 {
340 if (!$this->isBundleEnabled ) {
341 return parent::getDependencies();
342 }
343 else
344 {
345 $bundleConfig = new Config("{$this->path}/dist/deps.bundle.php");
346 return $bundleConfig->extensions;
347 }
348 }
349
350 public function getComponentDependencies(): ?array
351 {
352 $componentDependencies = parent::getComponentDependencies();
353 if (is_array($componentDependencies)) {
354 $dependencies = $this->getDependencies();
355
356 foreach ($dependencies as $dependency)
357 {
358 $list = (new Extension($dependency))->getComponentDependencies();
359 if ($list !== null) {
360 $componentDependencies = array_merge($componentDependencies, $list);
361 }
362 }
363
364 return array_unique($componentDependencies);
365 }
366
367 return null;
368 }
369
373 public function resolveDependencies(): ?array
374 {
375 $rootDeps = $this->getDependencyList();
376 $deps = [];
377
378 array_walk($rootDeps, function ($ext) use (&$deps) {
379 $list = (new Extension($ext))->getDependencies();
380 $deps = array_merge($deps, $list);
381 });
382
383 return array_unique($deps);
384 }
385
386 public function getExtensionsContent($excludeResult = false): string
387 {
388 $content = "\n//extension '{$this->name}'\n";
389 $deps = $this->getDependencies();
390 foreach ($deps as $ext)
391 {
392 try
393 {
394 $extension = new Extension($ext);
395 $content .= "\n" . $extension->getContent($excludeResult);
396 } catch (SystemException $e)
397 {
398 echo "Janative: error while initialization of '{$ext}' extension\n\n";
399 throw $e;
400 }
401 }
402 $loadedExtensions = "this.loadedExtensions = ".Utils::jsonEncode(array_values($deps), true).";\n";
403 return $loadedExtensions.$content;
404 }
405
406 public function setVersion(string $version = "1")
407 {
408 $this->version = $version;
409 }
410
411 private function isHotreloadEnabled(): Bool {
412 return (defined('JN_HOTRELOAD_ENABLED') && defined('JN_HOTRELOAD_HOST'));
413 }
414}
static normalize($path)
Definition path.php:26
static includeModule($moduleName)
Definition loader.php:69
__construct($path=null, $namespace="bitrix")
Definition component.php:36
static createInstanceByName($name, string $namespace='bitrix')
Definition component.php:80