* @copyright Copyright 2020-2023 © Teamwant Mateusz Szymański All right reserved * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * @category Teamwant * @package Teamwant */ namespace Teamwant_redis\OverrideSrc; use Tools; class OverrideHookFile implements OverrideSrc { const FILE = _PS_ROOT_DIR_ . '/classes/Hook.php'; public static function install() { $filecontent = Tools::file_get_contents(self::FILE); $override = 0; $re = '/if \(Module::isEnabled\(\'teamwant_redis\'\)\) \{/s'; preg_match($re, $filecontent, $matches); if (!count($matches)) { $override = 1; $re = '/(function getAllHookIds(.*?))\$result(.*?)\;/s'; $subst = '$1 $useTwCache = false; try { if (_PS_CACHE_ENABLED_) { if (_PS_CACHING_SYSTEM_ === \'Redis\') { $use_cache_for_hook_list = Teamwant_Redis::getCacheConfiguration()[\'use_cache_for_hook_list\']; if ($use_cache_for_hook_list) { $useTwCache = true; $cache = Cache::getInstance(); if (($r = $cache->get($cache->getQueryHash($sql))) !== false) { return $r; } } } } } catch (Throwable $thr) { if (defined(\'_PS_MODE_DEV_\') && _PS_MODE_DEV_) { throw new \PrestaShopException($thr->getMessage()); } } $result = $db->executeS($sql, false, true); '; $filecontent = preg_replace($re, $subst, $filecontent, 1); $re = '/function getAllHookIds(.*?)Cache::store\(\$cacheId\, \$hookIds\)\;/s'; $subst = '$0 if ($useTwCache) { $cache = Cache::getInstance(); $cache->set($cache->getQueryHash($sql), $hookIds); } '; $filecontent = preg_replace($re, $subst, $filecontent, 1); } if ($override) { file_put_contents(self::FILE, $filecontent); } } public static function uninstall() { //todo uninstall function //$filecontent = Tools::file_get_contents(self::FILE); // ////replace 1 //$re = '/\$this\-\>extensionsList \+\= \\\\Dispatcher\:\:teamwantRedisGetExtensionsListCachingType\(\)\;\n(\s|\t)+/s'; //$filecontent = preg_replace($re, '', $filecontent, 1); // ////replace 2 //$re = '/ \+ \\\\Dispatcher\:\:teamwantRedisGetAvailableCachingType\(\)/s'; //$filecontent = preg_replace($re, '', $filecontent, 1); // //file_put_contents(self::FILE, $filecontent); } }