Merge pull request #10 from AntCMS-org/currentconfig

Refactor AntConfig::currentConfig
This commit is contained in:
Belle Aerni 2023-01-10 17:26:46 -08:00 committed by GitHub
commit 4c0a950179
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 35 deletions

View file

@ -13,7 +13,6 @@ class AntCMS
{
$start_time = microtime(true);
$content = $this->getPage($page);
$currentConfig = AntConfig::currentConfig();
$antTwig = new AntTwig;
if (!$content || !is_array($content)) {
@ -36,7 +35,7 @@ class AntCMS
$end_time = microtime(true);
$elapsed_time = round($end_time - $start_time, 4);
if ($currentConfig['debug']) {
if (AntConfig::currentConfig('debug')) {
$pageTemplate = str_replace('<!--AntCMS-Debug-->', '<p>Took ' . $elapsed_time . ' seconds to render the page. </p>', $pageTemplate);
}
@ -46,13 +45,12 @@ class AntCMS
public function getPageLayout($theme = null)
{
$siteInfo = AntCMS::getSiteInfo();
$currentConfig = AntConfig::currentConfig();
$pageTemplate = $this->getThemeTemplate('default_layout', $theme);
$pageTemplate = str_replace('<!--AntCMS-Navigation-->', AntPages::generateNavigation($this->getThemeTemplate('nav_layout', $theme)), $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteTitle-->', $siteInfo['siteTitle'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteLink-->', '//' . $currentConfig['baseURL'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteLink-->', '//' . AntConfig::currentConfig('baseURL'), $pageTemplate);
return $pageTemplate;
}
@ -98,8 +96,7 @@ class AntCMS
public function getThemeTemplate($layout = 'default_layout', $theme = null)
{
$currentConfig = AntConfig::currentConfig();
$theme = $theme ?? $currentConfig['activeTheme'];
$theme = $theme ?? AntConfig::currentConfig('activeTheme');
if (!is_dir(antThemePath . '/' . $theme)) {
$theme = 'Default';
@ -183,8 +180,7 @@ class AntCMS
public static function getSiteInfo()
{
$currentConfig = AntConfig::currentConfig();
return $currentConfig['SiteInfo'];
return AntConfig::currentConfig('SiteInfo');
}
public function serveContent($path)

View file

@ -6,6 +6,10 @@ use AntCMS\AntYaml;
class AntConfig
{
/**
* Generates the default config file and saves it.
* @return void
*/
public static function generateConfig()
{
$defaultOptions = array(
@ -27,11 +31,42 @@ class AntConfig
AntYaml::saveFile(antConfigFile, $defaultOptions);
}
public static function currentConfig()
/**
* Retrieves the current configuration from the AntCMS config file.
*
* @param string|null $key The key of the configuration item to retrieve. Use dot notation to specify nested keys.
* @return mixed The configuration array or a specific value if the key is specified.
*/
public static function currentConfig($key = null)
{
return AntYaml::parseFile(antConfigFile);
$config = AntYaml::parseFile(antConfigFile);
if (is_null($key)) {
return $config;
} else {
$keys = explode('.', $key);
return self::getArrayValue($config, $keys);
}
}
private static function getArrayValue($array, $keys)
{
foreach ($keys as $k) {
if (isset($array[$k])) {
$array = $array[$k];
} else {
return null;
}
}
return $array;
}
/**
* Saves the AntCMS configuration
*
* @param array $config The config data to be saved.
* @return void
*/
public static function saveConfig($config)
{
AntYaml::saveFile(antConfigFile, $config);

View file

@ -11,9 +11,8 @@ class AntKeywords
{
$cache = new AntCache();
$cacheKey = $cache->createCacheKey($content, 'keywords');
$currentConfig = AntConfig::currentConfig();
if (!$currentConfig['generateKeywords']) {
if (!AntConfig::currentConfig('generateKeywords')) {
return '';
}

View file

@ -39,11 +39,10 @@ class AntPages
public static function generateNavigation($navTemplate = '')
{
$currentConfig = AntConfig::currentConfig();
$pages = AntPages::getPages();
$cache = new AntCache;
$theme = $currentConfig['activeTheme'];
$theme = AntConfig::currentConfig('activeTheme');
$cacheKey = $cache->createCacheKey(json_encode($pages), $theme);
if ($cache->isCached($cacheKey)) {
@ -54,7 +53,7 @@ class AntPages
}
}
$baseURL = $currentConfig['baseURL'];
$baseURL = AntConfig::currentConfig('baseURL');
foreach ($pages as $key => $page) {
$url = "//" . AntTools::repairURL($baseURL . $page['functionalPagePath']);
$pages[$key]['url'] = $url;

View file

@ -8,9 +8,8 @@ class AntTwig
{
public function renderWithTiwg(string $content = '', array $params = array(), string $theme = null)
{
$currentConfig = AntConfig::currentConfig();
$twigCache = $currentConfig['enableCache'] ? AntCachePath : false;
$theme = $theme ?? $currentConfig['activeTheme'];
$twigCache = AntConfig::currentConfig('enableCache') ? AntCachePath : false;
$theme = $theme ?? AntConfig::currentConfig('activeTheme');
if (!is_dir(antThemePath . '/' . $theme)) {
$theme = 'Default';
@ -23,7 +22,7 @@ class AntTwig
$loader = new \Twig\Loader\ChainLoader([$loaderString, $loaderFilesystem]);
$twig = new \Twig\Environment($loader, [
'cache' => $twigCache,
'debug' => $currentConfig['debug'],
'debug' => AntConfig::currentConfig('debug'),
]);
return $twig->render($content, $params);

View file

@ -18,7 +18,6 @@ class AdminPlugin extends AntPlugin
$currentStep = $route[0] ?? 'none';
$antCMS = new AntCMS;
$pageTemplate = $antCMS->getPageLayout();
$currentConfig = AntConfig::currentConfig();
array_shift($route);
switch ($currentStep) {
@ -40,8 +39,8 @@ class AdminPlugin extends AntPlugin
);
$HTMLTemplate = "<h1>AntCMS Admin Plugin</h1>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/config/'>AntCMS Configuration</a><br>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/pages/'>Page management</a><br>\n";
$HTMLTemplate .= "<a href='//" . AntConfig::currentConfig('baseURL') . "plugin/admin/config/'>AntCMS Configuration</a><br>\n";
$HTMLTemplate .= "<a href='//" . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/'>Page management</a><br>\n";
$pageTemplate = str_replace('<!--AntCMS-Body-->', $HTMLTemplate, $pageTemplate);
$pageTemplate = $antTwig->renderWithTiwg($pageTemplate, $params);
@ -120,7 +119,6 @@ class AdminPlugin extends AntPlugin
$pageTemplate = $antCMS->getPageLayout();
$HTMLTemplate = $antCMS->getThemeTemplate('markdown_edit_layout');
$pages = AntPages::getPages();
$currentConfig = AntConfig::currentConfig();
$antTwig = new AntTwig;
$params = array(
'AntCMSTitle' => 'AntCMS Page Management',
@ -132,7 +130,7 @@ class AdminPlugin extends AntPlugin
switch ($route[0] ?? 'none') {
case 'regenerate':
AntPages::generatePages();
header('Location: //' . $currentConfig['baseURL'] . "plugin/admin/pages/");
header('Location: //' . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/");
exit;
case 'edit':
@ -150,7 +148,7 @@ class AdminPlugin extends AntPlugin
$pagePath = AntTools::repairFilePath($pagePath);
$HTMLTemplate = str_replace('<!--AntCMS-ActionURL-->', '//' . $currentConfig['baseURL'] . "plugin/admin/pages/save/$pagePath", $HTMLTemplate);
$HTMLTemplate = str_replace('<!--AntCMS-ActionURL-->', '//' . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/save/$pagePath", $HTMLTemplate);
$HTMLTemplate = str_replace('<!--AntCMS-TextAreaContent-->', htmlspecialchars($page), $HTMLTemplate);
break;
@ -158,32 +156,32 @@ class AdminPlugin extends AntPlugin
array_shift($route);
$pagePath = antContentPath . '/' . implode('/', $route);
if (!isset($_POST['textarea'])) {
header('Location: //' . $currentConfig['baseURL'] . "plugin/admin/pages/");
header('Location: //' . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/");
}
file_put_contents($pagePath, $_POST['textarea']);
header('Location: //' . $currentConfig['baseURL'] . "plugin/admin/pages/");
header('Location: //' . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/");
exit;
case 'create':
$HTMLTemplate = "<h1>Page Management</h1>\n";
$HTMLTemplate .= "<p>Create new page</p>\n";
$HTMLTemplate .= '<form method="post" action="' . '//' . $currentConfig['baseURL'] . 'plugin/admin/pages/edit">';
$HTMLTemplate .= '<form method="post" action="' . '//' . AntConfig::currentConfig('baseURL') . 'plugin/admin/pages/edit">';
$HTMLTemplate .=
'<div style="display:flex; flex-direction: row; justify-content: center; align-items: center">
<label for="input">URL for new page: ' . $currentConfig['baseURL'] . ' </label> <input type="text" name="newpage" id="input">
<label for="input">URL for new page: ' . AntConfig::currentConfig('baseURL') . ' </label> <input type="text" name="newpage" id="input">
<input type="submit" value="Submit">
</div></form>';
break;
default:
$HTMLTemplate = "<h1>Page Management</h1>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/pages/regenerate'>Click here to regenerate the page list</a><br>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/pages/create'>Click here to create a new page</a><br>\n";
$HTMLTemplate .= "<a href='//" . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/regenerate'>Click here to regenerate the page list</a><br>\n";
$HTMLTemplate .= "<a href='//" . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/create'>Click here to create a new page</a><br>\n";
$HTMLTemplate .= "<ul>\n";
foreach ($pages as $page) {
$HTMLTemplate .= "<li>\n";
$HTMLTemplate .= "<h2>" . $page['pageTitle'] . "</h2>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/pages/edit" . $page['functionalPagePath'] . "'>Edit this page</a><br>\n";
$HTMLTemplate .= "<a href='//" . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/edit" . $page['functionalPagePath'] . "'>Edit this page</a><br>\n";
$HTMLTemplate .= "<ul>\n";
$HTMLTemplate .= "<li>Full page path: " . $page['fullPagePath'] . "</li>\n";
$HTMLTemplate .= "<li>Functional page path: " . $page['functionalPagePath'] . "</li>\n";

View file

@ -22,9 +22,7 @@ if (!file_exists(antPagesList)) {
AntPages::generatePages();
}
$currentConfg = AntConfig::currentConfig();
if ($currentConfg['forceHTTPS'] && 'cli' !== PHP_SAPI) {
if (AntConfig::currentConfig('forceHTTPS') && 'cli' !== PHP_SAPI) {
$isHTTPS = false;
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {