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