From 5db998e4748ed8dde5e9b52625bab81ff3ad1307 Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Fri, 26 Apr 2024 13:46:11 -0700 Subject: [PATCH] Restructured the config file & other minor tweaks (#99) * Restructured the config file & other minor tweaks * Fixed the tests --- .github/workflows/ci.yml | 2 - readme.md | 1 + src/AntCMS/Auth.php | 2 +- src/AntCMS/Cache.php | 2 +- src/AntCMS/Config.php | 9 ++--- src/AntCMS/Pages.php | 2 +- src/AntCMS/Tools.php | 38 +++++++++++-------- src/AntCMS/Twig.php | 6 +-- src/AntCMS/TwigFilters.php | 2 +- src/Bootstrap.php | 9 +++-- src/Plugins/Robotstxt/Controller.php | 2 +- src/Plugins/Sitemap/Controller.php | 2 +- .../Default/Templates/default.html.twig | 5 +-- src/index.php | 11 ++---- tests/ConfigTest.php | 11 ------ tests/Includes/Config.yaml | 12 ------ tests/MarkdownTest.php | 5 +-- 17 files changed, 47 insertions(+), 74 deletions(-) delete mode 100644 tests/Includes/Config.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9193d27..8d52300 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,8 +38,6 @@ jobs: - name: Install Dependencies run: composer install --no-interaction --prefer-dist --optimize-autoloader - - run: cp ./tests/Includes/Config.yaml ./src/Config/Config.yaml - - run: php src/Vendor/bin/phpunit tests PHPStan: diff --git a/readme.md b/readme.md index ff011d0..de49fbd 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,5 @@ ![PHPStan Level](https://img.shields.io/badge/PHPStan-level%205-brightgreen) +[![CI](https://github.com/AntCMS-org/AntCMS/actions/workflows/ci.yml/badge.svg)](https://github.com/AntCMS-org/AntCMS/actions/workflows/ci.yml) ![Supported PHP Versions](https://img.shields.io/badge/PHP%20Versions-8.0%7C8.1%7C8.2%7C8.3-brightgreen) # AntCMS diff --git a/src/AntCMS/Auth.php b/src/AntCMS/Auth.php index a03f5e3..841b051 100644 --- a/src/AntCMS/Auth.php +++ b/src/AntCMS/Auth.php @@ -76,7 +76,7 @@ private function requireAuth(): void setcookie("auth", "valid"); $siteInfo = Config::get('siteInfo'); - header('WWW-Authenticate: Basic realm="' . $siteInfo['siteTitle'] . '"'); + header('WWW-Authenticate: Basic realm="' . $siteInfo['title'] . '"'); http_response_code(401); echo 'You must enter a valid username and password to access this page'; exit; diff --git a/src/AntCMS/Cache.php b/src/AntCMS/Cache.php index d2ed0e6..4e03788 100644 --- a/src/AntCMS/Cache.php +++ b/src/AntCMS/Cache.php @@ -16,7 +16,7 @@ class Cache */ public function __construct(null|string $mode = null) { - $mode ??= Config::get('cacheMode') ?? 'auto'; + $mode ??= Config::get('performance.cacheMode') ?? 'auto'; if ($mode == 'auto') { $mode = function_exists('apcu_enabled') && apcu_enabled() ? 'apcu' : 'filesystem'; } diff --git a/src/AntCMS/Config.php b/src/AntCMS/Config.php index e2ee770..e59266e 100644 --- a/src/AntCMS/Config.php +++ b/src/AntCMS/Config.php @@ -11,8 +11,7 @@ class Config 'performance', 'forceHttps', 'activeTheme', - 'cacheMode', - 'debug', + 'debugLevel', 'baseUrl', 'embed', ]; @@ -24,17 +23,17 @@ public static function generateConfig(): void { $defaultOptions = [ 'siteInfo' => [ - 'siteTitle' => 'AntCMS', + 'title' => 'AntCMS', ], 'performance' => [ 'doOutputCompression' => true, 'compressTextAssets' => true, 'compressImageAssets' => true, + 'cacheMode' => 'auto', ], 'forceHttps' => !Enviroment::isPHPDevServer(), 'activeTheme' => 'Default', - 'cacheMode' => 'auto', - 'debug' => true, + 'debugLevel' => 1, // 0-2 at the moment 'baseUrl' => $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']), 'embed' => [ 'allowed_domains' => ['youtube.com', 'twitter.com', 'github.com', 'vimeo.com', 'flickr.com', 'instagram.com', 'facebook.com'], diff --git a/src/AntCMS/Pages.php b/src/AntCMS/Pages.php index edfff03..e13d3ce 100644 --- a/src/AntCMS/Pages.php +++ b/src/AntCMS/Pages.php @@ -20,7 +20,7 @@ private static function generatePageInfo(string $path): array 'title' => $pageHeader['title'], 'realPath' => $path, 'functionalPath' => $functionalPath, - 'url' => "//" . Tools::repairURL(baseUrl . $functionalPath), + 'url' => "//" . Tools::repairURL(BASE_URL . $functionalPath), 'active' => $functionalPath === self::$currentPage, 'navItem' => $pageHeader['NavItem'] !== 'false', ]; diff --git a/src/AntCMS/Tools.php b/src/AntCMS/Tools.php index a0f93f4..62e5cf1 100644 --- a/src/AntCMS/Tools.php +++ b/src/AntCMS/Tools.php @@ -190,7 +190,7 @@ private static function compressImage(string $path, int $quality = 85): string public static function getAssetCacheKey(string $path): string { $encoding = CompressionBuffer::getFirstMethodChoice(); - if (compressTextAssets && self::isCompressableTextAsset($path)) { + if (COMPRESS_TEXT_ASSETS && self::isCompressableTextAsset($path)) { return Cache::createCacheKeyFile($path, "assetCompression-$encoding"); } return Cache::createCacheKeyFile($path, 'asset'); @@ -206,7 +206,7 @@ public static function doAssetCompression(string $path): array $cacheKey = self::getAssetCacheKey($path); $encoding = 'identity'; - if (compressTextAssets && self::isCompressableTextAsset($path)) { + if (COMPRESS_TEXT_ASSETS && self::isCompressableTextAsset($path)) { CompressionBuffer::enable(); // We will use CompressionBuffer to handle text content $encoding = CompressionBuffer::getFirstMethodChoice(); @@ -217,7 +217,7 @@ public static function doAssetCompression(string $path): array }); } - if (compressImageAssets && self::isCompressableImage($path)) { + if (COMPRESS_IMAGES && self::isCompressableImage($path)) { $contents = $cache->get($cacheKey, function (ItemInterface $item) use ($path): string { $item->expiresAfter(604800); return self::compressImage($path); @@ -245,6 +245,10 @@ private static function createDebugLogLine(string $wording, bool|string $value): public static function buildDebugInfo(): string { + if (DEBUG_LEVEL === 0) { + return ''; + } + $elapsed_time = round((hrtime(true) - START) / 1e+6, 2); $mem_usage = round(memory_get_peak_usage() / 1e+6, 2); @@ -253,21 +257,23 @@ public static function buildDebugInfo(): string $result .= self::createDebugLogLine('Time to process request', "$elapsed_time ms"); $result .= self::createDebugLogLine('Memory usage', "$mem_usage MB"); - // System info - $result .= "
System Info
"; - $result .= self::createDebugLogLine('Output compression', doOutputCompression); - - if (CompressionBuffer::isEnabled() && doOutputCompression) { - $method = CompressionBuffer::getFirstMethodChoice(); - if ($method === 'br') { - $method = 'brotli'; + if (DEBUG_LEVEL >= 2) { + // System info + $result .= "
System Info
"; + $result .= self::createDebugLogLine('Output compression', COMPRESS_OUTPUT); + + if (CompressionBuffer::isEnabled() && COMPRESS_OUTPUT) { + $method = CompressionBuffer::getFirstMethodChoice(); + if ($method === 'br') { + $method = 'brotli'; + } + $result .= self::createDebugLogLine('This page was compressed with', $method); } - $result .= self::createDebugLogLine('This page was compressed with', $method); - } - $result .= self::createDebugLogLine('Asset compression', compressTextAssets); - $result .= self::createDebugLogLine('Image compression', compressImageAssets); - $result .= self::createDebugLogLine('PHP version', PHP_VERSION); + $result .= self::createDebugLogLine('Asset compression', COMPRESS_TEXT_ASSETS); + $result .= self::createDebugLogLine('Image compression', COMPRESS_IMAGES); + $result .= self::createDebugLogLine('PHP version', PHP_VERSION); + } return $result . ""; } diff --git a/src/AntCMS/Twig.php b/src/AntCMS/Twig.php index 5307556..4ecef80 100644 --- a/src/AntCMS/Twig.php +++ b/src/AntCMS/Twig.php @@ -28,13 +28,13 @@ public static function registerTwig(?Environment $twigEnvironment = null): void new ArrayLoader(), ]), [ - 'cache' => (Config::get('enableCache') !== 'none') ? PATH_CACHE : false, - 'debug' => Config::get('debug'), + 'cache' => (Config::get('performance.cacheMode') !== 'none') ? PATH_CACHE : false, + 'debug' => DEBUG_LEVEL >= 2, 'use_yield' => false, ] ); $twigEnvironment->addExtension(new TwigFilters()); - $twigEnvironment->addGlobal('AntCMSSiteTitle', Config::get('siteInfo')['siteTitle']); + $twigEnvironment->addGlobal('AntCMSSiteTitle', Config::get('siteInfo')['title']); self::$twigEnvironment = $twigEnvironment; } } diff --git a/src/AntCMS/TwigFilters.php b/src/AntCMS/TwigFilters.php index 009ca19..4f50c59 100644 --- a/src/AntCMS/TwigFilters.php +++ b/src/AntCMS/TwigFilters.php @@ -17,7 +17,7 @@ public function getFilters(): array public function absUrl(string $relative): string { - return '//' . Tools::repairURL(baseUrl . '/' . trim($relative)); + return '//' . Tools::repairURL(BASE_URL . '/' . trim($relative)); } public function markdown(string $content): string diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 35d75e0..37d55d7 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -43,7 +43,8 @@ // Define config-related constants $config = Config::get(); -define('compressTextAssets', $config['performance']['compressTextAssets']); -define('doOutputCompression', $config['performance']['doOutputCompression']); -define('compressImageAssets', $config['performance']['compressImageAssets']); -define('baseUrl', $config['baseUrl']); +define('COMPRESS_TEXT_ASSETS', $config['performance']['compressTextAssets']); +define('COMPRESS_OUTPUT', $config['performance']['doOutputCompression']); +define('COMPRESS_IMAGES', $config['performance']['compressImageAssets']); +define('BASE_URL', $config['baseUrl']); +define('DEBUG_LEVEL', $config['debugLevel']); diff --git a/src/Plugins/Robotstxt/Controller.php b/src/Plugins/Robotstxt/Controller.php index 95f5dbb..2146ec2 100644 --- a/src/Plugins/Robotstxt/Controller.php +++ b/src/Plugins/Robotstxt/Controller.php @@ -13,7 +13,7 @@ public function __construct() Flight::route("GET /robots.txt", function (): void { $protocol = Config::get('forceHttps') ? 'https' : Flight::request()->scheme; echo 'User-agent: *' . PHP_EOL; - echo 'Sitemap: ' . $protocol . '://' . Tools::repairURL(baseUrl . '/sitemap.xml' . PHP_EOL); + echo 'Sitemap: ' . $protocol . '://' . Tools::repairURL(BASE_URL . '/sitemap.xml' . PHP_EOL); Flight::response()->setHeader('Content-Type', 'text/plain'); }); } diff --git a/src/Plugins/Sitemap/Controller.php b/src/Plugins/Sitemap/Controller.php index c3328ca..3a4cf9a 100644 --- a/src/Plugins/Sitemap/Controller.php +++ b/src/Plugins/Sitemap/Controller.php @@ -29,7 +29,7 @@ public function __construct() foreach ($urls as $url) { $element = $domDocument->createElement('url'); - $loc = $domDocument->createElement('loc', $protocol . '://' . Tools::repairURL(baseUrl . $url['url'])); + $loc = $domDocument->createElement('loc', $protocol . '://' . Tools::repairURL(BASE_URL . $url['url'])); $element->appendChild($loc); $lastmod = $domDocument->createElement('lastmod', $url['lastchange']); diff --git a/src/Themes/Default/Templates/default.html.twig b/src/Themes/Default/Templates/default.html.twig index 1e7ac94..a22fd6f 100644 --- a/src/Themes/Default/Templates/default.html.twig +++ b/src/Themes/Default/Templates/default.html.twig @@ -72,10 +72,9 @@ -