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:
<?php
namespace MvcCore\Config;
trait ReadWrite {
public static function CreateInstance (array $mergedData = [], $configFullPath = NULL) {
$config = new static();
if ($mergedData)
$config->mergedData = & $mergedData;
if ($configFullPath)
$config->fullPath = $configFullPath;
return $config;
}
public static function GetSystem () {
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$configClass = $app->GetConfigClass();
$toolClass = $app->GetToolClass();
$appRootRelativePath = $configClass::GetSystemConfigPath();
$appRoot = self::$appRoot ?: self::$appRoot = $app->GetRequest()->GetAppRoot();
$configFullPath = $toolClass::RealPathVirtual($appRoot . '/' . str_replace(
'%appPath%', $app->GetAppDir(), ltrim($appRootRelativePath, '/')
));
if (!array_key_exists($configFullPath, self::$configsCache)) {
$config = $configClass::LoadConfig($configFullPath, $configClass, TRUE);
if ($config) {
$environment = $app->GetEnvironment();
$doNotThrownError = func_num_args() > 0 ? func_get_arg(0) : FALSE;
if ($environment->IsDetected()) {
$configClass::SetUpEnvironmentData($config, $environment->GetName());
} else if (!$doNotThrownError) {
throw new \RuntimeException(
"The configuration cannot be loaded until the environment is detected. ".
"Please detect the environment first before loading configuration by: ".
"`\MvcCore\Application::GetInstance()->GetEnvironment()->GetName()`."
);
}
}
self::$configsCache[$configFullPath] = $config;
}
return self::$configsCache[$configFullPath];
}
public static function GetConfig ($appRootRelativePath) {
$appRootRelativePath = ltrim($appRootRelativePath, '/');
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$appRoot = self::$appRoot ?: self::$appRoot = $app->GetRequest()->GetAppRoot();
$toolClass = $app->GetToolClass();
$configFullPath = $toolClass::RealPathVirtual($appRoot . '/' . str_replace(
'%appPath%', $app->GetAppDir(), $appRootRelativePath
));
if (!array_key_exists($configFullPath, self::$configsCache)) {
$systemConfigClass = $app->GetConfigClass();
$isSystem = $systemConfigClass::GetSystemConfigPath() === '/' . $appRootRelativePath;
$config = $systemConfigClass::LoadConfig($configFullPath, $systemConfigClass, $isSystem);
if ($config) {
$environment = $app->GetEnvironment();
$doNotThrownError = func_num_args() > 1 ? func_get_arg(1) : FALSE;
if ($environment->IsDetected()) {
$systemConfigClass::SetUpEnvironmentData($config, $environment->GetName());
} else if (!$doNotThrownError) {
throw new \RuntimeException(
"The configuration cannot be loaded until the environment is detected. ".
"Please detect the environment first before loading configuration by: ".
"`\MvcCore\Application::GetInstance()->GetEnvironment()->GetName()`."
);
}
}
self::$configsCache[$configFullPath] = $config;
}
return self::$configsCache[$configFullPath];
}
public function Save () {
$rawContent = $this->Dump();
if ($rawContent === NULL)
throw new \Exception('Configuration data was not possible to dump.');
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$toolClass = $app->GetToolClass();
try {
$toolClass::AtomicWrite(
$this->fullPath,
$rawContent,
'w',
10,
5000,
30000
);
} catch (\Exception $e) {
throw $e;
} catch (\Throwable $e) {
throw $e;
}
return TRUE;
}
public static function LoadConfig ($configFullPath, $systemConfigClass, $isSystemConfig = FALSE) {
$config = $systemConfigClass::CreateInstance([], $configFullPath);
if (!file_exists($configFullPath)) {
$config = NULL;
} else {
$config->system = $isSystemConfig;
if ($config->Read()) {
$config->mergedData = [];
$config->currentData = [];
} else {
$config = NULL;
}
}
return $config;
}
}