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: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229:
<?php
namespace MvcCore\Config;
trait Environment {
protected static $systemEnvironmentsSectionName = 'environments';
protected static $commonEnvironmentDataKey = '';
public static function & GetEnvironmentDetectionData (\MvcCore\IConfig $config) {
$envConfData = [];
if (!$config->system)
return $envConfData;
$commonEnvDataKey = static::$commonEnvironmentDataKey;
$someEnvironmentData = [];
if (count($config->mergedData) > 0) {
$firstEnvironmentName = key($config->mergedData);
$someEnvironmentData = & $config->mergedData[$firstEnvironmentName];
} else if (isset($config->envData[$commonEnvDataKey])) {
$someEnvironmentData = & $config->envData[$commonEnvDataKey];
}
if ($someEnvironmentData) {
$sysEnvSectionName = static::$systemEnvironmentsSectionName;
if (is_object($someEnvironmentData)) {
if (isset($someEnvironmentData->{$sysEnvSectionName}))
$envConfData = & $someEnvironmentData->{$sysEnvSectionName};
} else {
if (isset($someEnvironmentData[$sysEnvSectionName]))
$envConfData = & $someEnvironmentData[$sysEnvSectionName];
}
}
return $envConfData;
}
public static function SetUpEnvironmentData (\MvcCore\IConfig $config, $environmentName) {
if (array_key_exists($environmentName, $config->mergedData)) {
$config->currentData = $config->mergedData[$environmentName];
} else {
static::mergeEnvironmentData(
$config, $config->currentData, $environmentName
);
$config->mergedData[$environmentName] = & $config->currentData;
}
$config->envData = [];
}
public function & GetData ($environmentName = NULL) {
$result = [];
if ($environmentName === NULL) {
if ($this->currentData) {
$result = & $this->currentData;
} else {
$app = self::$app ?: (self::$app = \MvcCore\Application::GetInstance());
$currentEnvName = $app->GetEnvironment()->GetName();
if ($currentEnvName && array_key_exists($currentEnvName, $this->mergedData))
$result = & $this->mergedData[$currentEnvName];
}
} else if (array_key_exists($environmentName, $this->mergedData)) {
$result = & $this->mergedData[$environmentName];
} else {
static::mergeEnvironmentData(
$this, $this->mergedData[$environmentName], $environmentName
);
$result = & $this->mergedData[$environmentName];
}
return $result;
}
public function SetData (array $data = [], $environmentName = NULL) {
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$currentEnvName = $app->GetEnvironment()->GetName();
if ($environmentName === NULL) {
$this->currentData = & $data;
$this->mergedData[$currentEnvName] = & $data;
} else {
if ($environmentName === $currentEnvName)
$this->currentData = & $data;
$this->mergedData[$environmentName] = & $data;
}
return $this;
}
protected static function mergeEnvironmentData (\MvcCore\IConfig $config, & $resultCollection, $environmentName) {
$commonEnvDataKey = static::$commonEnvironmentDataKey;
$envCommonData = [];
$envSpecificData = [];
if (isset($config->envData[$commonEnvDataKey]))
$envCommonData = & $config->envData[$commonEnvDataKey];
if (isset($config->envData[$environmentName]))
$envSpecificData = & $config->envData[$environmentName];
$envCommonDataEmpty = count((array) $envCommonData) === 0;
$envSpecificDataEmpty = count((array) $envSpecificData) === 0;
if ($envCommonDataEmpty) {
$resultCollection = $envSpecificData;
} else if ($envSpecificDataEmpty) {
$resultCollection = $envCommonData;
} else {
$commonDataType = gettype($envCommonData);
$specificDataType = gettype($envSpecificData);
if ($commonDataType != $specificDataType)
settype($envSpecificData, $commonDataType);
$resultCollection = static::mergeRecursive(
$envCommonData, $envSpecificData
);
}
}
protected static function mergeRecursive ($commonEnvData, $specificEnvData) {
$commonEnvDataClone = unserialize(serialize($commonEnvData));
static::_mergeArraysOrObjectsRecursive($commonEnvDataClone, $specificEnvData);
return $commonEnvDataClone;
}
private static function _mergeArraysOrObjectsRecursive (& $commonEnvData, & $specificEnvData) {
if (is_object($specificEnvData)) {
$specificEnvKeys = array_keys(get_object_vars($specificEnvData));
foreach ($specificEnvKeys as $key) {
$commonEnvValue = & $commonEnvData->{$key};
$specificEnvValue = & $specificEnvData->{$key};
if (!is_scalar($specificEnvValue) && $specificEnvValue !== NULL) {
if (!isset($commonEnvData->{$key})) {
$commonEnvData->{$key} = $specificEnvValue;
} else {
static::_mergeArraysOrObjectsRecursive(
$commonEnvValue, $specificEnvValue
);
}
} else {
$commonEnvValue = $specificEnvValue;
}
}
} else if (is_array($specificEnvData)) {
$specificEnvKeys = array_keys($specificEnvData);
foreach ($specificEnvKeys as $key) {
$commonEnvValue = & $commonEnvData[$key];
$specificEnvValue = & $specificEnvData[$key];
if (!is_scalar($specificEnvValue) && $specificEnvValue !== NULL) {
if ($commonEnvValue === NULL) {
$commonEnvData[$key] = $specificEnvValue;
} else {
static::_mergeArraysOrObjectsRecursive(
$commonEnvValue, $specificEnvValue
);
}
} else {
$commonEnvValue = $specificEnvValue;
}
}
}
}
}