Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
14
15abstract class Base
16{
17 protected static array $modificationDates = [];
18 protected static array $dependencies = [];
19 protected static array $expandedDependencies = [];
20 protected $path;
21 protected $namespace;
22 protected $baseFileName;
23 public $name;
24 private $config;
25
26
27 protected function getConfig(): ?array {
28 if ($this->config == null) {
29 $this->config = [];
30 $file = new File("$this->path/deps.php");
31 $result = [];
32 if ($file->isExists())
33 {
34 $this->config = include($file->getPath());
35 if (!is_array($this->config)) {
36 $this->config = [];
37 }
38 }
39 }
40
41 return $this->config;
42 }
43
44 public function getModificationMarker()
45 {
46 if (defined("JN_DEV_RELOAD")) {
47 return "1.0";
48 }
49
50 $fullyQualifiedName = $this->getFullyQualifiedName();
51
52 if (!empty(static::$modificationDates[$fullyQualifiedName]))
53 {
54 return static::$modificationDates[$fullyQualifiedName];
55 }
56
57 $file = new File("{$this->path}/{$this->baseFileName}.js");
58 $marks = [Utils::getFileHash($file)];
59 $langDirectory = new Directory("{$this->path}/lang/");
60 if ($langDirectory->isExists())
61 {
62 $langs = $langDirectory->getChildren();
63 foreach ($langs as $lang)
64 {
65 if ($lang->isDirectory())
66 {
67 $langFile = new File($lang->getPath()."/{$this->baseFileName}.php");
68 $marks[] = Utils::getFileHash($langFile);
69 }
70 }
71 }
72
73 $this->onBeforeModificationMarkerSave($marks);
74 if (count($marks) == 1)
75 {
76 $value = $marks[0];
77 }
78 else
79 {
80 $value = md5(implode("/", $marks));
81 }
82
83 static::$modificationDates[$fullyQualifiedName] = $value;
84
85 return $value;
86 }
87
88 public function getFullyQualifiedName(): string {
89 return "$this->namespace:$this->name";
90 }
91
92 public function getDependencies()
93 {
94 if (!isset(static::$dependencies[$this->name]))
95 {
96 static::$dependencies[$this->name] = array_values($this->resolveDependencies());
97 }
98
99 return static::$dependencies[$this->name];
100 }
101
102 public function getPath()
103 {
104 $relativePath = str_replace(Application::getDocumentRoot(), "", "{$this->path}/");
105 return Path::normalize($relativePath);
106 }
107
108 public function getRelativePathToFile()
109 {
110 $relativePath = $this->getPath() . "/{$this->baseFileName}.js";
111 return Path::normalize($relativePath);
112 }
113
114 public function getLangMessages()
115 {
116 $langPhrases = Localization\Loc::loadLanguageFile("{$this->path}/{$this->baseFileName}.php");
117 return $langPhrases ?: [];
118 }
119
120 public function getDependencyList()
121 {
122 $config = $this->getConfig();
123 $list = [];
124 $result = [];
125 if (is_array($config))
126 {
127 if (array_keys($config) !== range(0, count($config) - 1))
128 {
129 if(array_key_exists('extensions', $config)) {
130 $list = $config['extensions'];
131 }
132 }
133 else
134 {
135 $list = $config;
136 }
137 }
138 if (!empty($list)) {
139 foreach ($list as $ext) {
140 $result[] = Base::expandDependency($ext);
141 }
142
143 if (!empty($result))
144 {
145 $result = array_merge(...$result);
146 }
147 }
148
149 return $result;
150 }
151
152 public function getBundleFiles(): array {
153 $config = $this->getConfig();
154 $list = [];
155 if (isset($config["bundle"])) {
156 $list = array_map(function ($file) {
157 $path = Path::normalize($this->path."/$file");
158 if (Path::getExtension($path) !== "js") {
159 $path .= ".js";
160 }
161 return $path;
162 }, $config["bundle"]);
163 }
164
165 return $list;
166 }
167
168 public function getComponentDependencies(): ?array
169 {
170 $config = $this->getConfig();
171 $result = [];
172 if (is_array($config))
173 {
174 if (array_keys($config) !== range(0, count($config) - 1)) {
175 if (isset($config['components'])) {
176 if (is_array($config['components'])) {
177 return $config['components'];
178 }
179 }
180 }
181 else
182 {
183 $result = null;
184 }
185
186 }
187
188 return $result;
189 }
190
196 private static function expandDependency($ext): array
197 {
198
199 $result = [];
200
201 if (!is_string($ext))
202 {
203 return [];
204 }
205
206 if ( isset(self::$expandedDependencies[$ext]))
207 {
208 return self::$expandedDependencies[$ext];
209 }
210
211 $findChildren = false;
212 $relativeExtDir = $ext;
213
214
215 if(mb_strpos($ext, "*") === (mb_strlen($ext) - 1))
216 {
217 $relativeExtDir = str_replace(["/*", "*"], "", $ext);
218 $findChildren = true;
219 }
220
221 $absolutePath = Manager::getExtensionPath($relativeExtDir);
222 if($findChildren && $absolutePath != null)
223 {
224 $dir = new Directory($absolutePath);
225 $items = $dir->getChildren();
226 for ($i = 0, $l = count($items); $i < $l; $i++)
227 {
229 $entry = $items[$i];
230 if ($entry->isDirectory())
231 {
232 $toAdd = $entry->getChildren();
233 $extensionFile = new File($entry->getPath(). '/extension.js');
234 if($extensionFile->isExists())
235 {
236 $result[] = $extensionFile->getPath();
237 }
238
239 $l += count($toAdd);
240 $items = array_merge($items, $toAdd);
241 }
242 }
243
244 $result = array_map(function($path) use ($absolutePath, $relativeExtDir) {
245 return str_replace([$absolutePath, "/extension.js"],[$relativeExtDir, ""], $path);
246 }, $result);
247 }
248
249 $rootExtension = new File($absolutePath . '/extension.js');
250 if($rootExtension->isExists())
251 {
252 $result[] = $relativeExtDir;
253 }
254
255 self::$expandedDependencies[$ext] = $result;
256 return $result;
257 }
258
260 {
261 $langPhrases = $this->getLangMessages();
262 if (!empty($langPhrases))
263 {
264 $jsonLangMessages = Utils::jsonEncode($langPhrases, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE);
265 return <<<JS
266BX.message($jsonLangMessages);
267JS;
268 }
269
270 return "";
271 }
272
273 abstract protected function onBeforeModificationMarkerSave(array &$value);
274 abstract protected function resolveDependencies();
275
276
277}
Definition fileentry.php:6
static getFileHash(File $file)
Definition utils.php:45
if(!function_exists(__NAMESPACE__.'\\___1034172934'))
Definition license.php:1