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