mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-03 16:51:40 +09:00
Separate common methods into BaseParser class
This commit is contained in:
parent
fa5f70c0e9
commit
e1df71bc38
3 changed files with 70 additions and 70 deletions
60
common/framework/parsers/baseparser.php
Normal file
60
common/framework/parsers/baseparser.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Parsers;
|
||||
|
||||
/**
|
||||
* This class provides common methods for other parser classes to use.
|
||||
*/
|
||||
abstract class BaseParser
|
||||
{
|
||||
/**
|
||||
* Get all attributes of an element as an associative array.
|
||||
*
|
||||
* @param SimpleXMLElement $element
|
||||
* @param bool $remove_symbols
|
||||
* @return array
|
||||
*/
|
||||
protected static function _getAttributes(\SimpleXMLElement $element, $remove_symbols = true): array
|
||||
{
|
||||
$result = array();
|
||||
foreach ($element->attributes() as $key => $val)
|
||||
{
|
||||
if ($remove_symbols)
|
||||
{
|
||||
$key = preg_replace('/[^a-z]/', '', $key);
|
||||
}
|
||||
$result[trim($key)] = trim($val);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contents of child elements that match a language.
|
||||
*
|
||||
* @param SimpleXMLElement $parent
|
||||
* @param string $tag_name
|
||||
* @param string $lang
|
||||
* @return string
|
||||
*/
|
||||
protected static function _getChildrenByLang(\SimpleXMLElement $parent, string $tag_name, string $lang): string
|
||||
{
|
||||
// If there is a child element that matches the language, return it.
|
||||
foreach ($parent->{$tag_name} as $child)
|
||||
{
|
||||
$attribs = $child->attributes('xml', true);
|
||||
if (strval($attribs['lang']) === $lang)
|
||||
{
|
||||
return trim($child);
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, return the first child element.
|
||||
foreach ($parent->{$tag_name} as $child)
|
||||
{
|
||||
return trim($child);
|
||||
}
|
||||
|
||||
// If there are no child elements, return an empty string.
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ namespace Rhymix\Framework\Parsers;
|
|||
/**
|
||||
* Module action (conf/module.xml) parser class for XE compatibility.
|
||||
*/
|
||||
class ModuleActionParser
|
||||
class ModuleActionParser extends BaseParser
|
||||
{
|
||||
/**
|
||||
* Shortcuts for route definition.
|
||||
|
|
@ -57,7 +57,7 @@ class ModuleActionParser
|
|||
foreach ($xml->grants->grant ?: [] as $grant)
|
||||
{
|
||||
$grant_info = new \stdClass;
|
||||
$grant_info->title = self::_getElementsByLang($grant, 'title', $lang);
|
||||
$grant_info->title = self::_getChildrenByLang($grant, 'title', $lang);
|
||||
$grant_info->default = trim($grant['default']);
|
||||
$grant_name = trim($grant['name']);
|
||||
$info->grant->{$grant_name} = $grant_info;
|
||||
|
|
@ -67,7 +67,7 @@ class ModuleActionParser
|
|||
foreach ($xml->menus->menu ?: [] as $menu)
|
||||
{
|
||||
$menu_info = new \stdClass;
|
||||
$menu_info->title = self::_getElementsByLang($menu, 'title', $lang);
|
||||
$menu_info->title = self::_getChildrenByLang($menu, 'title', $lang);
|
||||
$menu_info->index = null;
|
||||
$menu_info->acts = array();
|
||||
$menu_info->type = trim($menu['type']);
|
||||
|
|
@ -224,34 +224,4 @@ class ModuleActionParser
|
|||
$result->vars = $vars;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get child elements that match a language.
|
||||
*
|
||||
* @param SimpleXMLElement $parent
|
||||
* @param string $tag_name
|
||||
* @param string $lang
|
||||
* @return string
|
||||
*/
|
||||
protected static function _getElementsByLang(\SimpleXMLElement $parent, string $tag_name, string $lang): string
|
||||
{
|
||||
// If there is a child element that matches the language, return it.
|
||||
foreach ($parent->{$tag_name} as $child)
|
||||
{
|
||||
$attribs = $child->attributes('xml', true);
|
||||
if (strval($attribs['lang']) === $lang)
|
||||
{
|
||||
return trim($child);
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, return the first child element.
|
||||
foreach ($parent->{$tag_name} as $child)
|
||||
{
|
||||
return trim($child);
|
||||
}
|
||||
|
||||
// If there are no child elements, return an empty string.
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ namespace Rhymix\Framework\Parsers;
|
|||
/**
|
||||
* Module info (conf/info.xml) parser class for XE compatibility.
|
||||
*/
|
||||
class ModuleInfoParser
|
||||
class ModuleInfoParser extends BaseParser
|
||||
{
|
||||
/**
|
||||
* Load an XML file.
|
||||
|
|
@ -34,8 +34,8 @@ class ModuleInfoParser
|
|||
// Parse version 0.2
|
||||
if ($version === '0.2')
|
||||
{
|
||||
$info->title = self::_getElementsByLang($xml, 'title', $lang);
|
||||
$info->description = self::_getElementsByLang($xml, 'description', $lang);
|
||||
$info->title = self::_getChildrenByLang($xml, 'title', $lang);
|
||||
$info->description = self::_getChildrenByLang($xml, 'description', $lang);
|
||||
$info->version = trim($xml->version);
|
||||
$info->homepage = trim($xml->homepage);
|
||||
$info->category = trim($xml->category) ?: 'service';
|
||||
|
|
@ -47,7 +47,7 @@ class ModuleInfoParser
|
|||
foreach ($xml->author as $author)
|
||||
{
|
||||
$author_info = new \stdClass;
|
||||
$author_info->name = self::_getElementsByLang($author, 'name', $lang);
|
||||
$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;
|
||||
|
|
@ -57,8 +57,8 @@ class ModuleInfoParser
|
|||
// Parse version 0.1
|
||||
else
|
||||
{
|
||||
$info->title = self::_getElementsByLang($xml, 'title', $lang);
|
||||
$info->description = self::_getElementsByLang($xml->author, 'description', $lang);
|
||||
$info->title = self::_getChildrenByLang($xml, 'title', $lang);
|
||||
$info->description = self::_getChildrenByLang($xml->author, 'description', $lang);
|
||||
$info->version = trim($xml['version']);
|
||||
$info->homepage = trim($xml->homepage);
|
||||
$info->category = trim($xml['category']) ?: 'service';
|
||||
|
|
@ -70,7 +70,7 @@ class ModuleInfoParser
|
|||
foreach ($xml->author as $author)
|
||||
{
|
||||
$author_info = new \stdClass;
|
||||
$author_info->name = self::_getElementsByLang($author, 'name', $lang);
|
||||
$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;
|
||||
|
|
@ -88,34 +88,4 @@ class ModuleInfoParser
|
|||
// Return the complete result.
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get child elements that match a language.
|
||||
*
|
||||
* @param SimpleXMLElement $parent
|
||||
* @param string $tag_name
|
||||
* @param string $lang
|
||||
* @return string
|
||||
*/
|
||||
protected static function _getElementsByLang(\SimpleXMLElement $parent, string $tag_name, string $lang): string
|
||||
{
|
||||
// If there is a child element that matches the language, return it.
|
||||
foreach ($parent->{$tag_name} as $child)
|
||||
{
|
||||
$attribs = $child->attributes('xml', true);
|
||||
if (strval($attribs['lang']) === $lang)
|
||||
{
|
||||
return trim($child);
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, return the first child element.
|
||||
foreach ($parent->{$tag_name} as $child)
|
||||
{
|
||||
return trim($child);
|
||||
}
|
||||
|
||||
// If there are no child elements, return an empty string.
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue