29 if (self::$hasCoreExtension)
31 $content = Json::encode([
32 'address' => Builder::getDefaultSiteUri()
34 $content =
"var webPacker = $content;" . self::getEol();
43 $moduleContent = self::encodeModule($module);
44 $content .= self::wrap($moduleContent) . self::getEol();
47 return self::wrap($content);
56 protected static function wrap($content)
76 if (!self::isCoreExtension($name) && self::$hasCoreExtension)
78 $name = \CUtil::jsEscape($name);
79 $content =
"var module = new webPacker.module('$name');" . self::getEol(1);
83 $properties = $module->
getProfile()->getProperties();
84 if (!empty($properties))
86 $properties = Json::encode($properties);
87 $content .=
"module.setProperties($properties);" . self::getEol(1);
98 $method = $module->
getProfile()->getCallMethod();
101 $parameter = $module->
getProfile()->getCallParameter();
102 $parameter = $parameter ? Json::encode($parameter) :
'{}';
103 $content .=
"$method($parameter);";
117 protected static function encodePackage(Resource\Package $package, Resource\Profile $profile =
null)
120 foreach ($package::getOrderedTypeList() as $type)
122 $assets = $package->getAssets($type);
130 case Resource\Asset::CSS:
131 case Resource\Asset::LAYOUT:
132 if (!self::$hasCoreExtension)
137 $resources = $list = Json::encode($package->toArray($type));
138 $content .=
"module.loadResources($resources);" . self::getEol();
141 case Resource\Asset::JS:
142 foreach ($assets as $asset)
144 $content .= $asset->getContent() . self::getEol();
148 case Resource\Asset::LANG:
149 if (!self::$hasCoreExtension)
151 throw new InvalidOperationException(
"Resource of type `$type` not allowed without core extension.");
156 $language = ($profile ? $profile->getLanguage() :
null) ?: Loc::getCurrentLang();
157 $isAllLangs = $profile ? $profile->isAllLangs() :
false;
158 foreach ($assets as $asset)
161 $mess = $asset->useAllLangs($isAllLangs)->getContent();
162 if (!is_array($mess))
169 foreach ($mess as $messLanguage => $messList)
171 $messList = Resource\LangAsset::deletePrefixes(
173 $profile->getDeleteLangPrefixes()
175 if ($profile->isLangCamelCase())
177 $messList = Resource\LangAsset::toCamelCase($messList);
179 if (!isset($messages[$messLanguage]) || !is_array($messages[$messLanguage]))
181 $messages[$messLanguage] = [];
184 $messages[$messLanguage] = array_merge(
185 $messages[$messLanguage],
191 $languages = array_unique(array_merge(
193 array_keys($messages)
197 if (count($messages) === 1)
199 $messages = current($messages);
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();
214 protected static function getEol($multiplier = 2)
216 return str_repeat(
"\n", $multiplier);
221 return self::CORE_EXTENSION === $name;
static isCoreExtension($name)
static getEol($multiplier=2)
static encodeModule(Module $module)
static stringify(Builder $builder)