1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class NeonAdapter implements IConfigAdapter
22: {
23:
24: public static $sectionSeparator = ' < ';
25:
26:
27: 28: 29:
30: final public function __construct()
31: {
32: throw new StaticClassException;
33: }
34:
35:
36:
37: 38: 39: 40: 41: 42:
43: public static function load($file)
44: {
45: if (!is_file($file) || !is_readable($file)) {
46: throw new FileNotFoundException("File '$file' is missing or is not readable.");
47: }
48:
49: $neon = Neon::decode(file_get_contents($file));
50:
51: $separator = trim(self::$sectionSeparator);
52: $data = array();
53: foreach ($neon as $secName => $secData) {
54: if ($secData === NULL) {
55: $secData = array();
56: }
57:
58: if (is_array($secData)) {
59:
60: $parts = $separator ? explode($separator, $secName) : array($secName);
61: if (count($parts) > 1) {
62: $parent = trim($parts[1]);
63: if (!isset($data[$parent]) || !is_array($data[$parent])) {
64: throw new InvalidStateException("Missing parent section '$parent' in file '$file'.");
65: }
66: $secData = array_reverse(Arrays::mergeTree(array_reverse($secData, TRUE), array_reverse($data[$parent], TRUE)), TRUE);
67: $secName = trim($parts[0]);
68: if ($secName === '') {
69: throw new InvalidStateException("Invalid empty section name in file '$file'.");
70: }
71: }
72: }
73:
74: $data[$secName] = $secData;
75: }
76:
77: return $data;
78: }
79:
80:
81:
82: 83: 84: 85: 86: 87:
88: public static function save($config, $file)
89: {
90: if (!file_put_contents($file, "# generated by Nette\n\n" . Neon::encode($config, Neon::BLOCK))) {
91: throw new IOException("Cannot write file '$file'.");
92: }
93: }
94:
95: }
96: