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:
<?php
namespace MvcCore\Ext\Configs\Yamls;
use Symfony\Component\Yaml\Yaml;
trait YamlRead {
protected static $readingFlags = 0;
public static function SetReadingFlags ($readingFlags) {
return self::$readingFlags = $readingFlags;
}
public static function GetReadingFlags () {
return self::$readingFlags;
}
public function Read () {
if ($this->envData) return TRUE;
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$environmentClass = $app->GetEnvironmentClass();
$allEnvNames = array_merge([''], $environmentClass::GetAllNames());
$fullPathLastDot = mb_strrpos($this->fullPath, '.');
$fullPathParts = [
mb_substr($this->fullPath, 0, $fullPathLastDot),
'',
mb_substr($this->fullPath, $fullPathLastDot),
];
foreach ($allEnvNames as $envName) {
$commonEnv = $envName === '';
if ($commonEnv) {
$fullPath = $this->fullPath;
} else {
$fullPathParts[1] = '.' . $envName;
$fullPath = implode('', $fullPathParts);
}
clearstatcache(TRUE, $fullPath);
if (!file_exists($fullPath)) {
if ($commonEnv) return FALSE;
continue;
}
$lastChanged = filemtime($fullPath);
if ($lastChanged > $this->lastChanged)
$this->lastChanged = $lastChanged;
$rawContent = file_get_contents($fullPath);
if ($rawContent === FALSE) {
if ($commonEnv) return FALSE;
continue;
}
$rawYamlData = NULL;
try {
$rawYamlData = Yaml::parse(
str_replace("\t", ' ', $rawContent),
self::$readingFlags
);
} catch (\Exception $e) {
\MvcCore\Debug::Exception($e);
} catch (\Throwable $e) {
\MvcCore\Debug::Exception($e);
}
if (!$rawYamlData) {
if ($commonEnv) return FALSE;
continue;
}
$objectTypes = [];
foreach ($rawYamlData as $firstLevelKey => & $firstlLevelValue)
$this->readYamlObjectTypes($objectTypes, $firstlLevelValue, $firstLevelKey);
foreach ($objectTypes as & $objectType)
if ($objectType[0]) $objectType[1] = (object) $objectType[1];
unset($objectTypes);
$this->envData[$envName] = $rawYamlData;
}
return TRUE;
}
protected function readYamlObjectTypes (& $objectTypes, & $data, $levelKey) {
if (is_array($data)) {
$numericKeyCatched = FALSE;
foreach ($data as $key => & $value) {
if (is_numeric($key))
$numericKeyCatched = TRUE;
if (is_array($value))
$this->readYamlObjectTypes($objectTypes, $value, $levelKey . '.' . $key);
}
$objectTypes[$levelKey] = [$numericKeyCatched ? 0 : 1, & $data];
}
}
}