Rewrite addon info.xml parser and support default values

This commit is contained in:
Kijin Sung 2025-05-18 21:42:36 +09:00
parent addf4d9972
commit a3d682a294
4 changed files with 146 additions and 220 deletions

View file

@ -0,0 +1,95 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* Addon (info.xml) parser class for XE compatibility.
*/
class AddonInfoParser extends BaseParser
{
/**
* Load an XML file.
*
* @param string $filename
* @param string $addon_name
* @param string $lang
* @return ?object
*/
public static function loadXML(string $filename, string $addon_name, 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 addon definition.
$info = new \stdClass;
$info->addon_name = $addon_name;
// 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);
$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->homepage);
$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);
$info->description = self::_getChildrenByLang($xml->author, 'description', $lang);
$info->version = trim($xml['version'] ?? '');
$info->date = date('Ymd', strtotime($xml->author['date'] . 'T12:00:00Z'));
$info->homepage = trim($xml->homepage);
$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;
}
}
// Get extra_vars.
if ($xml->extra_vars)
{
$info->extra_vars = get_object_vars(self::_getExtraVars($xml->extra_vars, $lang));
}
// Prepare additional fields that will be filled in later.
$info->is_enabled = (object)['pc' => false, 'mobile' => false];
$info->xe_run_method = 'run_selected';
$info->mid_list = [];
// Return the complete result.
return $info;
}
}

View file

@ -10,7 +10,7 @@ abstract class BaseParser
/**
* Get all attributes of an element as an associative array.
*
* @param SimpleXMLElement $element
* @param \SimpleXMLElement $element
* @param bool $normalize
* @return array
*/
@ -31,7 +31,7 @@ abstract class BaseParser
/**
* Get the string value of an XML attribute after normalizing its name.
*
* @param SimpleXMLElement $element
* @param \SimpleXMLElement $element
* @param string $name
* @return string
*/
@ -55,7 +55,7 @@ abstract class BaseParser
* A value that is identical to the name of the attribute will be treated as true.
* Other values will be passed to toBool() for evaluation.
*
* @param SimpleXMLElement $element
* @param \SimpleXMLElement $element
* @param string $name
* @return bool
*/
@ -77,7 +77,7 @@ abstract class BaseParser
/**
* Get the contents of child elements that match a language.
*
* @param SimpleXMLElement $parent
* @param \SimpleXMLElement $parent
* @param string $tag_name
* @param string $lang
* @return string
@ -107,7 +107,7 @@ abstract class BaseParser
/**
* Parse extra_vars.
*
* @param SimpleXMLElement $extra_vars
* @param \SimpleXMLElement $extra_vars
* @param string $lang
* @return object
*/
@ -132,6 +132,10 @@ abstract class BaseParser
$item->title = self::_getChildrenByLang($var, 'title', $lang);
$item->description = str_replace('\\n', "\n", self::_getChildrenByLang($var, 'description', $lang));
$item->default = trim($var['default']) ?: null;
if (!isset($item->default))
{
$item->default = self::_getChildrenByLang($var, 'default', $lang);
}
$item->value = null;
if ($var->options)
{