Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
converter.php
1<?php
2
4
8
15{
16 const CORE_EXTENSION = 'ui.webpacker';
17
18 protected static $hasCoreExtension = false;
19
26 public static function stringify(Builder $builder)
27 {
28 self::$hasCoreExtension = $builder->hasCoreExtension();
29 if (self::$hasCoreExtension)
30 {
31 $content = Json::encode([
32 'address' => Builder::getDefaultSiteUri()
33 ]);
34 $content = "var webPacker = $content;" . self::getEol();
35 }
36 else
37 {
38 $content = '';
39 }
40
41 foreach ($builder->getModules() as $module)
42 {
43 $moduleContent = self::encodeModule($module);
44 $content .= self::wrap($moduleContent) . self::getEol();
45 }
46
47 return self::wrap($content);
48 }
49
56 protected static function wrap($content)
57 {
58 return <<<EOD
59;(function () {
60$content
61})();
62EOD;
63
64 }
65
72 protected static function encodeModule(Module $module)
73 {
74 $name = $module->getName();
75 $content = '';
76 if (!self::isCoreExtension($name) && self::$hasCoreExtension)
77 {
78 $name = \CUtil::jsEscape($name);
79 $content = "var module = new webPacker.module('$name');" . self::getEol(1);
80
81 if ($module->getProfile())
82 {
83 $properties = $module->getProfile()->getProperties();
84 if (!empty($properties))
85 {
86 $properties = Json::encode($properties);
87 $content .= "module.setProperties($properties);" . self::getEol(1);
88 }
89 }
90 }
91
92 if ($module->getPackage())
93 {
94 $content .= self::encodePackage($module->getPackage(), $module->getProfile());
95 }
96 if ($module->getProfile())
97 {
98 $method = $module->getProfile()->getCallMethod();
99 if ($method)
100 {
101 $parameter = $module->getProfile()->getCallParameter();
102 $parameter = $parameter ? Json::encode($parameter) : '{}';
103 $content .= "$method($parameter);";
104 }
105 }
106
107 return $content;
108 }
109
117 protected static function encodePackage(Resource\Package $package, Resource\Profile $profile = null)
118 {
119 $content = '';
120 foreach ($package::getOrderedTypeList() as $type)
121 {
122 $assets = $package->getAssets($type);
123 if (empty($assets))
124 {
125 continue;
126 }
127
128 switch ($type)
129 {
130 case Resource\Asset::CSS:
131 case Resource\Asset::LAYOUT:
132 if (!self::$hasCoreExtension)
133 {
134 throw new InvalidOperationException("Resource of type `$type` not allowed without core extension.");
135 }
136
137 $resources = $list = Json::encode($package->toArray($type));
138 $content .= "module.loadResources($resources);" . self::getEol();
139 break;
140
141 case Resource\Asset::JS:
142 foreach ($assets as $asset)
143 {
144 $content .= $asset->getContent() . self::getEol();
145 }
146 break;
147
148 case Resource\Asset::LANG:
149 if (!self::$hasCoreExtension)
150 {
151 throw new InvalidOperationException("Resource of type `$type` not allowed without core extension.");
152 }
153
154 $messages = [];
155 $languages = [];
156 $language = ($profile ? $profile->getLanguage() : null) ?: Loc::getCurrentLang();
157 $isAllLangs = $profile ? $profile->isAllLangs() : false;
158 foreach ($assets as $asset)
159 {
161 $mess = $asset->useAllLangs($isAllLangs)->getContent();
162 if (!is_array($mess))
163 {
164 break;
165 }
166
167 if ($profile)
168 {
169 foreach ($mess as $messLanguage => $messList)
170 {
171 $messList = Resource\LangAsset::deletePrefixes(
172 $messList,
173 $profile->getDeleteLangPrefixes()
174 );
175 if ($profile->isLangCamelCase())
176 {
177 $messList = Resource\LangAsset::toCamelCase($messList);
178 }
179 if (!isset($messages[$messLanguage]) || !is_array($messages[$messLanguage]))
180 {
181 $messages[$messLanguage] = [];
182 }
183
184 $messages[$messLanguage] = array_merge(
185 $messages[$messLanguage],
186 $messList
187 );
188 }
189 }
190
191 $languages = array_unique(array_merge(
192 $languages,
193 array_keys($messages)
194 ));
195 }
196
197 if (count($messages) === 1)
198 {
199 $messages = current($messages);
200 }
201 $messages = Json::encode($messages);
202 $languages = Json::encode($languages);
203 $content .= "module.language = \"$language\";" . self::getEol(1);
204 $content .= "module.languages = $languages;" . self::getEol(1);
205 $content .= "module.messages = $messages;" . self::getEol();
206
207 break;
208 }
209 }
210
211 return $content;
212 }
213
214 protected static function getEol($multiplier = 2)
215 {
216 return str_repeat("\n", $multiplier);
217 }
218
219 protected static function isCoreExtension($name)
220 {
221 return self::CORE_EXTENSION === $name;
222 }
223}
static encodeModule(Module $module)
Definition converter.php:72
static stringify(Builder $builder)
Definition converter.php:26