1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: class NConfig implements ArrayAccess, IteratorAggregate
21: {
22:
23: private static $extensions = array(
24: 'ini' => 'NConfigAdapterIni',
25: );
26:
27:
28:
29: 30: 31: 32: 33: 34:
35: public static function registerExtension($extension, $class)
36: {
37: if (!class_exists($class)) {
38: throw new InvalidArgumentException("Class '$class' was not found.");
39: }
40:
41: if (!NClassReflection::from($class)->implementsInterface('IConfigAdapter')) {
42: throw new InvalidArgumentException("Configuration adapter '$class' is not Nette\\Config\\IConfigAdapter implementor.");
43: }
44:
45: self::$extensions[strtolower($extension)] = $class;
46: }
47:
48:
49:
50: 51: 52: 53: 54: 55:
56: public static function fromFile($file, $section = NULL)
57: {
58: $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
59: if (isset(self::$extensions[$extension])) {
60: $arr = call_user_func(array(self::$extensions[$extension], 'load'), $file, $section);
61: return new self($arr);
62:
63: } else {
64: throw new InvalidArgumentException("Unknown file extension '$file'.");
65: }
66: }
67:
68:
69:
70: 71: 72:
73: public function __construct($arr = NULL)
74: {
75: foreach ((array) $arr as $k => $v) {
76: $this->$k = is_array($v) ? new self($v) : $v;
77: }
78: }
79:
80:
81:
82: 83: 84: 85: 86: 87:
88: public function save($file, $section = NULL)
89: {
90: $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
91: if (isset(self::$extensions[$extension])) {
92: return call_user_func(array(self::$extensions[$extension], 'save'), $this, $file, $section);
93:
94: } else {
95: throw new InvalidArgumentException("Unknown file extension '$file'.");
96: }
97: }
98:
99:
100:
101:
102:
103:
104:
105: public function __set($key, $value)
106: {
107: if (!is_scalar($key)) {
108: throw new InvalidArgumentException("Key must be either a string or an integer.");
109:
110: } elseif ($value === NULL) {
111: unset($this->$key);
112:
113: } else {
114: $this->$key = $value;
115: }
116: }
117:
118:
119:
120: public function &__get($key)
121: {
122: if (!is_scalar($key)) {
123: throw new InvalidArgumentException("Key must be either a string or an integer.");
124: }
125: return $this->$key;
126: }
127:
128:
129:
130: public function __isset($key)
131: {
132: return FALSE;
133: }
134:
135:
136:
137: public function __unset($key)
138: {
139: }
140:
141:
142:
143: 144: 145: 146: 147: 148:
149: public function offsetSet($key, $value)
150: {
151: $this->__set($key, $value);
152: }
153:
154:
155:
156: 157: 158: 159: 160:
161: public function offsetGet($key)
162: {
163: if (!is_scalar($key)) {
164: throw new InvalidArgumentException("Key must be either a string or an integer.");
165:
166: } elseif (!isset($this->$key)) {
167: return NULL;
168: }
169: return $this->$key;
170: }
171:
172:
173:
174: 175: 176: 177: 178:
179: public function offsetExists($key)
180: {
181: if (!is_scalar($key)) {
182: throw new InvalidArgumentException("Key must be either a string or an integer.");
183: }
184: return isset($this->$key);
185: }
186:
187:
188:
189: 190: 191: 192: 193:
194: public function offsetUnset($key)
195: {
196: if (!is_scalar($key)) {
197: throw new InvalidArgumentException("Key must be either a string or an integer.");
198: }
199: unset($this->$key);
200: }
201:
202:
203:
204: 205: 206: 207:
208: public function getIterator()
209: {
210: return new NGenericRecursiveIterator(new ArrayIterator($this));
211: }
212:
213:
214:
215: 216: 217:
218: public function toArray()
219: {
220: $arr = array();
221: foreach ($this as $k => $v) {
222: $arr[$k] = $v instanceof self ? $v->toArray() : $v;
223: }
224: return $arr;
225: }
226:
227: }
228: