Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
builder.php
1<?php
2
4
9
16{
18 protected $output;
19
22
24 private $modules = [Converter::CORE_EXTENSION];
25
27 private $extensions = [];
28
30 protected static $siteUri;
31
37 public function build()
38 {
39 return $this->getOutput()->output($this);
40 }
41
48 public function setOutput(Output\Base $output)
49 {
50 $this->output = $output;
51
52 return $this;
53 }
54
60 public function getOutput()
61 {
62 if (!$this->output)
63 {
64 $this->output = new Output\Base();
65 }
66
67 return $this->output;
68 }
69
76 public function addModule(Module $module)
77 {
78 $this->modules[] = $module;
79 return $this;
80 }
81
89 public function addExtension($name, $appendDependencies = true)
90 {
91 $list = $appendDependencies ? Extension::getDependencyList($name) : [];
92 if (!in_array($name, $list))
93 {
94 $list[] = $name;
95 }
96
97 $this->extensions = array_unique(array_merge($this->extensions, $list));
98
99 $list = array_diff($list, $this->modules);
100 $list = array_filter(
101 $list,
102 function ($item)
103 {
104 return $item !== Converter::CORE_EXTENSION;
105 }
106 );
107 $this->modules = array_merge($this->modules, $list);
108
109 return $this;
110 }
111
117 public function hasCoreExtension()
118 {
119 return in_array(Converter::CORE_EXTENSION, $this->extensions);
120 }
121
122 private function convertExtensionsToModules()
123 {
124 foreach ($this->modules as $index => $module)
125 {
126 if ($module === 'main.core')
127 {
128 $module .= '.minimal';
129 }
130
131 if ($module === Converter::CORE_EXTENSION && !$this->hasCoreExtension())
132 {
133 continue;
134 }
135
136 if (!is_string($module))
137 {
138 continue;
139 }
140
141 $name = $module;
142 $extension = Extension::getResourceList($name, ['with_dependency' => false]);
143
144 $package = (new Resource\Package());
145 $profile = (new Resource\Profile());
146 foreach ($extension as $key => $values)
147 {
148 if ($key === 'options')
149 {
150 $this->applyExtensionOptions($profile, $values);
151 continue;
152 }
153
154 if (!in_array($key, Resource\Asset::getTypeList()))
155 {
156 continue;
157 }
158
159 foreach ($values as $path)
160 {
161 $type = Resource\Asset::detectType($path);
162 if (!$type)
163 {
164 continue;
165 }
166
167 $package->addAsset(Resource\Asset::create($path));
168 }
169 }
170
171 $this->modules[$index] = new Module($name, $package, $profile);
172 }
173
174 return $this;
175 }
176
183 public function useMinification($use)
184 {
185 $this->useMinification = $use;
186 return $this;
187 }
188
196 protected function applyExtensionOptions($profile, $extensionOptions)
197 {
198 if (!is_array($extensionOptions))
199 {
200 return;
201 }
202
203 $webPacker = self::getValueByKey($extensionOptions, Resource\Profile::WEBPACKER);
204 $properties = self::getValueByKey($webPacker, Resource\Profile::PROPERTIES);
205 if (is_array($properties))
206 {
207 foreach ($properties as $propertyName => $propertyValue)
208 {
209 $profile->setProperty($propertyName, $propertyValue);
210 }
211 }
212 $profile->useAllLangs(!!self::getValueByKey($webPacker, Resource\Profile::USE_ALL_LANGS));
213 $profile->useLangCamelCase(!!self::getValueByKey($webPacker, Resource\Profile::USE_LANG_CAMEL_CASE));
214 $deleteLangPrefixes = self::getValueByKey($webPacker, Resource\Profile::DELETE_LANG_PREFIXES);
215 if (is_array($deleteLangPrefixes))
216 {
217 $profile->deleteLangPrefixes($deleteLangPrefixes);
218 }
219
220 $callMethod = self::getValueByKey($webPacker, Resource\Profile::CALL_METHOD);
221 if ($callMethod)
222 {
223 $profile->setCallMethod($callMethod);
224 }
225 }
226
227 protected function getValueByKey($array, $key)
228 {
229 if (!is_array($array))
230 {
231 return null;
232 }
233
234 return (isset($array[$key]) && $array[$key]) ? $array[$key] : null;
235 }
236
242 public function getModules()
243 {
244 $this->convertExtensionsToModules();
245 return array_filter(
246 $this->modules,
247 function ($module)
248 {
249 return $module instanceof Module;
250 }
251 );
252 }
253
260 public function getModule($name)
261 {
262 foreach ($this->getModules() as $module)
263 {
264 if ($module->getName() === $name)
265 {
266 return $module;
267 }
268 }
269
270 return null;
271 }
272
278 public function stringify()
279 {
280 return Converter::stringify($this);
281 }
282
288 public static function getDefaultSiteUri()
289 {
290 if (self::$siteUri)
291 {
292 return self::$siteUri;
293 }
294
295 $url = Config\Configuration::getValue('main.webpacker')['last_site_url'] ?? '';
296 if ($url)
297 {
298 self::$siteUri = $url;
299 return $url;
300 }
301
302 $server = Context::getCurrent()->getServer();
303 $url = $server->getHttpHost();
304
305 $canSave = !empty($url) && (!defined('BX_CRONTAB') || !BX_CRONTAB);
306 $isRestored = false;
307
308 if (!$url)
309 {
310 $url = Config\Option::get('main', 'last_site_url', null);
311 if ($url)
312 {
313 $isRestored = true;
314 }
315 else
316 {
317 $url = Config\Option::get('main', 'server_name', null);
318 $url = $url ?: $server->getServerName();
319 if (!$url)
320 {
321 $defaultSites = \CSite::getDefList();
322 while($defaultSite = $defaultSites->fetch())
323 {
324 $url = $defaultSite['SERVER_NAME'];
325 if ($url)
326 {
327 break;
328 }
329 }
330 }
331 }
332 }
333
334 if (!$isRestored)
335 {
336 if (strpos($url, ':') === false && $server->getServerPort())
337 {
338 if (!in_array($server->getServerPort(), array('80', '443')))
339 {
340 $url .= ':' . $server->getServerPort();
341 }
342 }
343
344 $url = (Context::getCurrent()->getRequest()->isHttps() ? "https" : "http")
345 . "://" . $url;
346 }
347
348 $uri = new Uri($url);
349 $url = $uri->getLocator();
350 if (mb_substr($url, -1) == '/')
351 {
352 $url = mb_substr($url, 0, -1);
353 }
354
355 if ($canSave)
356 {
357 Config\Option::set('main', 'last_site_url', $url);
358 }
359
360 self::$siteUri = $url;
361 return $url;
362 }
363}
static getCurrent()
Definition context.php:241
addExtension($name, $appendDependencies=true)
Definition builder.php:89
setOutput(Output\Base $output)
Definition builder.php:48
applyExtensionOptions($profile, $extensionOptions)
Definition builder.php:196
static stringify(Builder $builder)
Definition converter.php:26