| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Core; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | trait ConfigTrait |
| 18: | { |
| 19: | |
| 20: | protected array $config = []; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | public function readConfig($files = ['config.php'], string $format = 'php') |
| 36: | { |
| 37: | if (!is_array($files)) { |
| 38: | $files = [$files]; |
| 39: | } |
| 40: | |
| 41: | $configs = []; |
| 42: | foreach ($files as $file) { |
| 43: | if (!is_readable($file)) { |
| 44: | throw (new Exception('Cannot read config file')) |
| 45: | ->addMoreInfo('file', $file) |
| 46: | ->addMoreInfo('format', $format); |
| 47: | } |
| 48: | |
| 49: | $tempConfig = []; |
| 50: | |
| 51: | switch (strtolower($format)) { |
| 52: | case 'php': |
| 53: | $tempConfig = require $file; |
| 54: | |
| 55: | break; |
| 56: | case 'json': |
| 57: | $tempConfig = json_decode(file_get_contents($file), true); |
| 58: | |
| 59: | break; |
| 60: | case 'yaml': |
| 61: | $tempConfig = \Symfony\Component\Yaml\Yaml::parseFile($file); |
| 62: | |
| 63: | break; |
| 64: | default: |
| 65: | throw (new Exception('Unknown Format. Allowed formats: php, json, yml')) |
| 66: | ->addMoreInfo('file', $file) |
| 67: | ->addMoreInfo('format', $format); |
| 68: | } |
| 69: | |
| 70: | if (!is_array($tempConfig)) { |
| 71: | throw (new Exception('File was read but has a bad format')) |
| 72: | ->addMoreInfo('file', $file) |
| 73: | ->addMoreInfo('format', $format); |
| 74: | } |
| 75: | |
| 76: | $configs[] = $tempConfig; |
| 77: | } |
| 78: | |
| 79: | $this->config = array_replace_recursive($this->config, ...$configs); |
| 80: | |
| 81: | return $this; |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function setConfig($paths = [], $value = null) |
| 93: | { |
| 94: | if (!is_array($paths)) { |
| 95: | $paths = [$paths => $value]; |
| 96: | } |
| 97: | unset($value); |
| 98: | |
| 99: | foreach ($paths as $path => $value) { |
| 100: | $pos = &$this->_lookupConfigElement($path, true); |
| 101: | |
| 102: | if (is_array($pos) && count($pos) > 0 && is_array($value)) { |
| 103: | |
| 104: | $pos = array_merge($pos, $value); |
| 105: | } else { |
| 106: | |
| 107: | $pos = $value; |
| 108: | } |
| 109: | } |
| 110: | |
| 111: | return $this; |
| 112: | } |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | public function getConfig(string $path, $defaultValue = null) |
| 123: | { |
| 124: | $pos = &$this->_lookupConfigElement($path, false); |
| 125: | |
| 126: | |
| 127: | if ($pos === false) { |
| 128: | return $defaultValue; |
| 129: | } |
| 130: | |
| 131: | return $pos; |
| 132: | } |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | private function &_lookupConfigElement(string $path, bool $createElements = false) |
| 143: | { |
| 144: | $path = explode('/', $path); |
| 145: | $pos = &$this->config; |
| 146: | foreach ($path as $el) { |
| 147: | if (!is_array($pos) || !array_key_exists($el, $pos)) { |
| 148: | if (!is_array($pos) || !$createElements) { |
| 149: | $res = false; |
| 150: | |
| 151: | return $res; |
| 152: | } |
| 153: | |
| 154: | $pos[$el] = []; |
| 155: | } |
| 156: | |
| 157: | $pos = &$pos[$el]; |
| 158: | } |
| 159: | |
| 160: | return $pos; |
| 161: | } |
| 162: | } |
| 163: | |