| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\System\Module; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class Settings extends \Aurora\System\AbstractSettings |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | protected $aTenantSettings = []; |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | public $ModuleName; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | public function __construct($sModuleName) |
| 33: | { |
| 34: | $this->ModuleName = $sModuleName; |
| 35: | |
| 36: | parent::__construct( |
| 37: | \Aurora\System\Api::GetModuleManager()->GetModulesSettingsPath() . $sModuleName . '.config.json' |
| 38: | ); |
| 39: | } |
| 40: | |
| 41: | public function GetDefaultConfigFilePath() |
| 42: | { |
| 43: | return \Aurora\System\Api::GetModuleManager()->GetModulesRootPath() . $this->ModuleName . '/config.json'; |
| 44: | } |
| 45: | |
| 46: | public function InitDefaultConfiguration() |
| 47: | { |
| 48: | if (\file_exists($this->GetDefaultConfigFilePath())) { |
| 49: | $sModulesSettingsPath = \Aurora\System\Api::GetModuleManager()->GetModulesSettingsPath(); |
| 50: | if (!\file_exists($sModulesSettingsPath)) { |
| 51: | \set_error_handler(function () { |
| 52: | }); |
| 53: | \mkdir($sModulesSettingsPath, 0777); |
| 54: | \restore_error_handler(); |
| 55: | if (!\file_exists($sModulesSettingsPath)) { |
| 56: | return; |
| 57: | } |
| 58: | } |
| 59: | \copy($this->GetDefaultConfigFilePath(), $this->sPath); |
| 60: | } |
| 61: | } |
| 62: | |
| 63: | public function Load($bForceLoad = false) |
| 64: | { |
| 65: | $bResult = false; |
| 66: | if ($this->bIsLoaded && !$bForceLoad) { |
| 67: | $bResult = true; |
| 68: | } else { |
| 69: | if (!\file_exists($this->sPath)) { |
| 70: | $mData = $this->LoadDataFromFile($this->GetDefaultConfigFilePath()); |
| 71: | } else { |
| 72: | $mData = $this->LoadDataFromFile($this->sPath); |
| 73: | } |
| 74: | |
| 75: | if (!$mData) { |
| 76: | $mData = $this->LoadDataFromFile($this->sPath.'.bak'); |
| 77: | if ($mData) { |
| 78: | \copy($this->sPath.'.bak', $this->sPath); |
| 79: | } else { |
| 80: | $this->Save(); |
| 81: | } |
| 82: | } |
| 83: | |
| 84: | if ($mData !== false) { |
| 85: | $this->aContainer = $this->ParseData($mData); |
| 86: | $this->bIsLoaded = true; |
| 87: | $bResult = true; |
| 88: | } |
| 89: | } |
| 90: | |
| 91: | return $bResult; |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public function GetTenantSetttings($sTenantName) |
| 100: | { |
| 101: | if (!isset($this->aTenantSettings[$sTenantName]) && $this->IsTenantSettingsExists($sTenantName)) { |
| 102: | $this->aTenantSettings[$sTenantName] = new TenantSettings( |
| 103: | $this->ModuleName, |
| 104: | $sTenantName |
| 105: | ); |
| 106: | } |
| 107: | |
| 108: | return isset($this->aTenantSettings[$sTenantName]) ? $this->aTenantSettings[$sTenantName] : null; |
| 109: | } |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | public function GetValue($sName, $sDefaultValue = null) |
| 118: | { |
| 119: | return $this->GetTenantValue(\Aurora\System\Api::getTenantName(), $sName, $sDefaultValue); |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function GetTenantValue($sTenantName, $sName, $sDefaultValue = null) |
| 129: | { |
| 130: | $mResult = $sDefaultValue; |
| 131: | |
| 132: | $oTenantSettings = $this->GetTenantSetttings( |
| 133: | $sTenantName |
| 134: | ); |
| 135: | |
| 136: | if (isset($oTenantSettings) && isset($oTenantSettings->{$sName})) { |
| 137: | $mResult = $oTenantSettings->GetValue($sName, $sDefaultValue); |
| 138: | } else { |
| 139: | $mResult = parent::GetValue($sName, $sDefaultValue); |
| 140: | } |
| 141: | |
| 142: | |
| 143: | return $mResult; |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | public function SetTenantValue($sTenantName, $sName, $sValue = null) |
| 152: | { |
| 153: | if (isset($this->{$sName})) { |
| 154: | $oTenantSettings = null; |
| 155: | if (!isset($this->aTenantSettings[$sTenantName])) { |
| 156: | $oTenantSettings = new TenantSettings( |
| 157: | $this->ModuleName, |
| 158: | $sTenantName |
| 159: | ); |
| 160: | } else { |
| 161: | $oTenantSettings = $this->aTenantSettings[$sTenantName]; |
| 162: | } |
| 163: | |
| 164: | if (isset($oTenantSettings)) { |
| 165: | if (!isset($oTenantSettings->{$sName}) && isset($this->aContainer[$sName])) { |
| 166: | $oTenantSettings->SetProperty($this->aContainer[$sName]); |
| 167: | } |
| 168: | $oTenantSettings->SetValue($sName, $sValue); |
| 169: | $this->aTenantSettings[$sTenantName] = $oTenantSettings; |
| 170: | } else { |
| 171: | throw new \Aurora\System\Exceptions\SettingsException(); |
| 172: | } |
| 173: | } else { |
| 174: | throw new \Aurora\System\Exceptions\SettingsException(); |
| 175: | } |
| 176: | } |
| 177: | |
| 178: | public function SaveTenantSettings($sTenantName) |
| 179: | { |
| 180: | $mResult = false; |
| 181: | $oTenantSettings = $this->GetTenantSetttings($sTenantName); |
| 182: | if (isset($oTenantSettings)) { |
| 183: | $mResult = $oTenantSettings->Save(); |
| 184: | } |
| 185: | |
| 186: | return $mResult; |
| 187: | } |
| 188: | |
| 189: | public function GetDefaultConfigValues() |
| 190: | { |
| 191: | $oDefaultSettings = new DefaultSettings($this->GetDefaultConfigFilePath()); |
| 192: | $oDefaultSettings->Load(); |
| 193: | return $oDefaultSettings->GetValues(); |
| 194: | } |
| 195: | |
| 196: | public function IsTenantSettingsExists($sTenantName) |
| 197: | { |
| 198: | return \file_exists(\Aurora\System\Api::GetModuleManager()->GetModulesSettingsPath() . 'tenants/' . $sTenantName . '/' . $this->ModuleName . '.config.json'); |
| 199: | } |
| 200: | } |
| 201: | |