From 37d18df50cd6e435f54edd2fbc2aa5dbc8dd8014 Mon Sep 17 00:00:00 2001 From: thomas Date: Sun, 6 Jul 2025 20:26:39 +0200 Subject: [PATCH] =?UTF-8?q?Sprint=201.1:=20Cache-System=20in=20Tools.php?= =?UTF-8?q?=20implementiert=20-=20enableCache,=20restoreCacheSettings,=20c?= =?UTF-8?q?learCache,=20clearCompile,=20clearSmartyCache,=20clearSf2Cache,?= =?UTF-8?q?=20clearAllCache=20und=20Hilfsfunktionen=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHASE_3_TRACKER.md | 15 ++- classes/Tools.php | 233 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+), 8 deletions(-) diff --git a/PHASE_3_TRACKER.md b/PHASE_3_TRACKER.md index 88ae55c..8a178a1 100644 --- a/PHASE_3_TRACKER.md +++ b/PHASE_3_TRACKER.md @@ -2,11 +2,11 @@ ## Milestone 1: Core-System Erweiterung (Sprint 1.1-1.3) -### Sprint 1.1: Tools.php Erweiterung (50% abgeschlossen) +### Sprint 1.1: Tools.php Erweiterung (75% abgeschlossen) - [x] Security-Funktionen (hash, getToken, AdminToken, String-Operationen, Utility-Funktionen) - [x] File-Operationen (deleteDirectory, file_get_contents, copy, scandir, etc.) - [x] Math-Funktionen (ps_round, math_round, round_helper, ceilf, floorf, spreadAmount) -- [ ] Cache-System Erweiterung +- [x] Cache-System Erweiterung (enableCache, restoreCacheSettings, clearCache, clearCompile, clearSmartyCache, clearSf2Cache, clearAllCache, getMemoryLimit, getOctets, isX86_64arch, isPHPCLI, argvToGET, getMaxUploadSize, convertBytes) - [ ] Context.php Erweiterung - [ ] Cart.php Erweiterung @@ -122,11 +122,10 @@ - [ ] Monitoring Setup - [ ] Backup Setup -## Gesamtfortschritt: 8% (1 von 18 Sprints abgeschlossen) +## Gesamtfortschritt: 11% (1 von 18 Sprints zu 75% abgeschlossen) ### Nächste Schritte: -1. Cache-System Erweiterung in Tools.php -2. Context.php Erweiterung -3. Cart.php Erweiterung -4. Sprint 1.1 abschließen -5. Sprint 1.2 beginnen (Datenbank & ORM) \ No newline at end of file +1. Context.php Erweiterung +2. Cart.php Erweiterung +3. Sprint 1.1 abschließen +4. Sprint 1.2 beginnen (Datenbank & ORM) \ No newline at end of file diff --git a/classes/Tools.php b/classes/Tools.php index 9c4d97b..0303a72 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -1183,4 +1183,237 @@ class Tools $rows[0][$column] += $diff; } } + + /** + * Enable cache for Smarty + * + * @param int $level + * @param Context|null $context + */ + public static function enableCache($level = 1, ?Context $context = null) + { + if (!$context) { + $context = Context::getContext(); + } + $smarty = $context->smarty; + if (!Configuration::get('PS_SMARTY_CACHE')) { + return; + } + if ($smarty->force_compile == 0 && $smarty->caching == $level) { + return; + } + self::$_forceCompile = (int) $smarty->force_compile; + self::$_caching = (int) $smarty->caching; + $smarty->force_compile = false; + $smarty->caching = (int) $level; + $smarty->cache_lifetime = 31536000; // 1 Year + } + + /** + * Restore cache settings + * + * @param Context|null $context + */ + public static function restoreCacheSettings(?Context $context = null) + { + if (!$context) { + $context = Context::getContext(); + } + + if (isset(self::$_forceCompile)) { + $context->smarty->force_compile = (bool) self::$_forceCompile; + } + if (isset(self::$_caching)) { + $context->smarty->caching = (int) self::$_caching; + } + } + + /** + * Clear cache for Smarty + * + * @param Smarty|null $smarty + * @param bool|string $tpl + * @param string|null $cache_id + * @param string|null $compile_id + * @return int|null + */ + public static function clearCache($smarty = null, $tpl = false, $cache_id = null, $compile_id = null) + { + if ($smarty === null) { + $smarty = Context::getContext()->smarty; + } + + if ($smarty === null) { + return null; + } + + if (!$tpl && $cache_id === null && $compile_id === null) { + return $smarty->clearAllCache(); + } + + $ret = $smarty->clearCache($tpl, $cache_id, $compile_id); + + Hook::exec('actionClearCache'); + + return $ret; + } + + /** + * Clear compile for Smarty + * + * @param Smarty|null $smarty + * @return int|null + */ + public static function clearCompile($smarty = null) + { + if ($smarty === null) { + $smarty = Context::getContext()->smarty; + } + + if ($smarty === null) { + return null; + } + + $ret = $smarty->clearCompiledTemplate(); + + Hook::exec('actionClearCompileCache'); + + return $ret; + } + + /** + * Clear Smarty cache and compile folders + */ + public static function clearSmartyCache() + { + $smarty = Context::getContext()->smarty; + self::clearCache($smarty); + self::clearCompile($smarty); + } + + /** + * Clear Symfony cache + * + * @param string|null $env + */ + public static function clearSf2Cache($env = null) + { + if (null === $env) { + $env = _PS_ENV_; + } + + $dir = _PS_ROOT_DIR_ . '/var/cache/' . $env . '/'; + + register_shutdown_function(function () use ($dir) { + $fs = new Filesystem(); + $fs->remove($dir); + Hook::exec('actionClearSf2Cache'); + }); + } + + /** + * Clear both Smarty and Symfony cache + */ + public static function clearAllCache() + { + self::clearSmartyCache(); + self::clearSf2Cache(); + } + + /** + * Get memory limit in octets + * + * @return int|string + */ + public static function getMemoryLimit() + { + $memory_limit = @ini_get('memory_limit'); + + return self::getOctets($memory_limit); + } + + /** + * Get octets from configuration option + * + * @param string $option + * @return int|string + */ + public static function getOctets($option) + { + if (preg_match('/[0-9]+k/i', $option)) { + return 1024 * (int) $option; + } + + if (preg_match('/[0-9]+m/i', $option)) { + return 1024 * 1024 * (int) $option; + } + + if (preg_match('/[0-9]+g/i', $option)) { + return 1024 * 1024 * 1024 * (int) $option; + } + + return $option; + } + + /** + * Check if server uses 64bit architecture + * + * @return bool + */ + public static function isX86_64arch() + { + return PHP_INT_MAX == '9223372036854775807'; + } + + /** + * Check if php-cli is used + * + * @return bool + */ + public static function isPHPCLI() + { + return defined('STDIN') || (self::strtolower(PHP_SAPI) == 'cli' && (!isset($_SERVER['REMOTE_ADDR']) || empty($_SERVER['REMOTE_ADDR']))); + } + + /** + * Convert argv to GET parameters + * + * @param int $argc + * @param array $argv + */ + public static function argvToGET($argc, $argv) + { + if ($argc <= 1) { + return; + } + + parse_str($argv[1], $args); + if (!is_array($args) || !count($args)) { + return; + } + $_GET = array_merge($args, $_GET); + $_SERVER['QUERY_STRING'] = $argv[1]; + } + + /** + * Get max file upload size + * + * @param int $max_size + * @return int + */ + public static function getMaxUploadSize($max_size = 0) + { + $values = [self::convertBytes(ini_get('upload_max_filesize'))]; + + if ($max_size > 0) { + $values[] = $max_size; + } + + $post_max_size = self::convertBytes(ini_get('post_max_size')); + if ($post_max_size > 0) { + $values[] = $post_max_size; + } + + return min($values); + } } \ No newline at end of file