Clean up editor module

This commit is contained in:
Kijin Sung 2020-07-09 16:37:04 +09:00
parent d4c449c2a6
commit 143600306d
8 changed files with 215 additions and 230 deletions

View file

@ -57,4 +57,50 @@ abstract class BaseParser
// If there are no child elements, return an empty string.
return '';
}
/**
* Parse extra_vars.
*
* @param SimpleXMLElement $extra_vars
* @param string $lang
* @return object
*/
protected static function _getExtraVars(\SimpleXMLElement $extra_vars, string $lang): \stdClass
{
$result = new \stdClass;
$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);
foreach ($group_result as $key => $val)
{
$result->{$key} = $val;
}
}
foreach ($extra_vars->var ?: [] as $var)
{
$item = new \stdClass;
$item->group = $group_name;
$item->name = trim($var['name']);
$item->type = trim($var['type']);
$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->value = null;
if ($var->options)
{
$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']);
$item->options[$option_item->value] = $option_item;
}
}
$result->{$item->name} = $item;
}
return $result;
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* Editor component (info.xml) parser class for XE compatibility.
*/
class EditorComponentParser extends BaseParser
{
/**
* Load an XML file.
*
* @param string $filename
* @param string $component_name
* @param string $lang
* @return object|false
*/
public static function loadXML(string $filename, string $component_name, string $lang = '')
{
// Load the XML file.
$xml = simplexml_load_string(file_get_contents($filename));
if ($xml === false)
{
return false;
}
// Get the current language.
$lang = $lang ?: (\Context::getLangType() ?: 'en');
// Initialize the module definition.
$info = new \stdClass;
$info->component_name = $component_name;
// Get basic information.
$info->title = self::_getChildrenByLang($xml, 'title', $lang);
$info->description = self::_getChildrenByLang($xml, 'description', $lang);
$info->version = trim($xml->version);
$info->date = 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;
}
// Get extra_vars.
if ($xml->extra_vars)
{
$info->extra_vars = self::_getExtraVars($xml->extra_vars, $lang);
}
// Return the complete result.
return $info;
}
}