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: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360:
<?php
namespace MvcCore;
require_once(__DIR__.'/../MvcCore.php');
class Config
{
const ENVIRONMENT_DEVELOPMENT = 'development';
const ENVIRONMENT_BETA = 'beta';
const ENVIRONMENT_ALPHA = 'alpha';
const ENVIRONMENT_PRODUCTION = 'production';
public static $SystemConfigPath = '/%appPath%/config.ini';
protected static $environment = '';
protected static $systemConfig = NULL;
protected static $booleanValues = array('yes' => TRUE, 'no' => FALSE, 'true' => TRUE, 'false' => FALSE,);
protected $result = array();
protected $objectTypes = array();
public static function StaticInit () {
if (!static::$environment) {
$serverAddress = static::getServerIp();
$remoteAddress = static::getClientIp();
if ($serverAddress == $remoteAddress) {
static::$environment = static::ENVIRONMENT_DEVELOPMENT;
} else {
static::$environment = static::ENVIRONMENT_PRODUCTION;
}
}
}
public static function IsDevelopment () {
return static::$environment == static::ENVIRONMENT_DEVELOPMENT;
}
public static function IsBeta () {
return static::$environment == static::ENVIRONMENT_BETA;
}
public static function IsAlpha () {
return static::$environment == static::ENVIRONMENT_ALPHA;
}
public static function IsProduction () {
return static::$environment == static::ENVIRONMENT_PRODUCTION;
}
public static function GetEnvironment () {
return static::$environment;
}
public static function SetEnvironment ($environment = self::ENVIRONMENT_PRODUCTION) {
static::$environment = $environment;
}
public static function & GetSystem () {
if (!static::$systemConfig) {
$systemConfigClass = \MvcCore::GetInstance()->GetConfigClass();
$instance = new $systemConfigClass;
static::$systemConfig = $instance->Load(
str_replace('%appPath%', \MvcCore::GetInstance()->GetAppDir(), $systemConfigClass::$SystemConfigPath)
);
}
return static::$systemConfig;
}
protected static function getServerIp () {
return isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'] ;
}
protected static function getClientIp () {
return isset($_SERVER['HTTP_X_CLIENT_IP']) ? $_SERVER['HTTP_X_CLIENT_IP'] : $_SERVER['REMOTE_ADDR'];
}
public function & Load ($configPath = '') {
$cfgFullPath = \MvcCore::GetInstance()->GetRequest()->AppRoot . $configPath;
if (!file_exists($cfgFullPath)) return FALSE;
$rawIniData = parse_ini_file($cfgFullPath, TRUE);
$environment = $this->detectEnvironmentBySystemConfig($rawIniData);
if ($rawIniData === FALSE) return FALSE;
$iniData = $this->prepareIniDataToParse($rawIniData, $environment);
$this->processIniData($iniData);
foreach ($this->objectTypes as & $objectType) {
if ($objectType[0]) $objectType[1] = (object) $objectType[1];
}
unset($this->objectTypes);
return $this->result;
}
protected function & prepareIniDataToParse (& $rawIniData, $environment) {
$iniData = array();
foreach ($rawIniData as $keyOrSectionName => $valueOrSectionValues) {
if (gettype($valueOrSectionValues) == 'array') {
if (strpos($keyOrSectionName, '>') !== FALSE) {
list($envNameLocal, $keyOrSectionName) = explode('>', str_replace(' ', '', $keyOrSectionName));
if ($envNameLocal !== $environment) continue;
}
$sectionValues = array();
foreach ($valueOrSectionValues as $key => $value) $sectionValues[$keyOrSectionName.'.'.$key] = $value;
$iniData = array_merge($iniData, $sectionValues);
} else {
$iniData[$keyOrSectionName] = $valueOrSectionValues;
}
}
return $iniData;
}
protected function detectEnvironmentBySystemConfig (array & $rawIni = array()) {
$environment = '';
if (isset($rawIni['environments'])) {
$environments = & $rawIni['environments'];
$serverAddress = ','.static::getServerIp().',';
$serverComputerName = ','.gethostname().',';
foreach ($environments as $environmentName => $environmentComputerNamesOrIps) {
$environmentComputerNamesOrIps = ','.$environmentComputerNamesOrIps.',';
if (
strpos($environmentComputerNamesOrIps, $serverAddress) !== FALSE ||
strpos($environmentComputerNamesOrIps, $serverComputerName) !== FALSE
) {
$environment = $environmentName;
break;
}
}
}
if ($environment && !static::$environment) static::SetEnvironment($environment);
return static::$environment;
}
protected function processIniData (& $iniData) {
$this->objectTypes[''] = array(1, & $this->result);
foreach ($iniData as $rawKey => $rawValue) {
$current = & $this->result;
$rawKeys = array();
$lastRawKey = $rawKey;
$lastDotPos = strrpos($rawKey, '.');
if ($lastDotPos !== FALSE) {
$rawKeys = explode('.', substr($rawKey, 0, $lastDotPos));
$lastRawKey = substr($rawKey, $lastDotPos + 1);
}
$levelKey = '';
$prevLevelKey = '';
foreach ($rawKeys as $key) {
$prevLevelKey = $levelKey;
$levelKey .= ($levelKey ? '.' : '') . $key;
if (!isset($current[$key])) {
$current[$key] = array();
$this->objectTypes[$levelKey] = array(1, & $current[$key]);
if ($this->isKeyNumeric($key) && isset($this->objectTypes[$prevLevelKey])) {
$this->objectTypes[$prevLevelKey][0] = 0;
}
}
$current = & $current[$key];
}
$typedValue = $this->getTypedValue($rawValue);
if (isset($current[$lastRawKey])) {
$current[$lastRawKey][] = $typedValue;
$this->objectTypes[$levelKey ? $levelKey : $lastRawKey][0] = 0;
} else {
if (gettype($current) != 'array') {
$current = array($current);
$this->objectTypes[$levelKey] = array(0, & $current);
}
$current[$lastRawKey] = $typedValue;
if ($this->isKeyNumeric($lastRawKey)) $this->objectTypes[$levelKey][0] = 0;
}
}
}
protected function isKeyNumeric ($rawKey) {
$numericRawKey = preg_replace("#[^0-9\-]#", '', $rawKey);
return $numericRawKey == $rawKey;
}
protected function getTypedValue ($rawValue) {
if (gettype($rawValue) == "array") {
foreach ($rawValue as $key => $value) {
$rawValue[$key] = $this->getTypedValue($value);
}
return $rawValue;
} else {
$numericRawVal = preg_replace("#[^0-9\-\.]#", '', $rawValue);
if ($numericRawVal == $rawValue) {
return $this->getTypedValueFloatIpOrInt($rawValue);
} else {
return $this->getTypedValueBoolOrString($rawValue);
}
}
}
protected function getTypedValueFloatIpOrInt ($rawValue) {
if (strpos($rawValue, '.') !== FALSE) {
if (substr_count($rawValue, '.') === 1) {
return floatval($rawValue);
} else {
return $rawValue;
}
} else {
$intVal = intval($rawValue);
return (string) $intVal === $rawValue ? $intVal : $rawValue;
}
}
protected function getTypedValueBoolOrString ($rawValue) {
$lowerRawValue = strtolower($rawValue);
if (isset(static::$booleanValues[$lowerRawValue])) {
return static::$booleanValues[$lowerRawValue];
} else {
return trim($rawValue);
}
}
}
\MvcCore\Config::StaticInit();