1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class Config
22: {
23:
24: private static $extensions = array(
25: 'ini' => 'IniAdapter',
26: 'neon' => 'NeonAdapter',
27: );
28:
29:
30:
31: 32: 33:
34: final public function __construct()
35: {
36: throw new StaticClassException;
37: }
38:
39:
40:
41: 42: 43: 44: 45: 46:
47: public static function registerExtension($extension, $class)
48: {
49: if (!class_exists($class)) {
50: throw new InvalidArgumentException("Class '$class' was not found.");
51: }
52:
53: if (!ClassReflection::from($class)->implementsInterface('IConfigAdapter')) {
54: throw new InvalidArgumentException("Configuration adapter '$class' is not IConfigAdapter implementor.");
55: }
56:
57: self::$extensions[strtolower($extension)] = $class;
58: }
59:
60:
61:
62: 63: 64: 65: 66: 67:
68: public static function fromFile($file, $section = NULL)
69: {
70: $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
71: if (!isset(self::$extensions[$extension])) {
72: throw new InvalidArgumentException("Unknown file extension '$file'.");
73: }
74:
75: $data = call_user_func(array(self::$extensions[$extension], 'load'), $file, $section);
76: if ($section) {
77: if (!isset($data[$section]) || !is_array($data[$section])) {
78: throw new InvalidStateException("There is not section [$section] in file '$file'.");
79: }
80: $data = $data[$section];
81: }
82: return $data;
83: }
84:
85:
86:
87: 88: 89: 90: 91: 92:
93: public static function save($config, $file)
94: {
95: $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
96: if (!isset(self::$extensions[$extension])) {
97: throw new InvalidArgumentException("Unknown file extension '$file'.");
98: }
99: return call_user_func(array(self::$extensions[$extension], 'save'), $config, $file);
100: }
101:
102: }
103: