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:
<?php
namespace MvcCore\Config;
trait IniRead {
public function Read () {
if ($this->envData) return TRUE;
$iniScannerMode = \PHP_VERSION_ID < 50610 ? 1 : 2;
clearstatcache(TRUE, $this->fullPath);
$this->lastChanged = filemtime($this->fullPath);
$rawIniData = parse_ini_file(
$this->fullPath, TRUE, $iniScannerMode
);
if ($rawIniData === FALSE) return FALSE;
$this->envData = [];
$allEnvIniDataPlain = static::readAllEnvironmentsSections($rawIniData);
foreach ($allEnvIniDataPlain as $envName => $envIniData) {
list($data, $objectTypes) = static::readExpandLevelsAndReType(
$envIniData, $iniScannerMode
);
foreach ($objectTypes as & $objectType)
if ($objectType[0])
$objectType[1] = (object) $objectType[1];
$this->envData[$envName] = $data;
}
return TRUE;
}
protected static function & readAllEnvironmentsSections (array & $rawIniData) {
$allEnvsIniData = [];
$commonEnvDataKey = static::$commonEnvironmentDataKey;
$environmentNamesFilter = static::$environmentNamesFilter;
$sectionNamesFilter = static::$sectionNamesFilter;
foreach ($rawIniData as $keyOrSectionName => $valueOrSectionValues) {
$parsedEnvNames = [];
if (is_array($valueOrSectionValues)) {
$pos = mb_strpos($keyOrSectionName, '>');
if ($pos === FALSE) {
$key = $keyOrSectionName;
} else {
$envNames = mb_substr($keyOrSectionName, 0, $pos);
$envNames = preg_replace($environmentNamesFilter, '', $envNames);
$key = mb_substr($keyOrSectionName, $pos + 1);
$key = preg_replace($sectionNamesFilter, '', $key);
$parsedEnvNames = explode(',', $envNames);
}
$newValues = [];
foreach ($valueOrSectionValues as $subKey => $subValue)
$newValues[$key.'.'.$subKey] = $subValue;
} else {
$newValues = [$keyOrSectionName => $valueOrSectionValues];
}
if (!$parsedEnvNames)
$parsedEnvNames = [$commonEnvDataKey];
foreach ($parsedEnvNames as $parsedEnvName) {
if (!array_key_exists($parsedEnvName, $allEnvsIniData))
$allEnvsIniData[$parsedEnvName] = [];
$allEnvsIniData[$parsedEnvName] = array_merge($allEnvsIniData[$parsedEnvName], $newValues);
}
}
return $allEnvsIniData;
}
protected static function readExpandLevelsAndReType (array & $iniData, $iniScannerMode) {
$result = [];
$objectTypes = [];
$oldIniScannerMode = $iniScannerMode === 1;
foreach ($iniData as $rawKey => $rawValue) {
$current = & $result;
$rawKeys = [];
$lastRawKey = $rawKey;
$lastDotPos = strrpos($rawKey, '.');
if ($lastDotPos !== FALSE) {
$rawKeys = explode('.', substr($rawKey, 0, $lastDotPos));
$lastRawKey = substr($rawKey, $lastDotPos + 1);
}
$absoluteKey = '';
$prevAbsoluteKey = '';
foreach ($rawKeys as $key) {
$prevAbsoluteKey = $absoluteKey;
$absoluteKey .= ($absoluteKey ? '.' : '') . $key;
if (!isset($current[$key])) {
$keyIsNumeric = is_numeric($key);
$current[$key] = [];
$objectTypes[$absoluteKey] = [0, & $current[$key]];
if (isset($objectTypes[$prevAbsoluteKey])) {
$objTypesRec = & $objectTypes[$prevAbsoluteKey];
if (!$keyIsNumeric && !$objTypesRec[0])
$objTypesRec[0] = 1;
}
}
$current = & $current[$key];
}
if ($oldIniScannerMode) {
$typedValue = static::readTypedValue($rawValue);
} else {
$typedValue = $rawValue;
}
if (isset($current[$lastRawKey])) {
$current[$lastRawKey][] = $typedValue;
} else {
if (!is_array($current))
$current = [$current];
$current[$lastRawKey] = $typedValue;
}
if (!is_numeric($lastRawKey))
$objectTypes[$absoluteKey][0] = 1;
}
return [$result, $objectTypes];
}
protected static function readTypedValue ($rawValue) {
if (gettype($rawValue) == "array") {
foreach ($rawValue as $key => $value) {
$rawValue[$key] = static::readTypedValue($value);
}
return $rawValue;
} else if (mb_strlen($rawValue) > 0) {
if (is_numeric($rawValue)) {
return static::readTypedValueFloatOrInt($rawValue);
} else {
return static::readTypedSpecialValueOrString($rawValue);
}
} else {
return static::readTypedSpecialValueOrString($rawValue);
}
}
protected static function readTypedValueFloatOrInt ($rawValue) {
if (strpos($rawValue, '.') !== FALSE || strpos($rawValue, 'e') !== FALSE || strpos($rawValue, 'E') !== FALSE) {
return floatval($rawValue);
} else {
$intVal = intval($rawValue);
return (string) $intVal === $rawValue
? $intVal
: $rawValue;
}
}
protected static function readTypedSpecialValueOrString ($rawValue) {
$lowerRawValue = strtolower($rawValue);
if (isset(static::$specialValues[$lowerRawValue])) {
return static::$specialValues[$lowerRawValue];
} else {
return trim($rawValue);
}
}
}