Cfg/Fixup

* only throw errors if there is actually a config to work with. (like, not when you are just setting up for the first time)
 * do not use trigger_error() in CLI mode as it can cause a lockup as the error_handler tries to throw more errors.
 * assign lost validator fn flag to settings
 * also use validator onReset
This commit is contained in:
Sarjuuk
2024-06-04 16:58:03 +02:00
parent 5c1e9747c6
commit e734b41632
4 changed files with 80 additions and 14 deletions

View File

@@ -43,6 +43,7 @@ class Cfg
private const IDX_COMMENT = 4; private const IDX_COMMENT = 4;
private static $store = []; // name => [value, flags, cat, default, comment] private static $store = []; // name => [value, flags, cat, default, comment]
private static $isLoaded = false;
private static $rebuildScripts = array( private static $rebuildScripts = array(
// 'rep_req_border_unco' => ['global'], // currently not a template or buildScript // 'rep_req_border_unco' => ['global'], // currently not a template or buildScript
@@ -70,20 +71,20 @@ class Cfg
if ($err = self::validate($value, $flags, $comment)) if ($err = self::validate($value, $flags, $comment))
{ {
trigger_error('Aowow config '.strtoupper($key).' failed validation and was skipped: '.$err, E_USER_ERROR); self::throwError('Aowow config '.strtoupper($key).' failed validation and was skipped: '.$err);
continue; continue;
} }
if ($flags & self::FLAG_INTERNAL) if ($flags & self::FLAG_INTERNAL)
{ {
trigger_error('Aowow config '.strtoupper($key).' is flagged as internaly generated and should not have been set in DB.', E_USER_ERROR); self::throwError('Aowow config '.strtoupper($key).' is flagged as internaly generated and should not have been set in DB.');
continue; continue;
} }
if ($flags & self::FLAG_ON_LOAD_FN) if ($flags & self::FLAG_ON_LOAD_FN)
{ {
if (!method_exists('Cfg', $key)) if (!method_exists('Cfg', $key))
trigger_error('Aowow config '.strtoupper($key).' flagged for onLoadFN handling, but no handler was set', E_USER_WARNING); self::throwError('Aowow config '.strtoupper($key).' flagged for onLoadFN handling, but no handler was set');
else else
self::{$key}($value); self::{$key}($value);
} }
@@ -93,10 +94,21 @@ class Cfg
self::$store[strtolower($key)] = [$value, $flags, $catg, $default, $comment]; self::$store[strtolower($key)] = [$value, $flags, $catg, $default, $comment];
} }
if (CLI && !count(self::$store))
{
CLI::write('Cfg::load - aowow_config unexpectedly empty.', CLI::LOG_WARN);
return;
}
self::$isLoaded = true;
} }
public static function add(string $key, /*int|string*/ $value) : string public static function add(string $key, /*int|string*/ $value) : string
{ {
if (!self::$isLoaded)
return 'used add() on uninitialized config';
if (!$key) if (!$key)
return 'empty option name given'; return 'empty option name given';
@@ -124,6 +136,9 @@ class Cfg
public static function delete(string $key) : string public static function delete(string $key) : string
{ {
if (!self::$isLoaded)
return 'used delete() on uninitialized config';
$key = strtolower($key); $key = strtolower($key);
if (!isset(self::$store[$key])) if (!isset(self::$store[$key]))
@@ -151,7 +166,9 @@ class Cfg
if (!isset(self::$store[$key])) if (!isset(self::$store[$key]))
{ {
trigger_error('cfg not defined: '.$key, E_USER_ERROR); if (self::$isLoaded)
self::throwError('cfg not defined: '.strtoupper($key));
return ''; return '';
} }
@@ -167,6 +184,9 @@ class Cfg
public static function set(string $key, /*int|string*/ $value, ?array &$rebuildFiles = []) : string public static function set(string $key, /*int|string*/ $value, ?array &$rebuildFiles = []) : string
{ {
if (!self::$isLoaded)
return 'used set() on uninitialized config';
$key = strtolower($key); $key = strtolower($key);
if (!isset(self::$store[$key])) if (!isset(self::$store[$key]))
@@ -202,7 +222,6 @@ class Cfg
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $oldValue, $key); DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $oldValue, $key);
self::$store[$key][self::IDX_VALUE] = $oldValue; self::$store[$key][self::IDX_VALUE] = $oldValue;
// trigger_error($errMsg) ?
return $errMsg; return $errMsg;
} }
} }
@@ -221,12 +240,15 @@ class Cfg
public static function reset(string $key, ?array &$rebuildFiles = []) : string public static function reset(string $key, ?array &$rebuildFiles = []) : string
{ {
if (!self::$isLoaded)
return 'used reset() on uninitialized config';
$key = strtolower($key); $key = strtolower($key);
if (!isset(self::$store[$key])) if (!isset(self::$store[$key]))
return 'configuration option not found'; return 'configuration option not found';
[, $flags, , $default, ] = self::$store[$key]; [$oldValue, $flags, , $default, ] = self::$store[$key];
if ($flags & self::FLAG_INTERNAL) if ($flags & self::FLAG_INTERNAL)
return 'can\'t set internal options directly'; return 'can\'t set internal options directly';
@@ -240,6 +262,25 @@ class Cfg
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $default, $key); DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $default, $key);
self::$store[$key][self::IDX_VALUE] = $default; self::$store[$key][self::IDX_VALUE] = $default;
// validate change
if ($flags & self::FLAG_ON_SET_FN)
{
$errMsg = '';
if (!method_exists('Cfg', $key))
$errMsg = 'required onSetFN validator not set';
else
self::{$key}($default, $errMsg);
if ($errMsg)
{
// rollback change
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $oldValue, $key);
self::$store[$key][self::IDX_VALUE] = $oldValue;
return $errMsg;
}
}
// trigger setup build // trigger setup build
return self::handleFileBuild($key, $rebuildFiles); return self::handleFileBuild($key, $rebuildFiles);
} }
@@ -333,7 +374,32 @@ class Cfg
return $msg; return $msg;
} }
private static function acc_auth_mode(/*int|string*/ $value, ?string $msg = '') : bool private static function throwError($msg) : void
{
if (CLI)
CLI::write($msg, CLI::LOG_ERROR);
else
trigger_error($msg, E_USER_ERROR);
}
private static function locales(/*int|string*/ $value, ?string &$msg = '') : bool
{
if (!CLI)
return true;
CLISetup::$localeIds = [];
foreach (CLISetup::$locales as $idx => $_)
if (!($value) || ($value & (1 << $idx)))
CLISetup::$localeIds[] = $idx;
if (!empty(CLISetup::$localeIds))
return true;
$msg .= 'no valid locales set';
return false;
}
private static function acc_auth_mode(/*int|string*/ $value, ?string &$msg = '') : bool
{ {
if ($value == 1 && !extension_loaded('gmp')) if ($value == 1 && !extension_loaded('gmp'))
{ {
@@ -344,7 +410,7 @@ class Cfg
return true; return true;
} }
private static function profiler_enable(/*int|string*/ $value, ?string $msg = '') : bool private static function profiler_enable(/*int|string*/ $value, ?string &$msg = '') : bool
{ {
if ($value != 1) if ($value != 1)
return true; return true;
@@ -352,7 +418,7 @@ class Cfg
return Profiler::queueStart($msg); return Profiler::queueStart($msg);
} }
private static function static_host(/*int|string*/ $value, ?string $msg = '') : bool private static function static_host(/*int|string*/ $value, ?string &$msg = '') : bool
{ {
self::$store['static_url'] = array( // points js to images & scripts self::$store['static_url'] = array( // points js to images & scripts
(self::useSSL() ? 'https://' : 'http://').$value, (self::useSSL() ? 'https://' : 'http://').$value,
@@ -365,7 +431,7 @@ class Cfg
return true; return true;
} }
private static function site_host(/*int|string*/ $value, ?string $msg = '') : bool private static function site_host(/*int|string*/ $value, ?string &$msg = '') : bool
{ {
self::$store['host_url'] = array( // points js to executable files self::$store['host_url'] = array( // points js to executable files
(self::useSSL() ? 'https://' : 'http://').$value, (self::useSSL() ? 'https://' : 'http://').$value,

View File

@@ -171,8 +171,7 @@ abstract class CLI
continue; continue;
} }
if (!$nCols) $nCols = max($nCols, count($row));
$nCols = count($row);
for ($j = 0; $j < $nCols - 1; $j++) // don't pad last column for ($j = 0; $j < $nCols - 1; $j++) // don't pad last column
$pads[$j] = max($pads[$j] ?? 0, mb_strlen($row[$j] ?? '')); $pads[$j] = max($pads[$j] ?? 0, mb_strlen($row[$j] ?? ''));

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
UPDATE aowow_config SET `flags` = `flags`| 0x400 WHERE `key` IN ('locales', 'acc_auth_mode', 'profiler_enable');