1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Config;
13:
14: class Loader extends \ManiaLib\Utils\Singleton
15: {
16: static $aliases = array(
17: 'config' => 'ManiaLive\\Config\\Config',
18: 'database' => 'ManiaLive\\Database\\Config',
19: 'wsapi' => 'ManiaLive\\Features\\WebServices\\Config',
20: 'manialive' => 'ManiaLive\\Application\\Config',
21: 'server' => 'ManiaLive\\DedicatedApi\\Config',
22: 'threading' => 'ManiaLive\\Threading\\Config',
23: );
24:
25: protected $configFilename;
26: protected $debugPrefix = '[CONFIG LOADER]';
27:
28: function setConfigFilename($configFilename)
29: {
30: $this->configFilename = $configFilename;
31: }
32:
33: final public function run()
34: {
35: $mtime = microtime(true);
36: $this->preLoad();
37: $this->data = $this->load();
38: $mtime = microtime(true) - $mtime;
39: $this->postLoad();
40: }
41:
42: protected function preLoad()
43: {
44: if(!file_exists($this->configFilename))
45: {
46: throw new \Exception($this->configFilename.' does not exist');
47: }
48: }
49:
50: protected function postLoad()
51: {
52:
53: }
54:
55: 56: 57:
58: protected function load()
59: {
60: $values = $this->loadINI($this->configFilename);
61: list($values, $overrides) = $this->scanOverrides($values);
62: $values = $this->processOverrides($values, $overrides);
63: $values = $this->loadAliases($values);
64: $values = $this->replaceAliases($values);
65: $instances = $this->arrayToSingletons($values);
66: return $instances;
67: }
68:
69: 70: 71:
72: protected function loadINI($filename)
73: {
74: try
75: {
76: return parse_ini_file($filename, true);
77: }
78: catch(Exception $e)
79: {
80: throw new Exception('Could not parse INI file: '.$e->getMessage());
81: }
82: }
83:
84: 85: 86: 87:
88: protected function scanOverrides(array $array)
89: {
90: $values = array();
91: $overrides = array();
92:
93: foreach($array as $key => $value)
94: {
95: if(strstr($key, ':'))
96: {
97: $overrides[$key] = $value;
98: }
99: else
100: {
101: $values[$key] = $value;
102: }
103: }
104: return array($values, $overrides);
105: }
106:
107: 108: 109: 110: 111:
112: protected function processOverrides(array $values, array $overrides)
113: {
114: if($overrides)
115: {
116: foreach($overrides as $key => $override)
117: {
118: $matches = null;
119: if(preg_match('/^hostname: (.+)$/i', $key, $matches))
120: {
121: if($matches[1] == gethostname())
122: {
123: $values = $this->overrideArray($values, $override);
124: break;
125: }
126: }
127: }
128: }
129: return $values;
130: }
131:
132: 133: 134: 135: 136:
137: protected function overrideArray(array $source, array $override)
138: {
139: foreach($override as $key => $value)
140: {
141: $source[$key] = $value;
142: }
143: return $source;
144: }
145:
146: 147: 148:
149: protected function loadAliases(array $values)
150: {
151: foreach ($values as $key => $value)
152: {
153: if(preg_match('/^\s*alias\s+(\S+)$/i', $key, $matches))
154: {
155: if(isset($matches[1]))
156: {
157: self::$aliases[$matches[1]] = $value;
158: unset($values[$key]);
159: }
160: }
161: }
162: return $values;
163: }
164:
165: 166: 167:
168: protected function replaceAliases(array $values)
169: {
170: $newValues = array();
171: foreach ($values as $key => $value)
172: {
173: $callback = explode('.', $key, 2);
174: if(count($callback) == 2)
175: {
176: $className = reset($callback);
177: $propertyName = end($callback);
178: if(isset(self::$aliases[$className]))
179: {
180: $className = self::$aliases[$className];
181: }
182: $newValues[$className.'.'.$propertyName] = $value;
183: }
184: else
185: {
186: $newValues[$key] = $value;
187: }
188: }
189: return $newValues;
190: }
191:
192: 193: 194:
195: protected function arrayToSingletons($values)
196: {
197: $instances = array();
198: foreach($values as $key => $value)
199: {
200: $callback = explode('.', $key, 2);
201: $className = reset($callback);
202: $propertyName = end($callback);
203: $instance = call_user_func(array($className, 'getInstance'));
204:
205: $instance->$propertyName = $value;
206: $instances[$className] = $instance;
207: }
208: return $instances;
209: }
210: }
211:
212: ?>