Reimplement skin XML parser

This commit is contained in:
Kijin Sung 2025-06-01 15:28:37 +09:00
parent 07e3298eb1
commit b849c597bc
3 changed files with 184 additions and 236 deletions

View file

@ -121,7 +121,7 @@ abstract class BaseParser
$group_name = $extra_vars->getName() === 'group' ? self::_getChildrenByLang($extra_vars, 'title', $lang) : null;
foreach ($extra_vars->group ?: [] as $group)
{
$group_result = self::_getExtraVars($group, $lang, $type);
$group_result = self::_getExtraVars($group, $lang, $type, $options);
foreach ($group_result as $key => $val)
{
$result->{$key} = $val;
@ -173,13 +173,36 @@ abstract class BaseParser
{
$item->default = self::_getChildrenByLang($var, 'default', $lang);
}
$item->value = null;
if ($type === 'skin')
{
$item->value = trim($var['value'] ?? '') ?: null;
if ($item->value && preg_match('/(,|\|@\|)/', $item->value ?? '', $delimiter))
{
$item->value = explode($delimiter[1], $item->value);
}
if ($item->type === 'mid_list' && !is_array($item->value))
{
$item->value = [$item->value];
}
}
else
{
$item->value = null;
}
// Options
if ($var->options)
if ($type === 'skin' && $options['version'] === '0.1')
{
$item->options = array();
foreach ($var->options as $option)
$xml_options = $var->default ?? null;
}
else
{
$xml_options = $var->options ?? null;
}
if ($xml_options)
{
$item->options = [];
foreach ($xml_options as $option)
{
if ($type === 'widget' || $type === 'widgetstyle')
{
@ -207,10 +230,24 @@ abstract class BaseParser
}
}
$title = self::_getChildrenByLang($option, 'title', $lang);
$value = trim($option['value'] ?? '') ?: trim($option->value ?? '');
$value = trim($option['value'] ?? '');
$option_item->val = $title;
$item->options[$value] = $option_item;
}
elseif ($type === 'skin' && $options['version'] === '0.1')
{
$option_item = new \stdClass;
$option_item->title = trim($option);
$option_item->value = trim($option);
$item->options[] = $option_item; // Numeric keys only
}
elseif ($type === 'skin' && $options['version'] === '0.2')
{
$option_item = new \stdClass;
$option_item->title = self::_getChildrenByLang($option, 'title', $lang);
$option_item->value = trim($option['value'] ?? '');
$item->options[] = $option_item; // Numeric keys only
}
else
{
$option_item = new \stdClass;
@ -221,6 +258,17 @@ abstract class BaseParser
}
}
// Other attributes
if ($type === 'skin' && $options['version'] === '0.1')
{
$item->width = intval($var['width'] ?? 0) ?: null;
$item->height = intval($var['height'] ?? 0) ?: null;
if (isset($item->options) && count($item->options))
{
$item->default = reset($item->options)->value;
}
}
// Add to list of variables
if ($type === 'widget' || $type === 'widgetstyle')
{

View file

@ -0,0 +1,122 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* Skin (info.xml) parser class for XE compatibility.
*/
class SkinInfoParser extends BaseParser
{
/**
* Load an XML file.
*
* @param string $filename
* @param string $skin_name
* @param string $skin_path
* @param string $lang
* @return ?object
*/
public static function loadXML(string $filename, string $skin_name, string $skin_path, string $lang = ''): ?object
{
// Load the XML file.
$xml = simplexml_load_string(file_get_contents($filename));
if ($xml === false)
{
return null;
}
// Get the current language.
$lang = $lang ?: (\Context::getLangType() ?: 'en');
// Initialize the layout definition.
$info = new \stdClass;
$info->skin = $skin_name;
$info->path = $skin_path;
// Get the XML schema version.
$version = strval($xml['version']) ?: '0.1';
// Parse version 0.2
if ($version === '0.2')
{
$info->title = self::_getChildrenByLang($xml, 'title', $lang) ?: $skin_name;
$info->description = self::_getChildrenByLang($xml, 'description', $lang);
$info->version = trim($xml->version);
$info->date = ($xml->date === 'RX_CORE') ? '' : date('Ymd', strtotime($xml->date . 'T12:00:00Z'));
$info->homepage = trim($xml->link);
$info->license = trim($xml->license);
$info->license_link = trim($xml->license['link'] ?? '');
$info->author = array();
foreach ($xml->author as $author)
{
$author_info = new \stdClass;
$author_info->name = self::_getChildrenByLang($author, 'name', $lang);
$author_info->email_address = trim($author['email_address'] ?? '');
$author_info->homepage = trim($author['link'] ?? '');
$info->author[] = $author_info;
}
}
// Parse version 0.1
else
{
$info->title = self::_getChildrenByLang($xml, 'title', $lang) ?: $skin_name;
$info->description = self::_getChildrenByLang($xml->maker, 'description', $lang);
$info->version = trim($xml['version'] ?? '');
$info->date = date('Ymd', strtotime($xml->maker['date'] . 'T12:00:00Z'));
$info->homepage = trim($xml->link);
$info->license = trim($xml->license);
$info->license_link = trim($xml->license['link'] ?? '');
$info->author = array();
$author_info = new \stdClass;
$author_info->name = self::_getChildrenByLang($xml->maker, 'name', $lang);
$author_info->email_address = trim($xml->maker['email_address']);
$author_info->homepage = trim($xml->maker['link'] ?? '');
$info->author[] = $author_info;
}
// Get extra_vars.
if ($xml->extra_vars)
{
$info->extra_vars = get_object_vars(self::_getExtraVars($xml->extra_vars, $lang, 'skin', ['version' => $version]));
}
else
{
$info->extra_vars = [];
}
// Get colorsets.
if ($xml->colorset && $xml->colorset->color)
{
$info->colorset = [];
foreach ($xml->colorset->color as $color)
{
$color_item = new \stdClass;
$color_item->name = trim($color['name'] ?? '');
$color_item->title = self::_getChildrenByLang($color, 'title', $lang);
$screenshot = trim($color['src'] ?? '');
if ($screenshot)
{
$screenshot = $info->path . $screenshot;
}
$color_item->screenshot = $screenshot;
$info->colorset[] = $color_item;
}
}
// Get thumbnail path.
if (file_exists($info->path . 'thumbnail.png'))
{
$info->thumbnail = $info->path . 'thumbnail.png';
}
else
{
$info->thumbnail = '';
}
// Return the complete result.
return $info;
}
}