diff --git a/.github/workflows/test_mysql.yml b/.github/workflows/test_mysql.yml index 4078a8b5..a53aab33 100644 --- a/.github/workflows/test_mysql.yml +++ b/.github/workflows/test_mysql.yml @@ -55,9 +55,12 @@ jobs: - name: Install dependencies run: COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction - - name: Run test cases + - name: Run acceptance test cases run: sh ./test_sh + - name: Run integration test cases + run: php _vendor/bin/codecept run integration + - name: Output logs if: failure() run: cat _cache/test/*.log && cat _tests/_output/email/*.txt diff --git a/.github/workflows/test_postgres.yml b/.github/workflows/test_postgres.yml index 158d088a..13ca7fb6 100644 --- a/.github/workflows/test_postgres.yml +++ b/.github/workflows/test_postgres.yml @@ -40,9 +40,12 @@ jobs: - name: Install dependencies run: COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction - - name: Run test cases + - name: Run acceptance test cases run: sh ./test_sh + - name: Run integration test cases + run: php _vendor/bin/codecept run integration + - name: Output logs if: failure() run: cat _cache/test/*.log && cat _tests/_output/email/*.txt diff --git a/.github/workflows/test_sqlite.yml b/.github/workflows/test_sqlite.yml index 88172661..c6fe9ad1 100644 --- a/.github/workflows/test_sqlite.yml +++ b/.github/workflows/test_sqlite.yml @@ -32,9 +32,12 @@ jobs: - name: Install dependencies run: COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction - - name: Run test cases + - name: Run acceptance test cases run: sh ./test_sh + - name: Run integration test cases + run: php _vendor/bin/codecept run integration + - name: Output logs if: failure() run: cat _cache/test/*.log && cat _tests/_output/email/*.txt diff --git a/_admin/extensions.php b/_admin/extensions.php index 27349059..c68dbe69 100644 --- a/_admin/extensions.php +++ b/_admin/extensions.php @@ -10,6 +10,7 @@ */ +use S2\Cms\Model\ExtensionCache; use S2\Cms\Pdo\DbLayer; if (!defined('S2_ROOT')) @@ -359,10 +360,11 @@ function s2_install_extension ($id) } } - // Regenerate the hooks cache - S2Cache::clear(); - S2Cache::generate_hooks(); + /** @var ExtensionCache $cache */ + $cache = \Container::get(ExtensionCache::class); + $cache->clear(); // TODO also clear DynamicConfigProvider cache + $cache->generateHooks(); return $messages; } @@ -458,9 +460,11 @@ function s2_flip_extension ($id) $s2_db->buildAndQuery($query); - // Regenerate the hooks cache - S2Cache::clear(); - S2Cache::generate_hooks(); + // Regenerate the hooks cache + /** @var ExtensionCache $cache */ + $cache = \Container::get(ExtensionCache::class); + $cache->clear(); // TODO also clear DynamicConfigProvider cache + $cache->generateHooks(); ($hook = s2_hook('fn_flip_extension_end')) ? eval($hook) : null; @@ -539,9 +543,11 @@ function s2_uninstall_extension ($id) $s2_db->buildAndQuery($query); - // Regenerate the hooks cache - S2Cache::clear(); - S2Cache::generate_hooks(); + // Regenerate the hooks cache + /** @var ExtensionCache $cache */ + $cache = \Container::get(ExtensionCache::class); + $cache->clear(); // TODO also clear DynamicConfigProvider cache + $cache->generateHooks(); ($hook = s2_hook('fn_uninstall_extension_end')) ? eval($hook) : null; diff --git a/_admin/info.php b/_admin/info.php index 2f1118dd..f4d8baa7 100644 --- a/_admin/info.php +++ b/_admin/info.php @@ -114,7 +114,7 @@ function s2_stat_info () $version = array( 'S2 '.S2_VERSION.' ↑', - '© 2007–2023 Roman Parpalak', + '© 2007–2024 Roman Parpalak', ); $environment = array( diff --git a/_admin/install.php b/_admin/install.php index 0a02960e..13f810a0 100644 --- a/_admin/install.php +++ b/_admin/install.php @@ -14,6 +14,7 @@ use S2\Cms\CmsExtension; use S2\Cms\Framework\Application; use S2\Cms\Logger\Logger; +use S2\Cms\Model\ExtensionCache; use S2\Cms\Pdo\DbLayer; use S2\Cms\Pdo\DbLayerException; use Symfony\Component\ErrorHandler\Debug; @@ -68,7 +69,7 @@ function generate_config_file () foreach (array('', '/?', '/index.php', '/index.php?') as $prefix) { $url_prefix = $prefix; $content = s2_get_remote_file($base_url.$url_prefix.'/this/URL/_DoEs_/_NoT_/_eXiSt', 1, false, 10, true); - if ($content !== null && false !== strpos($content['content'], '')) { + if ($content !== null && str_contains($content['content'], '')) { break; } } @@ -76,13 +77,14 @@ function generate_config_file () $path = preg_replace('#^[^:/]+://[^/]*#', '', $base_url); $use_https = false; - if (substr($base_url, 0, 8) == 'https://') - $use_https = true; - else - { + if (str_starts_with($base_url, 'https://')) { + $use_https = true; + } + else { $content = s2_get_remote_file('https://'.substr($base_url, 7).$url_prefix.'/this/URL/_DoEs_/_NoT_/_eXiSt', 1, false, 10, true); - if ($content !== null && false !== strpos($content['content'], '')) - $use_https = true; + if ($content !== null && str_contains($content['content'], '')) { + $use_https = true; + } } return 'boot((static function (): array { $result = [ - 'root_dir' => S2_ROOT, - 'cache_dir' => S2_CACHE_DIR, - 'log_dir' => defined('S2_LOG_DIR') ? S2_LOG_DIR : S2_CACHE_DIR, - 'base_url' => defined('S2_BASE_URL') ? S2_BASE_URL : null, - 'base_path' => defined('S2_PATH') ? S2_PATH : null, - 'debug' => defined('S2_DEBUG'), - 'debug_view' => defined('S2_DEBUG_VIEW'), - 'redirect_map' => [], + 'root_dir' => S2_ROOT, + 'cache_dir' => S2_CACHE_DIR, + 'disable_cache' => false, + 'log_dir' => defined('S2_LOG_DIR') ? S2_LOG_DIR : S2_CACHE_DIR, + 'base_url' => defined('S2_BASE_URL') ? S2_BASE_URL : null, + 'base_path' => defined('S2_PATH') ? S2_PATH : null, + 'debug' => defined('S2_DEBUG'), + 'debug_view' => defined('S2_DEBUG_VIEW'), + 'redirect_map' => [], ]; foreach (['db_type', 'db_host', 'db_name', 'db_username', 'db_password', 'db_prefix', 'p_connect'] as $globalVarName) { @@ -570,7 +573,9 @@ function get_preferred_lang ($languages) $s2_db->close(); - S2Cache::clear(); + /** @var ExtensionCache $cache */ + $cache = \Container::get(ExtensionCache::class); + $cache->clear(); $alerts = array(); // Check if the cache directory is writable diff --git a/_admin/site_ajax.php b/_admin/site_ajax.php index dab8ee1c..ed921b54 100644 --- a/_admin/site_ajax.php +++ b/_admin/site_ajax.php @@ -10,6 +10,7 @@ */ use S2\Cms\Model\Model; +use S2\Cms\Model\ExtensionCache; use S2\Cms\Pdo\DbLayer; define('S2_ROOT', '../'); @@ -766,10 +767,11 @@ ($hook = s2_hook('rq_action_refresh_hooks_start')) ? eval($hook) : null; s2_test_user_rights($is_permission); - S2Cache::generate_hooks(); - /** @var DbLayer $s2_db */ - $s2_db = \Container::get(DbLayer::class); - S2Cache::generateEnabledExtensionClassNames($s2_db); + // Regenerate the hooks cache + /** @var ExtensionCache $cache */ + $cache = \Container::get(ExtensionCache::class); + $cache->generateHooks(); + $cache->generateEnabledExtensionClassNames(); } elseif ($action == 'uninstall_extension') diff --git a/_include/S2Cache.php b/_include/S2Cache.php deleted file mode 100644 index bb7e58a2..00000000 --- a/_include/S2Cache.php +++ /dev/null @@ -1,130 +0,0 @@ -buildAndQuery([ - 'SELECT' => 'id', - 'FROM' => 'extensions', - 'WHERE' => 'disabled=0', - ]); - - $extensionClassNames = []; - while ($extension = $dbLayer->fetchAssoc($result)) { - $className = sprintf('\s2_extensions\%s\Extension', $extension['id']); - if (class_exists($className)) { - $extensionClassNames[] = $className; - } - } - - // Output hooks as PHP code - try { - s2_overwrite_file_skip_locked(self::CACHE_ENABLED_EXTENSIONS_FILENAME, " 'e.id', - 'FROM' => 'extensions AS e', - 'WHERE' => 'e.disabled=0', - ); - - $result = $s2_db->buildAndQuery($query); - - $map = []; - while ($extension = $s2_db->fetchAssoc($result)) { - $hooks = glob(S2_ROOT . '_extensions/' . $extension['id'] . '/hooks/*.php'); - foreach ($hooks as $filename) { - if (1 !== preg_match($regex = '#/([a-z_\-0-9]+?)(?:_(\d))?\.php$#S', $filename, $matches)) { - throw new RuntimeException(sprintf('Found invalid characters in hook filename "%s". Allowed name must match %s.', $filename, $regex)); - } - $priority = (int)($matches[2] ?? 5); - $hookName = $matches[1]; - - // Structure - $map[$hookName][$priority][] = '_extensions/' . $extension['id'] . '/hooks' . $matches[0]; - } - } - - array_walk($map, static function (&$mapItem) { - // Sort by priority - ksort($mapItem); - // Remove grouping by priority - $mapItem = array_merge(...$mapItem); - }); - - if (defined('S2_DISABLE_CACHE')) { - return $map; - } - - $cacheHookNamesContent = " S2_ROOT, - 'cache_dir' => S2_CACHE_DIR, - 'log_dir' => defined('S2_LOG_DIR') ? S2_LOG_DIR : S2_CACHE_DIR, - 'base_url' => defined('S2_BASE_URL') ? S2_BASE_URL : null, - 'base_path' => defined('S2_PATH') ? S2_PATH : null, - 'url_prefix' => defined('S2_URL_PREFIX') ? S2_URL_PREFIX : null, - 'debug' => defined('S2_DEBUG'), - 'debug_view' => defined('S2_DEBUG_VIEW'), - 'show_queries' => defined('S2_SHOW_QUERIES'), - 'redirect_map' => $GLOBALS['s2_redirect'] ?? [], + 'root_dir' => S2_ROOT, + 'cache_dir' => S2_CACHE_DIR, + 'disable_cache' => defined('S2_DISABLE_CACHE'), + 'log_dir' => defined('S2_LOG_DIR') ? S2_LOG_DIR : S2_CACHE_DIR, + 'base_url' => defined('S2_BASE_URL') ? S2_BASE_URL : null, + 'base_path' => defined('S2_PATH') ? S2_PATH : null, + 'url_prefix' => defined('S2_URL_PREFIX') ? S2_URL_PREFIX : null, + 'debug' => defined('S2_DEBUG'), + 'debug_view' => defined('S2_DEBUG_VIEW'), + 'show_queries' => defined('S2_SHOW_QUERIES'), + 'redirect_map' => $GLOBALS['s2_redirect'] ?? [], ]; foreach (['db_type', 'db_host', 'db_name', 'db_username', 'db_password', 'db_prefix', 'p_connect'] as $globalVarName) { @@ -68,26 +84,30 @@ function collectParameters(): array $app = new Application(); $app->addExtension(new CmsExtension()); -if (!defined('S2_DISABLE_CACHE')) { - $app->setCachedRoutesFilename(S2Cache::CACHE_ROUTES_FILENAME); -} - $enabledExtensions = null; -if (!defined('S2_DISABLE_CACHE') && file_exists(S2Cache::CACHE_ENABLED_EXTENSIONS_FILENAME)) { - $enabledExtensions = include S2Cache::CACHE_ENABLED_EXTENSIONS_FILENAME; +if (!defined('S2_DISABLE_CACHE') && file_exists(S2_CACHE_DIR.ExtensionCache::CACHE_ENABLED_EXTENSIONS_FILENAME)) { + $enabledExtensions = include S2_CACHE_DIR.ExtensionCache::CACHE_ENABLED_EXTENSIONS_FILENAME; } try { if (!is_array($enabledExtensions)) { $app->boot(collectParameters()); \Container::setContainer($app->container); - $enabledExtensions = S2Cache::generateEnabledExtensionClassNames($app->container->get(\S2\Cms\Pdo\DbLayer::class)); + /** @var ExtensionCache $appCache */ + $appCache = $app->container->get(ExtensionCache::class); + $enabledExtensions = $appCache->generateEnabledExtensionClassNames(); } foreach ($enabledExtensions as $extension) { $app->addExtension(new $extension()); } $app->boot(collectParameters()); + /** @var ExtensionCache $appCache */ + $appCache = $app->container->get(ExtensionCache::class); + if (!defined('S2_DISABLE_CACHE')) { + $app->setCachedRoutesFilename($appCache->getCachedRoutesFilename()); + } + \Container::setContainer($app->container); $app->container->getParameter('base_url'); } catch (\S2\Cms\Framework\Exception\ParameterNotFoundException $e) { @@ -100,20 +120,6 @@ function collectParameters(): array } $errorHandler->setDefaultLogger($app->container->get(LoggerInterface::class)); -if (!defined('S2_URL_PREFIX')) { - define('S2_URL_PREFIX', ''); -} - -// If the image directory is not specified, we use the default setting -if (!defined('S2_IMG_DIR')) { - define('S2_IMG_DIR', '_pictures'); -} -define('S2_IMG_PATH', S2_ROOT . S2_IMG_DIR); - -if (!defined('S2_ALLOWED_EXTENSIONS')) { - define('S2_ALLOWED_EXTENSIONS', 'gif bmp jpg jpeg png ico svg mp3 wav avi flv mpg mpeg mkv zip 7z doc pdf'); -} - // Load cached config if (file_exists(S2_CACHE_DIR . 'cache_config.php')) { include S2_CACHE_DIR . 'cache_config.php'; diff --git a/_include/functions.php b/_include/functions.php index f729390d..c199c0bf 100644 --- a/_include/functions.php +++ b/_include/functions.php @@ -8,6 +8,7 @@ */ +use S2\Cms\Model\ExtensionCache; use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayerException; @@ -117,20 +118,6 @@ function s2_paging($page, $total_pages, $url, &$link_nav) } -function s2_build_copyright() -{ - global $request_uri; - - $author = S2_WEBMASTER ? S2_WEBMASTER : S2_SITE_NAME; - $copyright = S2_WEBMASTER && S2_WEBMASTER_EMAIL ? s2_js_mailto($author, S2_WEBMASTER_EMAIL) : ($request_uri !== '/' ? '' . $author . '' : $author); - - return (S2_START_YEAR != date('Y') ? - sprintf(Lang::get('Copyright 2'), $copyright, S2_START_YEAR, date('Y')) : - sprintf(Lang::get('Copyright 1'), $copyright, date('Y'))) . ' ' . - sprintf(Lang::get('Powered by'), 'S2'); -} - - // // Encodes the contents of $str so that they are safe to output on an (X)HTML page // @@ -345,12 +332,9 @@ function s2_hook($hook_id) static $hookNames = null; if ($hookNames === null) { - if (!defined('S2_DISABLE_CACHE') && file_exists(S2Cache::CACHE_HOOK_NAMES_FILENAME)) { - $hookNames = include S2Cache::CACHE_HOOK_NAMES_FILENAME; - } - if (!is_array($hookNames)) { - $hookNames = S2Cache::generate_hooks(); - } + /** @var ExtensionCache $cache */ + $cache = \Container::get(ExtensionCache::class); + $hookNames = $cache->getHookNames(); } if (!isset($hookNames[$hook_id])) { @@ -419,7 +403,7 @@ function error() - + Error - <?php echo s2_htmlencode(S2_SITE_NAME); ?>