From 9e13c5ee6ee3caf34d8b3032aeb544512695310b Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Fri, 11 Aug 2023 02:37:39 +0900 Subject: [PATCH] Add convenience functions for XML parsing --- common/framework/parsers/BaseParser.php | 46 +++++++++++++++++++ .../framework/parsers/ModuleActionParser.php | 22 ++++----- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/common/framework/parsers/BaseParser.php b/common/framework/parsers/BaseParser.php index aa1a522d5..14b1c15cd 100644 --- a/common/framework/parsers/BaseParser.php +++ b/common/framework/parsers/BaseParser.php @@ -28,6 +28,52 @@ abstract class BaseParser 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. * diff --git a/common/framework/parsers/ModuleActionParser.php b/common/framework/parsers/ModuleActionParser.php index f0a7b601c..ecf701020 100644 --- a/common/framework/parsers/ModuleActionParser.php +++ b/common/framework/parsers/ModuleActionParser.php @@ -187,40 +187,40 @@ class ModuleActionParser extends BaseParser $action_info->method = implode('|', $methods); $action_info->route = $route_arg; $action_info->standalone = $standalone; - $action_info->check_csrf = (trim($action['check_csrf'] ?? '') ?: trim($action['check-csrf'] ?? '')) === 'false' ? 'false' : 'true'; - $action_info->meta_noindex = (trim($action['meta_noindex'] ?? '') ?: trim($action['meta-noindex'] ?? '')) === 'true' ? 'true' : 'false'; + $action_info->check_csrf = self::_getAttributeString($action, 'check-csrf') === 'false' ? 'false' : 'true'; + $action_info->meta_noindex = self::_getAttributeString($action, 'meta-noindex') === 'true' ? 'true' : 'false'; $action_info->global_route = $global_route; $info->action->{$action_name} = $action_info; // 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})) { $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; } } - if (toBool($action['index'])) + if (self::_getAttributeBool($action, 'index')) { $info->default_index_act = $action_name; } - if (toBool($action['admin_index'])) + if (self::_getAttributeBool($action, 'admin_index')) { $info->admin_index_act = $action_name; } - if (toBool($action['setup_index'])) + if (self::_getAttributeBool($action, 'setup_index')) { $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; } // 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) { if (intval($error_handler) > 200) @@ -237,8 +237,8 @@ class ModuleActionParser extends BaseParser if (isset($info->action->{$action_name})) { $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_type = trim($permission['check_type'] ?? '') ?: trim($permission['check-type'] ?? ''); + $info->action->{$action_name}->permission->check_var = self::_getAttributeString($permission, 'check-var'); + $info->action->{$action_name}->permission->check_type = self::_getAttributeString($permission, 'check-type'); } }