1C-Bitrix 25.700.0
extension.php
См. документацию.
1<?php
2
3namespace Bitrix\MobileApp\Janative\Entity;
4
5use Bitrix\Main\IO\File;
6use Bitrix\Main\IO\Path;
7use Bitrix\Main\SystemException;
8use Bitrix\Main\Web\Json;
9use Bitrix\MobileApp\Janative\Manager;
10use Bitrix\MobileApp\Janative\Utils;
11
12class Extension extends Base
13{
14 protected static array $modificationDates = [];
15 protected static array $dependencies = [];
16 protected static array $resolvedDependencies = [];
17 protected static array $extensionCache = [];
18 protected static $paths = [];
19 public ?string $result = null;
20
27 public static function getInstance($identifier): Extension
28 {
29 if (!isset(self::$extensionCache[$identifier]))
30 {
31 self::$extensionCache[$identifier] = new Extension($identifier);
32 }
33
34 return self::$extensionCache[$identifier];
35 }
36
37 public function __construct($identifier)
38 {
39 $identifier = Path::normalize($identifier);
40 $this->baseFileName = 'extension';
42 $this->name = $desc['name'];
43 $this->namespace = $desc['namespace'];
44 if (isset(self::$paths[$desc['fullname']]))
45 {
46 $this->path = self::$paths[$desc['fullname']];
47 }
48 else
49 {
50 $this->path = Manager::getExtensionPath($identifier);
51 self::$paths[$desc['fullname']] = $this->path;
52 }
53
54 if (!$this->path)
55 {
56 throw new SystemException("Extension '{$desc['fullname']}' doesn't exists");
57 }
58 }
59
60 private function getBundleContent(): string
61 {
62 $files = $this->getBundleFiles();
63 $content = "";
64 foreach ($files as $path)
65 {
66 $file = new File($path);
67 if ($file->isExists())
68 {
69 $content .= "\n".$file->getContents()."\n";
70 }
71 }
72
73 return $content;
74 }
75
82 public function getContent($excludeResult = false): string
83 {
84 $content = "";
85 $extensionFile = new File($this->path . '/' . $this->baseFileName . '.js');
86 if ($excludeResult !== true) {
87 $content .= $this->getResultExpression();
88 }
89 else
90 {
91 $this->result = $this->getResult();
92 }
93
94 if ($extensionFile->isExists() && $extensionContent = $extensionFile->getContents())
95 {
96 $content .= $this->getBundleContent();
97 $content .= $extensionContent;
98 }
99
100 $content .= "\n\n";
101
102 return $content;
103 }
104
105 public function getResultExpression(): string {
106 $this->result = $this->getResult();
107 if ($this->result != null && $this->result !== '')
108 {
109 $name = ($this->namespace != "bitrix"? $this->namespace.":" : "").$this->name;
110 return <<<JS
111this.jnExtensionData.set("{$name}", {$this->result});
112JS;
113 }
114
115 return "";
116 }
117
118 private function getResult(): ?string
119 {
120 $file = new File($this->path . '/extension.php');
121 $result = null;
122 if ($file->isExists())
123 {
124 $result = include($file->getPath());
125 }
126
127 if (!empty($result) && is_array($result))
128 {
129 return Json::encode($result);
130 }
131
132 return null;
133 }
134
135 public function getIncludeExpression($callbackName = 'onExtensionsLoaded'): string
136 {
137 $relativePath = $this->getPath() . 'extension.js';
138 $localizationPhrases = $this->getLangDefinitionExpression();
139 $content = "\n//extension '{$this->name}'\n";
140 $content .= "{$localizationPhrases}\n";
141 $content .= "loadScript(\"{$relativePath}\", false, {$callbackName});";
142
143 return $content;
144 }
145
155 public static function getResolvedDependencyList($name, &$list = [], &$alreadyResolved = [], $margin = 0): array
156 {
157 $baseExtension = Extension::getInstance($name);
158 $depsList = $baseExtension->getDependencyList();
159 $alreadyResolved[] = $name;
160 if (!empty($depsList))
161 {
162 $margin++;
163 foreach ($depsList as $ext)
164 {
165 $depExtension = Extension::getInstance($ext);
166 $extDepsList = $depExtension->getDependencyList();
167 if (empty($extDepsList))
168 {
169 array_unshift($list, $ext);
170 }
171 elseif (!in_array($ext, $alreadyResolved))
172 {
173 self::getResolvedDependencyList($ext, $list, $alreadyResolved, $margin);
174 }
175 }
176 }
177
178 $list[] = $name;
179
180 return array_unique($list);
181 }
182
183 protected function onBeforeModificationMarkerSave(array &$value)
184 {
185 $files = $this->getBundleFiles();
186 foreach ($files as $path)
187 {
188 $file = new File($path);
189 if ($file->isExists())
190 {
191 $value[] = Utils::getFileHash($file);
192 }
193 }
194 }
195
200 protected function resolveDependencies(): array
201 {
202 $name = ($this->namespace !== "bitrix" ? $this->namespace . ":" : "") . $this->name;
203 return self::getResolvedDependencyList($name);
204 }
205
206 public function getDependencies(): array
207 {
208 $fullyQualifiedName = $this->getFullyQualifiedName();
209 self::$resolvedDependencies[$fullyQualifiedName] ??= array_values($this->resolveDependencies());
210
211 return self::$resolvedDependencies[$fullyQualifiedName];
212 }
213
214 public function getDependencyList()
215 {
216 $fullyQualifiedName = $this->getFullyQualifiedName();
217 self::$dependencies[$fullyQualifiedName] ??= parent::getDependencyList();
218
219 return self::$dependencies[$fullyQualifiedName];
220 }
221}
$path
Определения access_edit.php:21
getLangDefinitionExpression()
Определения base.php:286
onBeforeModificationMarkerSave(array &$value)
Определения extension.php:183
static array $extensionCache
Определения extension.php:17
static getResolvedDependencyList($name, &$list=[], &$alreadyResolved=[], $margin=0)
Определения extension.php:155
static array $resolvedDependencies
Определения extension.php:16
getIncludeExpression($callbackName='onExtensionsLoaded')
Определения extension.php:135
static getInstance($identifier)
Определения extension.php:27
static array $dependencies
Определения extension.php:15
__construct($identifier)
Определения extension.php:37
static array $modificationDates
Определения extension.php:14
getContent($excludeResult=false)
Определения extension.php:82
static getExtensionPath($ext)
Определения manager.php:97
static getFileHash(File $file)
Определения utils.php:52
static extractEntityDescription($entityIdentifier, string $defaultNamespace="bitrix")
Определения utils.php:16
$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
Определения Image.php:9
$files
Определения mysql_to_pgsql.php:30
if(mb_strlen($order)< 6) $desc
Определения payment.php:44
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(CSalePaySystemAction::GetParamValue('BACKGROUND', false)) $margin
Определения html.php:61
path
Определения template_copy.php:201