From e77628498bf42b1d61a80dcc59dfbc52fb05069b Mon Sep 17 00:00:00 2001 From: Taufik Nurrohman Date: Tue, 9 Jun 2020 20:46:04 +0700 Subject: [PATCH] Standardize PHP Function Naming Convention --- README.md | 4 ++++ panel/about.page | 2 +- panel/engine/f.php | 24 ++++++++++++++++-------- panel/engine/f/field.php | 8 ++++---- panel/engine/f/h.php | 2 +- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 315bf164..ed1f1032 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ GUI Extension for Mecha Release Notes ------------- +### 2.3.1 + + - Improved function naming convention. PHP functions should conform to the snake-case naming convention (e.g. `a_b__c\d`). + ### 2.3.0 - Moved from `Pjax` to `F3H`. diff --git a/panel/about.page b/panel/about.page index 96ff102f..ede19449 100644 --- a/panel/about.page +++ b/panel/about.page @@ -3,7 +3,7 @@ title: Panel description: GUI extension for Mecha. author: Taufik Nurrohman type: Markdown -version: 2.3.0 +version: 2.3.1 use: '.\lot\x\layout': 1 diff --git a/panel/engine/f.php b/panel/engine/f.php index 819a5a8b..8739739c 100644 --- a/panel/engine/f.php +++ b/panel/engine/f.php @@ -22,7 +22,7 @@ function button($in, $key) { function field($in, $key) { $tags = ['field', 'p']; if (isset($in['type'])) { - $tags[] = \strtr(\p2f($in['type'], '-', '/'), ['/' => ':']); + $tags[] = \strtr($in['type'], '/', ':'); } $id = $in['id'] ?? 'f:' . \dechex(\time()); $in[2]['id'] = $in[2]['id'] ?? \str_replace('f:', 'field:', $id); @@ -93,7 +93,11 @@ function fields($in) { continue; } $type = $v['type'] ?? null; - if (\function_exists($fn = \rtrim(__NAMESPACE__ . "\\" . \f2p(\strtr($type, '/', '.')), "\\"))) { + if ($type && \function_exists($fn = __NAMESPACE__ . "\\" . \strtr($type, [ + '/' => "\\", + '-' => '_', + '.' => '__' + ]))) { if ('field/hidden' !== $type) { $out[1] .= \call_user_func($fn, $v, $k); } else { @@ -567,7 +571,7 @@ function content($in, $key) { $out[1] .= \_\lot\x\panel\h\content($in['content']); } $out[2] = \array_replace([ - 'class' => 'count:1 lot' . (isset($type) ? ' lot:' . \implode(' lot:', \step(\c2f($type))) : ""), + 'class' => 'count:1 lot' . (isset($type) ? ' lot:' . \implode(' lot:', \step(\strtr($type, '/', '.'))) : ""), 'id' => $in['id'] ?? null ], $out[2]); return new \HTML($out); @@ -586,7 +590,7 @@ function lot($in, $key) { $out[1] .= \_\lot\x\panel\h\lot($in['lot'], null, $count); } $out[2] = \array_replace([ - 'class' => 'count:' . $count . ' lot' . (isset($type) ? ' lot:' . \implode(' lot:', \step(\c2f($type))) : ""), + 'class' => 'count:' . $count . ' lot' . (isset($type) ? ' lot:' . \implode(' lot:', \step(\strtr($type, '/', '.'))) : ""), 'id' => $in['id'] ?? null ], $out[2]); return new \HTML($out); @@ -602,11 +606,15 @@ function panel($in, $key) { return ""; } $out = ""; - if ($type = isset($in['type']) ? \f2p(\strtr($in['type'], '/', '.')) : null) { - if (\function_exists($fn = \rtrim(__NAMESPACE__ . "\\panel\\" . $type, "\\"))) { + if ($type = isset($in['type']) ? \strtr($in['type'], [ + '/' => "\\", + '-' => '_', + '.' => '__' + ]) : null) { + if ($type && \function_exists($fn = __NAMESPACE__ . "\\panel\\" . $type)) { $out .= \call_user_func($fn, $in, $key); } else if (isset($in['content'])) { - if (\function_exists($fn = \rtrim(__NAMESPACE__ . "\\panel\\content\\" . $type, "\\"))) { + if ($type && \function_exists($fn = __NAMESPACE__ . "\\panel\\content\\" . $type)) { $out .= \call_user_func($fn, $in, $key); } else { if (\defined("\\DEBUG") && \DEBUG) { @@ -615,7 +623,7 @@ function panel($in, $key) { $out .= \_\lot\x\panel\content($in, $key); } } else if (isset($in['lot'])) { - if (\function_exists($fn = \rtrim(__NAMESPACE__ . "\\panel\\lot\\" . $type, "\\"))) { + if ($type && \function_exists($fn = __NAMESPACE__ . "\\panel\\lot\\" . $type)) { $out .= \call_user_func($fn, $in, $key); } else { if (\defined("\\DEBUG") && \DEBUG) { diff --git a/panel/engine/f/field.php b/panel/engine/f/field.php index 00334cb2..0dc347c5 100644 --- a/panel/engine/f/field.php +++ b/panel/engine/f/field.php @@ -153,10 +153,10 @@ function date($in, $key) { if (!isset($in['pattern'])) { $in['pattern'] = "^[1-9]\\d{3,}-(0\\d|1[0-2])-(0\\d|[1-2]\\d|3[0-1])$"; } - return \_\lot\x\panel\field\dateTime($in, $key); + return \_\lot\x\panel\field\date_time($in, $key); } -function dateTime($in, $key) { +function date_time($in, $key) { if (!isset($in['alt'])) { $in['alt'] = \date('Y-m-d H:i:s'); } @@ -418,7 +418,7 @@ function time($in, $key) { if (!isset($in['pattern'])) { $in['pattern'] = "^([0-1]\\d|2[0-4])(:([0-5]\\d|60)){1,2}$"; } - $out = \_\lot\x\panel\field\dateTime($in, $key); + $out = \_\lot\x\panel\field\date_time($in, $key); return $out; } @@ -440,7 +440,7 @@ function toggle($in, $key) { return \_\lot\x\panel\field($out, $key); } -function URL($in, $key) { +function u_r_l($in, $key) { if (!isset($in['alt'])) { $url = $GLOBALS['url']; $in['alt'] = \S . $url->protocol . \S . $url->host . \S; diff --git a/panel/engine/f/h.php b/panel/engine/f/h.php index e4602e1b..98939d94 100644 --- a/panel/engine/f/h.php +++ b/panel/engine/f/h.php @@ -116,7 +116,7 @@ function p(&$lot, $prefix) { // Add prefix to `type` $type = $prefix . '/' . $type; } - $v['type'] = $type; + $v['type'] = \trim($type, '/'); } unset($v); }