mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-09 20:12:14 +09:00
Add convenience functions for XML parsing
This commit is contained in:
parent
57be6abc9d
commit
9e13c5ee6e
2 changed files with 57 additions and 11 deletions
|
|
@ -28,6 +28,52 @@ abstract class BaseParser
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the string value of an XML attribute after normalizing its name.
|
||||||
|
*
|
||||||
|
* @param SimpleXMLElement $element
|
||||||
|
* @param string $name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function _getAttributeString(\SimpleXMLElement $element, string $name): string
|
||||||
|
{
|
||||||
|
$normalized_name = strtolower(preg_replace('/[^a-zA-Z]/', '', $name));
|
||||||
|
foreach ($element->attributes() as $key => $val)
|
||||||
|
{
|
||||||
|
$normalized_key = strtolower(preg_replace('/[^a-zA-Z]/', '', $key));
|
||||||
|
if ($normalized_key === $normalized_name)
|
||||||
|
{
|
||||||
|
return trim($val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the boolean value of an XML attribute after normalizing its name.
|
||||||
|
*
|
||||||
|
* 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 string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected static function _getAttributeBool(\SimpleXMLElement $element, string $name): bool
|
||||||
|
{
|
||||||
|
$normalized_name = strtolower(preg_replace('/[^a-zA-Z]/', '', $name));
|
||||||
|
foreach ($element->attributes() as $key => $val)
|
||||||
|
{
|
||||||
|
$normalized_key = strtolower(preg_replace('/[^a-zA-Z]/', '', $key));
|
||||||
|
if ($normalized_key === $normalized_name)
|
||||||
|
{
|
||||||
|
$normalized_val = strtolower(preg_replace('/[^a-zA-Z]/', '', $val));
|
||||||
|
return ($normalized_key === $normalized_val) || toBool($val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the contents of child elements that match a language.
|
* Get the contents of child elements that match a language.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -187,40 +187,40 @@ class ModuleActionParser extends BaseParser
|
||||||
$action_info->method = implode('|', $methods);
|
$action_info->method = implode('|', $methods);
|
||||||
$action_info->route = $route_arg;
|
$action_info->route = $route_arg;
|
||||||
$action_info->standalone = $standalone;
|
$action_info->standalone = $standalone;
|
||||||
$action_info->check_csrf = (trim($action['check_csrf'] ?? '') ?: trim($action['check-csrf'] ?? '')) === 'false' ? 'false' : 'true';
|
$action_info->check_csrf = self::_getAttributeString($action, 'check-csrf') === 'false' ? 'false' : 'true';
|
||||||
$action_info->meta_noindex = (trim($action['meta_noindex'] ?? '') ?: trim($action['meta-noindex'] ?? '')) === 'true' ? 'true' : 'false';
|
$action_info->meta_noindex = self::_getAttributeString($action, 'meta-noindex') === 'true' ? 'true' : 'false';
|
||||||
$action_info->global_route = $global_route;
|
$action_info->global_route = $global_route;
|
||||||
$info->action->{$action_name} = $action_info;
|
$info->action->{$action_name} = $action_info;
|
||||||
|
|
||||||
// Set the menu name and index settings.
|
// Set the menu name and index settings.
|
||||||
$menu_name = trim($action['menu_name'] ?? '');
|
$menu_name = self::_getAttributeString($action, 'menu-name');
|
||||||
if ($menu_name && isset($info->menu->{$menu_name}))
|
if ($menu_name && isset($info->menu->{$menu_name}))
|
||||||
{
|
{
|
||||||
$info->menu->{$menu_name}->acts[] = $action_name;
|
$info->menu->{$menu_name}->acts[] = $action_name;
|
||||||
if (toBool($action['menu_index']))
|
if (self::_getAttributeBool($action, 'menu_index'))
|
||||||
{
|
{
|
||||||
$info->menu->{$menu_name}->index = $action_name;
|
$info->menu->{$menu_name}->index = $action_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (toBool($action['index']))
|
if (self::_getAttributeBool($action, 'index'))
|
||||||
{
|
{
|
||||||
$info->default_index_act = $action_name;
|
$info->default_index_act = $action_name;
|
||||||
}
|
}
|
||||||
if (toBool($action['admin_index']))
|
if (self::_getAttributeBool($action, 'admin_index'))
|
||||||
{
|
{
|
||||||
$info->admin_index_act = $action_name;
|
$info->admin_index_act = $action_name;
|
||||||
}
|
}
|
||||||
if (toBool($action['setup_index']))
|
if (self::_getAttributeBool($action, 'setup_index'))
|
||||||
{
|
{
|
||||||
$info->setup_index_act = $action_name;
|
$info->setup_index_act = $action_name;
|
||||||
}
|
}
|
||||||
if (toBool($action['simple_setup_index']))
|
if (self::_getAttributeBool($action, 'simple_setup_index'))
|
||||||
{
|
{
|
||||||
$info->simple_setup_index_act = $action_name;
|
$info->simple_setup_index_act = $action_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set error handler settings.
|
// Set error handler settings.
|
||||||
$error_handlers = explode(',', trim($action['error_handlers'] ?? '') ?: trim($action['error-handlers'] ?? ''));
|
$error_handlers = explode(',', self::_getAttributeString($action, 'error-handlers'));
|
||||||
foreach ($error_handlers as $error_handler)
|
foreach ($error_handlers as $error_handler)
|
||||||
{
|
{
|
||||||
if (intval($error_handler) > 200)
|
if (intval($error_handler) > 200)
|
||||||
|
|
@ -237,8 +237,8 @@ class ModuleActionParser extends BaseParser
|
||||||
if (isset($info->action->{$action_name}))
|
if (isset($info->action->{$action_name}))
|
||||||
{
|
{
|
||||||
$info->action->{$action_name}->permission->target = trim($permission['target'] ?? '');
|
$info->action->{$action_name}->permission->target = trim($permission['target'] ?? '');
|
||||||
$info->action->{$action_name}->permission->check_var = trim($permission['check_var'] ?? '') ?: trim($permission['check-var'] ?? '');
|
$info->action->{$action_name}->permission->check_var = self::_getAttributeString($permission, 'check-var');
|
||||||
$info->action->{$action_name}->permission->check_type = trim($permission['check_type'] ?? '') ?: trim($permission['check-type'] ?? '');
|
$info->action->{$action_name}->permission->check_type = self::_getAttributeString($permission, 'check-type');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue