Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
extension.php
1<?php
2
4
11
12class Extension extends Base
13{
14 protected static array $modificationDates = [];
15 protected static array $dependencies = [];
16 protected static $paths = [];
17 public ?string $result = null;
18
25 public function __construct($identifier)
26 {
27 $identifier = Path::normalize($identifier);
28 $this->baseFileName = 'extension';
29 $desc = Utils::extractEntityDescription($identifier);
30 $this->name = $desc['name'];
31 $this->namespace = $desc['namespace'];
32 if (isset(self::$paths[$desc['fullname']]))
33 {
34 $this->path = self::$paths[$desc['fullname']];
35 }
36 else
37 {
38 $this->path = Manager::getExtensionPath($identifier);
39 self::$paths[$desc['fullname']] = $this->path;
40 }
41
42 if (!$this->path)
43 {
44 throw new SystemException("Extension '{$desc['fullname']}' doesn't exists");
45 }
46 }
47
48 private function getBundleContent(): string
49 {
50 $files = $this->getBundleFiles();
51 $content = "";
52 foreach ($files as $path)
53 {
54 $file = new File($path);
55 if ($file->isExists())
56 {
57 $content .= "\n".$file->getContents()."\n";
58 }
59 }
60
61 return $content;
62 }
63
70 public function getContent($excludeResult = false): string
71 {
72 $content = "";
73 $extensionFile = new File($this->path . '/' . $this->baseFileName . '.js');
74 if ($excludeResult !== true) {
75 $content .= $this->getResultExpression();
76 }
77 else
78 {
79 $this->result = $this->getResult();
80 }
81
82 if ($extensionFile->isExists() && $extensionContent = $extensionFile->getContents())
83 {
84 $content .= $this->getBundleContent();
85 $content .= $extensionContent;
86 }
87
88 $content .= "\n\n";
89
90 return $content;
91 }
92
93 public function getResultExpression(): string {
94 $this->result = $this->getResult();
95 if ($this->result != null && $this->result !== '')
96 {
97 $name = ($this->namespace != "bitrix"? $this->namespace.":" : "").$this->name;
98 return <<<JS
99this.jnExtensionData.set("{$name}", {$this->result});
100JS;
101 }
102
103 return "";
104 }
105
106 private function getResult(): ?string
107 {
108 $file = new File($this->path . '/extension.php');
109 $result = null;
110 if ($file->isExists())
111 {
112 $result = include($file->getPath());
113 }
114
115 if (!empty($result) && is_array($result))
116 {
117 return Json::encode($result);
118 }
119
120 return null;
121 }
122
123 public function getIncludeExpression($callbackName = 'onExtensionsLoaded'): string
124 {
125 $relativePath = $this->getPath() . 'extension.js';
126 $localizationPhrases = $this->getLangDefinitionExpression();
127 $content = "\n//extension '{$this->name}'\n";
128 $content .= "{$localizationPhrases}\n";
129 $content .= "loadScript(\"{$relativePath}\", false, {$callbackName});";
130
131 return $content;
132 }
133
143 public static function getResolvedDependencyList($name, &$list = [], &$alreadyResolved = [], $margin = 0): array
144 {
145 $baseExtension = new Extension($name);
146 $depsList = $baseExtension->getDependencyList();
147 $alreadyResolved[] = $name;
148 if (!empty($depsList))
149 {
150 $margin++;
151 foreach ($depsList as $ext)
152 {
153 $depExtension = new Extension($ext);
154 $extDepsList = $depExtension->getDependencyList();
155 if (empty($extDepsList))
156 {
157 array_unshift($list, $ext);
158 }
159 elseif (!in_array($ext, $alreadyResolved))
160 {
161 self::getResolvedDependencyList($ext, $list, $alreadyResolved, $margin);
162 }
163 }
164 }
165
166 $list[] = $name;
167
168 return array_unique($list);
169 }
170
171 protected function onBeforeModificationMarkerSave(array &$value)
172 {
173 $files = $this->getBundleFiles();
174 foreach ($files as $path)
175 {
176 $file = new File($path);
177 if ($file->isExists())
178 {
179 $value[] = Utils::getFileHash($file);
180 }
181 }
182 }
183
188 protected function resolveDependencies(): array
189 {
190 $name = ($this->namespace !== "bitrix" ? $this->namespace . ":" : "") . $this->name;
191 return self::getResolvedDependencyList($name);
192 }
193
194 public function getDependencyList()
195 {
196 $fullName = "$this->namespace:$this->name";
197 if (isset(self::$dependencies[$fullName]))
198 {
199 return self::$dependencies[$fullName];
200 }
201 else
202 {
203 $list = parent::getDependencyList();
204 self::$dependencies[$fullName] = $list;
205 }
206
207 return self::$dependencies[$fullName];
208 }
209
210}
static getResolvedDependencyList($name, &$list=[], &$alreadyResolved=[], $margin=0)
getIncludeExpression($callbackName='onExtensionsLoaded')
static getFileHash(File $file)
Definition utils.php:45
static extractEntityDescription($entityIdentifier, $defaultNamespace="bitrix")
Definition utils.php:15