1 <?php
2 3 4 5 6 7
8
9 namespace Cross\Core;
10
11 use Cross\Exception\CoreException;
12
13 14 15 16 17
18 class Config
19 {
20 21 22
23 private $res_file;
24
25 26 27
28 private $config_data;
29
30 31 32
33 private static $instance;
34
35 36 37
38 private $ca;
39
40 41 42 43 44
45 private static $cache;
46
47 48 49 50 51 52
53 private function __construct($res_file)
54 {
55 $this->res_file = $res_file;
56 $this->config_data = Loader::read($res_file);
57
58 $this->ca = CrossArray::init($this->config_data, $this->res_file);
59 }
60
61 62 63 64 65 66 67
68 static function load($file)
69 {
70 if (!isset(self::$instance[$file])) {
71 self::$instance[$file] = new self($file);
72 }
73
74 return self::$instance[$file];
75 }
76
77 78 79 80 81 82
83 function combine(array $append_config = array())
84 {
85 if (!empty($append_config)) {
86 foreach ($append_config as $key => $value) {
87 if (isset($this->config_data[$key]) && is_array($value)) {
88 $this->config_data[$key] = array_merge($this->config_data[$key], $value);
89 } else {
90 $this->config_data[$key] = $value;
91 }
92
93 $this->clearIndexCache($key);
94 }
95 }
96
97 return $this;
98 }
99
100 101 102 103 104 105 106
107 function get($index, $options = '')
108 {
109 $key = $this->getIndexCacheKey($index);
110 if (is_array($options)) {
111 $opk = implode('.', $options);
112 } elseif ($options) {
113 $opk = $options;
114 } else {
115 $opk = '-###-';
116 }
117
118 if (!isset(self::$cache[$key][$opk])) {
119 self::$cache[$key][$opk] = $this->ca->get($index, $options);
120 }
121
122 return self::$cache[$key][$opk];
123 }
124
125 126 127 128 129 130 131
132 function set($index, $values = '')
133 {
134 $result = $this->ca->set($index, $values);
135 $this->clearIndexCache($index);
136
137 return $result;
138 }
139
140 141 142 143 144 145
146 function getAll($obj = false)
147 {
148 if ($obj) {
149 return CrossArray::arrayToObject($this->config_data);
150 }
151
152 return $this->config_data;
153 }
154
155 156 157 158 159 160
161 protected function getIndexCacheKey($index)
162 {
163 return $this->res_file . '.' . $index;
164 }
165
166 167 168 169 170
171 protected function clearIndexCache($index)
172 {
173 $key = $this->getIndexCacheKey($index);
174 unset(self::$cache[$key]);
175 }
176 }
177