Replace widget and widgetstyle XML parsing and caching logic

This commit is contained in:
Kijin Sung 2025-05-19 21:38:58 +09:00
parent 6727b124cd
commit 278369b70e
4 changed files with 258 additions and 250 deletions

View file

@ -134,14 +134,14 @@ abstract class BaseParser
$item->group = $group_name;
// id and name
if ($type === 'widget')
if ($type === 'widget' || $type === 'widgetstyle')
{
$item->id = trim($var['id']) ?: trim($var->id);
$item->id = trim($var['id'] ?? '') ?: trim($var->id);
if (!$item->id)
{
$item->id = trim($var['name']);
$item->id = trim($var['name'] ?? '');
}
$item->name = $var->nameself::_getChildrenByLang($var, 'name', $lang);
$item->name = self::_getChildrenByLang($var, 'name', $lang);
if (!$item->name)
{
$item->name = self::_getChildrenByLang($var, 'title', $lang);
@ -149,11 +149,11 @@ abstract class BaseParser
}
else
{
$item->name = trim($var['name']);
$item->name = trim($var['name'] ?? '');
}
// type
$item->type = trim($var['type']);
$item->type = trim($var['type'] ?? '');
if (!$item->type)
{
$item->type = trim($var->type) ?: 'text';
@ -167,7 +167,7 @@ abstract class BaseParser
// Other common attributes
$item->title = self::_getChildrenByLang($var, 'title', $lang);
$item->description = str_replace('\\n', "\n", self::_getChildrenByLang($var, 'description', $lang));
$item->default = trim($var['default']) ?: null;
$item->default = trim($var['default'] ?? '') ?: null;
if (!isset($item->default))
{
$item->default = self::_getChildrenByLang($var, 'default', $lang);
@ -180,22 +180,38 @@ abstract class BaseParser
$item->options = array();
foreach ($var->options as $option)
{
$option_item = new \stdClass;
$option_item->title = self::_getChildrenByLang($option, 'title', $lang);
$option_item->value = trim($option['value']) ?: trim($option->value);
$item->options[$option_item->value] = $option_item;
if ($type === 'widget' && $option['default'] === 'true')
if ($type === 'widget' || $type === 'widgetstyle')
{
$item->default_options[$option_item->value] = true;
$value = trim($option->value ?? '');
$item->options[$value] = self::_getChildrenByLang($option, 'name', $lang);
if ($option['default'] === 'true')
{
$item->default_options[$value] = true;
}
if ($option['init'] === 'true')
{
$item->init_options[$value] = true;
}
}
if ($type === 'widget' && $option['init'] === 'true')
else
{
$item->init_options[$option_item->value] = true;
$option_item = new \stdClass;
$option_item->title = self::_getChildrenByLang($option, 'title', $lang);
$option_item->value = trim($option['value'] ?? '') ?: trim($option->value ?? '');
$item->options[$option_item->value] = $option_item;
}
}
}
$result->{$item->name} = $item;
// Add to list of variables
if ($type === 'widget' || $type === 'widgetstyle')
{
$result->{$item->id} = $item;
}
else
{
$result->{$item->name} = $item;
}
}
return $result;

View file

@ -0,0 +1,96 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* Widget (info.xml) parser class for XE compatibility.
*/
class WidgetInfoParser extends BaseParser
{
/**
* Load an XML file.
*
* @param string $filename
* @param string $widget_name
* @param string $lang
* @return ?object
*/
public static function loadXML(string $filename, string $widget_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 widget definition.
$info = new \stdClass;
$info->widget = $widget_name;
$info->path = sprintf('./widgets/%s/', $widget_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->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);
$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->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->author, 'name', $lang);
$author_info->email_address = trim($xml->author['email_address']);
$author_info->homepage = trim($xml->author['link'] ?? '');
$info->author[] = $author_info;
}
// Get extra_vars.
if ($xml->extra_vars)
{
$info->extra_var = self::_getExtraVars($xml->extra_vars, $lang, 'widget');
}
else
{
$info->extra_var = new \stdClass;
}
// Prepare additional fields that will be filled in later.
$info->widget_srl = null;
$info->widget_title = null;
// Return the complete result.
return $info;
}
}

View file

@ -0,0 +1,83 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* Widget Style (info.xml) parser class for XE compatibility.
*/
class WidgetStyleInfoParser extends BaseParser
{
/**
* Load an XML file.
*
* @param string $filename
* @param string $widgetstyle_name
* @param string $lang
* @return ?object
*/
public static function loadXML(string $filename, string $widgetstyle_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 widget definition.
$info = new \stdClass;
$info->widgetStyle = $widgetstyle_name;
$info->path = sprintf('./widgetstyles/%s/', $widgetstyle_name);
// Parse common fields.
$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->link);
$info->license = trim($xml->license);
$info->license_link = trim($xml->license['link'] ?? '');
// Parse the preview image.
$preview_filename = trim($xml->preview ?? 'preview.jpg');
$preview_path = sprintf('%s%s', $info->path, $preview_filename);
if (file_exists($preview_path))
{
$info->preview = $preview_path;
}
else
{
$info->preview = null;
}
// Parse the author list.
$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_var = self::_getExtraVars($xml->extra_vars, $lang, 'widget');
}
else
{
$info->extra_var = new \stdClass;
}
// Count extra vars.
$info->extra_var_count = count(get_object_vars($info->extra_var));
// Return the complete result.
return $info;
}
}