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