From 9c33fa76bb07537d3e70f6939446bf6b1f2a8db6 Mon Sep 17 00:00:00 2001 From: Dag Date: Fri, 30 Aug 2024 02:15:15 +0200 Subject: [PATCH] fix: extract duplicate config loading Also fix a problem with bin/cache-prune and FileCache and its enable_purge option --- actions/DisplayAction.php | 4 +++- bin/cache-clear | 11 +--------- bin/cache-prune | 20 +++++++++---------- bin/test | 11 +--------- caches/FileCache.php | 2 ++ index.php | 13 ++---------- lib/Configuration.php | 42 ++++----------------------------------- lib/config.php | 13 ++++++++++++ 8 files changed, 36 insertions(+), 80 deletions(-) create mode 100644 lib/config.php diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index 9749004f18b..bda45558457 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -89,7 +89,9 @@ public function __invoke(Request $request): Response $this->cache->set($cacheKey, $response, 60 * 15); } - if (rand(1, 100) === 2) { + // For 1% of requests, prune cache + if (rand(1, 100) === 1) { + // This might be resource intensive! $this->cache->prune(); } diff --git a/bin/cache-clear b/bin/cache-clear index 7146636035d..c8f5312298e 100755 --- a/bin/cache-clear +++ b/bin/cache-clear @@ -6,16 +6,7 @@ */ require __DIR__ . '/../lib/bootstrap.php'; - -$config = []; -if (file_exists(__DIR__ . '/../config.ini.php')) { - $config = parse_ini_file(__DIR__ . '/../config.ini.php', true, INI_SCANNER_TYPED); - if (!$config) { - http_response_code(500); - exit("Error parsing config.ini.php\n"); - } -} -Configuration::loadConfiguration($config, getenv()); +require __DIR__ . '/../lib/config.php'; $container = require __DIR__ . '/../lib/dependencies.php'; diff --git a/bin/cache-prune b/bin/cache-prune index 37696e14637..755ed8d5832 100755 --- a/bin/cache-prune +++ b/bin/cache-prune @@ -6,19 +6,19 @@ */ require __DIR__ . '/../lib/bootstrap.php'; - -$config = []; -if (file_exists(__DIR__ . '/../config.ini.php')) { - $config = parse_ini_file(__DIR__ . '/../config.ini.php', true, INI_SCANNER_TYPED); - if (!$config) { - http_response_code(500); - exit("Error parsing config.ini.php\n"); - } -} -Configuration::loadConfiguration($config, getenv()); +require __DIR__ . '/../lib/config.php'; $container = require __DIR__ . '/../lib/dependencies.php'; +/** @var CacheInterface $cache */ $cache = $container['cache']; +if ( + Configuration::getConfig('cache', 'type') === 'file' + && !Configuration::getConfig('FileCache', 'enable_purge') +) { + // Override enable_purge for this execution + Configuration::setConfig('FileCache', 'enable_purge', true); +} + $cache->prune(); diff --git a/bin/test b/bin/test index f3556fc1fb1..746924107a9 100755 --- a/bin/test +++ b/bin/test @@ -6,16 +6,7 @@ */ require __DIR__ . '/../lib/bootstrap.php'; - -$config = []; -if (file_exists(__DIR__ . '/../config.ini.php')) { - $config = parse_ini_file(__DIR__ . '/../config.ini.php', true, INI_SCANNER_TYPED); - if (!$config) { - http_response_code(500); - exit("Error parsing config.ini.php\n"); - } -} -Configuration::loadConfiguration($config, getenv()); +require __DIR__ . '/../lib/config.php'; $container = require __DIR__ . '/../lib/dependencies.php'; diff --git a/caches/FileCache.php b/caches/FileCache.php index 7a0eb81d95e..dfd295e8f56 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -97,8 +97,10 @@ public function prune(): void } $expiration = $item['expiration'] ?? time(); if ($expiration === 0 || $expiration > time()) { + // Cached forever, or not expired yet continue; } + // Expired, so delete file unlink($cacheFile); } } diff --git a/index.php b/index.php index 7b44194493b..e67b0c9f99b 100644 --- a/index.php +++ b/index.php @@ -5,17 +5,8 @@ exit("RSS-Bridge requires minimum PHP version 7.4\n"); } -require_once __DIR__ . '/lib/bootstrap.php'; - -$config = []; -if (file_exists(__DIR__ . '/config.ini.php')) { - $config = parse_ini_file(__DIR__ . '/config.ini.php', true, INI_SCANNER_TYPED); - if (!$config) { - http_response_code(500); - exit("Error parsing config.ini.php\n"); - } -} -Configuration::loadConfiguration($config, getenv()); +require __DIR__ . '/lib/bootstrap.php'; +require __DIR__ . '/lib/config.php'; $container = require __DIR__ . '/lib/dependencies.php'; diff --git a/lib/Configuration.php b/lib/Configuration.php index b104a251ec6..44fd36123c1 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -15,43 +15,6 @@ private function __construct() { } - public static function checkInstallation(): array - { - $errors = []; - - // OpenSSL: https://www.php.net/manual/en/book.openssl.php - if (!extension_loaded('openssl')) { - $errors[] = 'openssl extension not loaded'; - } - - // libxml: https://www.php.net/manual/en/book.libxml.php - if (!extension_loaded('libxml')) { - $errors[] = 'libxml extension not loaded'; - } - - // Multibyte String (mbstring): https://www.php.net/manual/en/book.mbstring.php - if (!extension_loaded('mbstring')) { - $errors[] = 'mbstring extension not loaded'; - } - - // SimpleXML: https://www.php.net/manual/en/book.simplexml.php - if (!extension_loaded('simplexml')) { - $errors[] = 'simplexml extension not loaded'; - } - - // Client URL Library (curl): https://www.php.net/manual/en/book.curl.php - // Allow RSS-Bridge to run without curl module in CLI mode without root certificates - if (!extension_loaded('curl') && !(php_sapi_name() === 'cli' && empty(ini_get('curl.cainfo')))) { - $errors[] = 'curl extension not loaded'; - } - - // JavaScript Object Notation (json): https://www.php.net/manual/en/book.json.php - if (!extension_loaded('json')) { - $errors[] = 'json extension not loaded'; - } - return $errors; - } - public static function loadConfiguration(array $customConfig = [], array $env = []) { if (!file_exists(__DIR__ . '/../config.default.ini.php')) { @@ -204,7 +167,10 @@ public static function getConfig(string $section, string $key, $default = null) return self::$config[strtolower($section)][strtolower($key)] ?? $default; } - private static function setConfig(string $section, string $key, $value): void + /** + * @internal Please avoid usage + */ + public static function setConfig(string $section, string $key, $value): void { self::$config[strtolower($section)][strtolower($key)] = $value; } diff --git a/lib/config.php b/lib/config.php new file mode 100644 index 00000000000..4ff72565268 --- /dev/null +++ b/lib/config.php @@ -0,0 +1,13 @@ +