1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class ConfigNeonAdapter extends Object implements IConfigAdapter
22: {
23:
24: const INHERITING_SEPARATOR = '<',
25: PREVENT_MERGING = '!';
26:
27: 28: 29: 30: 31:
32: public function load($file)
33: {
34: return $this->process((array) Neon::decode(file_get_contents($file)));
35: }
36:
37:
38: private function process(array $arr)
39: {
40: $res = array();
41: foreach ($arr as $key => $val) {
42: if (substr($key, -1) === self::PREVENT_MERGING) {
43: if (!is_array($val) && $val !== NULL) {
44: throw new InvalidStateException("Replacing operator is available only for arrays, item '$key' is not array.");
45: }
46: $key = substr($key, 0, -1);
47: $val[ConfigHelpers::EXTENDS_KEY] = ConfigHelpers::OVERWRITE;
48:
49: } elseif (preg_match('#^(\S+)\s+' . self::INHERITING_SEPARATOR . '\s+(\S+)\z#', $key, $matches)) {
50: if (!is_array($val) && $val !== NULL) {
51: throw new InvalidStateException("Inheritance operator is available only for arrays, item '$key' is not array.");
52: }
53: list(, $key, $val[ConfigHelpers::EXTENDS_KEY]) = $matches;
54: if (isset($res[$key])) {
55: throw new InvalidStateException("Duplicated key '$key'.");
56: }
57: }
58:
59: if (is_array($val)) {
60: $val = $this->process($val);
61: } elseif ($val instanceof NeonEntity) {
62: $val = (object) array('value' => $val->value, 'attributes' => $this->process($val->attributes));
63: }
64: $res[$key] = $val;
65: }
66: return $res;
67: }
68:
69:
70: 71: 72: 73:
74: public function dump(array $data)
75: {
76: $tmp = array();
77: foreach ($data as $name => $secData) {
78: if ($parent = ConfigHelpers::takeParent($secData)) {
79: $name .= ' ' . self::INHERITING_SEPARATOR . ' ' . $parent;
80: }
81: $tmp[$name] = $secData;
82: }
83: return "# generated by Nette\n\n" . Neon::encode($tmp, Neon::BLOCK);
84: }
85:
86: }
87: