1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172:
<?php
namespace MvcCore\Model;
trait Config {
public static function GetSysConfigProperties () {
return (object) static::$sysConfigProperties;
}
public static function & GetConfigs () {
if (self::$configs === NULL) static::loadConfigs(TRUE);
return self::$configs;
}
public static function SetConfigs (array $configs = [], $defaultConnectionName = NULL) {
self::$configs = [];
foreach ($configs as $key => $value) self::$configs[$key] = (object) $value;
self::$configs = & $configs;
if ($defaultConnectionName !== NULL)
self::$defaultConnectionName = $defaultConnectionName;
return TRUE;
}
public static function & GetConfig ($connectionName = NULL) {
if (self::$configs === NULL) static::loadConfigs(TRUE);
if ($connectionName === NULL && isset(static::$connectionName)) $connectionName = static::$connectionName;
if ($connectionName === NULL && isset(self::$connectionName)) $connectionName = self::$connectionName;
if ($connectionName === NULL) $connectionName = self::$defaultConnectionName;
if ($connectionName === NULL) {
$result = NULL;
return $result;
}
return self::$configs[$connectionName];
}
public static function SetConfig (array $config = [], $connectionName = NULL) {
if (self::$configs === NULL) static::loadConfigs(FALSE);
$sysCfgProps = (object) static::$sysConfigProperties;
if ($connectionName === NULL) {
if (isset($config[$sysCfgProps->name])) {
$connectionName = $config[$sysCfgProps->name];
} else if (isset($config[$sysCfgProps->index])) {
$connectionName = $config[$sysCfgProps->index];
}
}
if ($connectionName === NULL) {
$configNumericKeys = array_filter(array_keys(self::$configs), 'is_numeric');
if ($configNumericKeys) {
sort($configNumericKeys);
$connectionName = $configNumericKeys[count($configNumericKeys) - 1] + 1;
} else {
$connectionName = 0;
}
}
self::$configs[$connectionName] = (object) $config;
return $connectionName;
}
protected static function loadConfigs ($throwExceptionIfNoSysConfig = TRUE, $strict = TRUE) {
$configClass = \MvcCore\Application::GetInstance()->GetConfigClass();
$systemCfg = $configClass::GetSystem();
if ($systemCfg === NULL) {
if ($throwExceptionIfNoSysConfig)
throw new \Exception(
"[".get_class()."] System config not found in `"
. $configClass::GetSystemConfigPath() . "`."
);
return;
}
$sysCfgProps = (object) static::$sysConfigProperties;
$dbSectionName = $sysCfgProps->sectionName;
if (!isset($systemCfg->{$dbSectionName}) && $throwExceptionIfNoSysConfig)
throw new \Exception(
"[".get_class()."] No [" . $dbSectionName . "] section and no records matched "
."`" . $dbSectionName . ".*` found in system config in: `" . $configClass::GetSystemConfigPath() . "`."
);
$systemCfgDb = (object) $systemCfg->{$dbSectionName};
$configs = [];
$defaultConnectionName = NULL;
$defaultConnectionClass = NULL;
$configsConnectionsNames = [];
if (isset($systemCfgDb->{$sysCfgProps->defaultName}))
$defaultConnectionName = $systemCfgDb->{$sysCfgProps->defaultName};
if (isset($systemCfgDb->{$sysCfgProps->defaultClass}))
$defaultConnectionClass = $systemCfgDb->{$sysCfgProps->defaultClass};
if (isset($systemCfgDb->driver)) {
$configs[0] = $systemCfgDb;
$configsConnectionsNames[] = '0';
} else {
foreach ($systemCfgDb as $key => $value) {
if (is_scalar($value)) {
$configs[$key] = $value;
} else {
$configs[$key] = (object) $value;
$configsConnectionsNames[] = (string) $key;
}
}
}
$configsConnectionsNamesCount = count($configsConnectionsNames);
if ($defaultConnectionName === NULL) {
if (
($configsConnectionsNamesCount === 1) ||
(!$strict && $configsConnectionsNamesCount > 0)
) {
$defaultConnectionName = $configsConnectionsNames[0];
}
}
if ($defaultConnectionName !== NULL && !isset($configs[$defaultConnectionName]))
throw new \Exception(
"[".get_class()."] No default connection name '{$defaultConnectionName}'"
." found in 'db.*' section in system config.ini."
);
if ($defaultConnectionName !== NULL)
self::$defaultConnectionName = $defaultConnectionName;
if ($defaultConnectionClass !== NULL)
self::$defaultConnectionClass = $defaultConnectionClass;
self::$configs = & $configs;
}
}