Skip to content

Commit 5db998e

Browse files
Restructured the config file & other minor tweaks (#99)
* Restructured the config file & other minor tweaks * Fixed the tests
1 parent cf07696 commit 5db998e

17 files changed

Lines changed: 47 additions & 74 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ jobs:
3838
- name: Install Dependencies
3939
run: composer install --no-interaction --prefer-dist --optimize-autoloader
4040

41-
- run: cp ./tests/Includes/Config.yaml ./src/Config/Config.yaml
42-
4341
- run: php src/Vendor/bin/phpunit tests
4442

4543
PHPStan:

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
![PHPStan Level](https://img.shields.io/badge/PHPStan-level%205-brightgreen)
2+
[![CI](https://github.com/AntCMS-org/AntCMS/actions/workflows/ci.yml/badge.svg)](https://github.com/AntCMS-org/AntCMS/actions/workflows/ci.yml)
23
![Supported PHP Versions](https://img.shields.io/badge/PHP%20Versions-8.0%7C8.1%7C8.2%7C8.3-brightgreen)
34

45
# AntCMS

src/AntCMS/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function requireAuth(): void
7676
setcookie("auth", "valid");
7777

7878
$siteInfo = Config::get('siteInfo');
79-
header('WWW-Authenticate: Basic realm="' . $siteInfo['siteTitle'] . '"');
79+
header('WWW-Authenticate: Basic realm="' . $siteInfo['title'] . '"');
8080
http_response_code(401);
8181
echo 'You must enter a valid username and password to access this page';
8282
exit;

src/AntCMS/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Cache
1616
*/
1717
public function __construct(null|string $mode = null)
1818
{
19-
$mode ??= Config::get('cacheMode') ?? 'auto';
19+
$mode ??= Config::get('performance.cacheMode') ?? 'auto';
2020
if ($mode == 'auto') {
2121
$mode = function_exists('apcu_enabled') && apcu_enabled() ? 'apcu' : 'filesystem';
2222
}

src/AntCMS/Config.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ class Config
1111
'performance',
1212
'forceHttps',
1313
'activeTheme',
14-
'cacheMode',
15-
'debug',
14+
'debugLevel',
1615
'baseUrl',
1716
'embed',
1817
];
@@ -24,17 +23,17 @@ public static function generateConfig(): void
2423
{
2524
$defaultOptions = [
2625
'siteInfo' => [
27-
'siteTitle' => 'AntCMS',
26+
'title' => 'AntCMS',
2827
],
2928
'performance' => [
3029
'doOutputCompression' => true,
3130
'compressTextAssets' => true,
3231
'compressImageAssets' => true,
32+
'cacheMode' => 'auto',
3333
],
3434
'forceHttps' => !Enviroment::isPHPDevServer(),
3535
'activeTheme' => 'Default',
36-
'cacheMode' => 'auto',
37-
'debug' => true,
36+
'debugLevel' => 1, // 0-2 at the moment
3837
'baseUrl' => $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']),
3938
'embed' => [
4039
'allowed_domains' => ['youtube.com', 'twitter.com', 'github.com', 'vimeo.com', 'flickr.com', 'instagram.com', 'facebook.com'],

src/AntCMS/Pages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static function generatePageInfo(string $path): array
2020
'title' => $pageHeader['title'],
2121
'realPath' => $path,
2222
'functionalPath' => $functionalPath,
23-
'url' => "//" . Tools::repairURL(baseUrl . $functionalPath),
23+
'url' => "//" . Tools::repairURL(BASE_URL . $functionalPath),
2424
'active' => $functionalPath === self::$currentPage,
2525
'navItem' => $pageHeader['NavItem'] !== 'false',
2626
];

src/AntCMS/Tools.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private static function compressImage(string $path, int $quality = 85): string
190190
public static function getAssetCacheKey(string $path): string
191191
{
192192
$encoding = CompressionBuffer::getFirstMethodChoice();
193-
if (compressTextAssets && self::isCompressableTextAsset($path)) {
193+
if (COMPRESS_TEXT_ASSETS && self::isCompressableTextAsset($path)) {
194194
return Cache::createCacheKeyFile($path, "assetCompression-$encoding");
195195
}
196196
return Cache::createCacheKeyFile($path, 'asset');
@@ -206,7 +206,7 @@ public static function doAssetCompression(string $path): array
206206
$cacheKey = self::getAssetCacheKey($path);
207207
$encoding = 'identity';
208208

209-
if (compressTextAssets && self::isCompressableTextAsset($path)) {
209+
if (COMPRESS_TEXT_ASSETS && self::isCompressableTextAsset($path)) {
210210
CompressionBuffer::enable(); // We will use CompressionBuffer to handle text content
211211
$encoding = CompressionBuffer::getFirstMethodChoice();
212212

@@ -217,7 +217,7 @@ public static function doAssetCompression(string $path): array
217217
});
218218
}
219219

220-
if (compressImageAssets && self::isCompressableImage($path)) {
220+
if (COMPRESS_IMAGES && self::isCompressableImage($path)) {
221221
$contents = $cache->get($cacheKey, function (ItemInterface $item) use ($path): string {
222222
$item->expiresAfter(604800);
223223
return self::compressImage($path);
@@ -245,6 +245,10 @@ private static function createDebugLogLine(string $wording, bool|string $value):
245245

246246
public static function buildDebugInfo(): string
247247
{
248+
if (DEBUG_LEVEL === 0) {
249+
return '';
250+
}
251+
248252
$elapsed_time = round((hrtime(true) - START) / 1e+6, 2);
249253
$mem_usage = round(memory_get_peak_usage() / 1e+6, 2);
250254

@@ -253,21 +257,23 @@ public static function buildDebugInfo(): string
253257
$result .= self::createDebugLogLine('Time to process request', "$elapsed_time ms");
254258
$result .= self::createDebugLogLine('Memory usage', "$mem_usage MB");
255259

256-
// System info
257-
$result .= "<dt>System Info</dt>";
258-
$result .= self::createDebugLogLine('Output compression', doOutputCompression);
259-
260-
if (CompressionBuffer::isEnabled() && doOutputCompression) {
261-
$method = CompressionBuffer::getFirstMethodChoice();
262-
if ($method === 'br') {
263-
$method = 'brotli';
260+
if (DEBUG_LEVEL >= 2) {
261+
// System info
262+
$result .= "<dt>System Info</dt>";
263+
$result .= self::createDebugLogLine('Output compression', COMPRESS_OUTPUT);
264+
265+
if (CompressionBuffer::isEnabled() && COMPRESS_OUTPUT) {
266+
$method = CompressionBuffer::getFirstMethodChoice();
267+
if ($method === 'br') {
268+
$method = 'brotli';
269+
}
270+
$result .= self::createDebugLogLine('This page was compressed with', $method);
264271
}
265-
$result .= self::createDebugLogLine('This page was compressed with', $method);
266-
}
267272

268-
$result .= self::createDebugLogLine('Asset compression', compressTextAssets);
269-
$result .= self::createDebugLogLine('Image compression', compressImageAssets);
270-
$result .= self::createDebugLogLine('PHP version', PHP_VERSION);
273+
$result .= self::createDebugLogLine('Asset compression', COMPRESS_TEXT_ASSETS);
274+
$result .= self::createDebugLogLine('Image compression', COMPRESS_IMAGES);
275+
$result .= self::createDebugLogLine('PHP version', PHP_VERSION);
276+
}
271277

272278
return $result . "</dl>";
273279
}

src/AntCMS/Twig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public static function registerTwig(?Environment $twigEnvironment = null): void
2828
new ArrayLoader(),
2929
]),
3030
[
31-
'cache' => (Config::get('enableCache') !== 'none') ? PATH_CACHE : false,
32-
'debug' => Config::get('debug'),
31+
'cache' => (Config::get('performance.cacheMode') !== 'none') ? PATH_CACHE : false,
32+
'debug' => DEBUG_LEVEL >= 2,
3333
'use_yield' => false,
3434
]
3535
);
3636
$twigEnvironment->addExtension(new TwigFilters());
37-
$twigEnvironment->addGlobal('AntCMSSiteTitle', Config::get('siteInfo')['siteTitle']);
37+
$twigEnvironment->addGlobal('AntCMSSiteTitle', Config::get('siteInfo')['title']);
3838
self::$twigEnvironment = $twigEnvironment;
3939
}
4040
}

src/AntCMS/TwigFilters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function getFilters(): array
1717

1818
public function absUrl(string $relative): string
1919
{
20-
return '//' . Tools::repairURL(baseUrl . '/' . trim($relative));
20+
return '//' . Tools::repairURL(BASE_URL . '/' . trim($relative));
2121
}
2222

2323
public function markdown(string $content): string

src/Bootstrap.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343

4444
// Define config-related constants
4545
$config = Config::get();
46-
define('compressTextAssets', $config['performance']['compressTextAssets']);
47-
define('doOutputCompression', $config['performance']['doOutputCompression']);
48-
define('compressImageAssets', $config['performance']['compressImageAssets']);
49-
define('baseUrl', $config['baseUrl']);
46+
define('COMPRESS_TEXT_ASSETS', $config['performance']['compressTextAssets']);
47+
define('COMPRESS_OUTPUT', $config['performance']['doOutputCompression']);
48+
define('COMPRESS_IMAGES', $config['performance']['compressImageAssets']);
49+
define('BASE_URL', $config['baseUrl']);
50+
define('DEBUG_LEVEL', $config['debugLevel']);

0 commit comments

Comments
 (0)