From 7ffdc10bef06624ca2c6cf58e1109f880bbbf13e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 12 Dec 2021 00:18:41 +0100 Subject: [PATCH] Continued: - StringUtils::convertToClassName() is now "in-method cached". That means that the parameter $str is used as key and the value is being generated when the key does not exist in self::$cache array MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../utils/strings/class_StringUtils.php | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/framework/main/classes/utils/strings/class_StringUtils.php b/framework/main/classes/utils/strings/class_StringUtils.php index 947a657a..257d325d 100644 --- a/framework/main/classes/utils/strings/class_StringUtils.php +++ b/framework/main/classes/utils/strings/class_StringUtils.php @@ -44,6 +44,11 @@ final class StringUtils extends BaseFrameworkSystem { */ private static $decimals = ''; // German + /** + * In-method cache array + */ + private static $cache = []; + /** * Array with bitmasks and such for pack/unpack methods to support both * 32-bit and 64-bit systems @@ -190,30 +195,36 @@ final class StringUtils extends BaseFrameworkSystem { * * @param $str The string, what ever it is needs to be converted * @return $className Generated class name + * @throws InvalidArgumentException If a paramter is invalid */ public static final function convertToClassName (string $str) { // Is the parameter valid? - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - CALLED!', $str)); + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - CALLED!', $str)); if (empty($str)) { // No empty strings, please throw new InvalidArgumentException('Parameter "str" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Init class name - $className = ''; - - // Convert all dashes in underscores - $str = self::convertDashesToUnderscores($str); + } elseif (!isset(self::$cache[$str])) { + // Init class name + $className = ''; + + // Convert all dashes in underscores + $str = self::convertDashesToUnderscores($str); + + // Now use that underscores to get classname parts for hungarian style + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - AFTER!', $str)); + foreach (explode('_', $str) as $strPart) { + // Make the class name part lower case and first upper case + $className .= ucfirst(strtolower($strPart)); + } - // Now use that underscores to get classname parts for hungarian style - foreach (explode('_', $str) as $strPart) { - // Make the class name part lower case and first upper case - $className .= ucfirst(strtolower($strPart)); + // Set cache + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: self[%s]=%s - SET!', $str, $className)); + self::$cache[$str] = $className; } // Return class name - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: className=%s - EXIT!', $className)); - return $className; + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: self[%s]=%s - EXIT!', $str, $className)); + return self::$cache[$str]; } /** -- 2.39.5