-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPConfig.php
More file actions
388 lines (330 loc) · 9.65 KB
/
Copy pathPConfig.php
File metadata and controls
388 lines (330 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace pconfig;
use ArrayAccess;
use Closure;
use Exception;
use pconfig\provider\impl\FileProvider;
use pconfig\provider\IProvider;
use pconfig\serializer\ISerializer;
use pconfig\utils\ArrayUtil;
use pconfig\utils\ClassUtil;
/**
* Class PConfig
* @package pconfig
*/
class PConfig implements ArrayAccess
{
const CONFIG_KEY_FILE = "file";
const CONFIG_KEY_DATA = "data";
const CONFIG_KEY_PROVIDER = "provider";
const CONFIG_KEY_SERIALIZER = "serializer";
const CONFIG_KEY_EXTRACT_CONFIG = "config";
/**
* Key for config key separator
*/
const CONFIG_KEY_EXTRACT_SEPARATOR = 'separator';
const EXISTS_MODE_BREAK = 0;
const EXISTS_MODE_APPEND = 1;
/**
* @var ISerializer config serializer implementation
*/
private $serializer;
/**
* @var FileProvider config data provider implementation
*/
private $provider;
/**
* @var array data
*/
private $data = [];
/**
* @var array parsing config
*/
private $config = [
self::CONFIG_KEY_EXTRACT_SEPARATOR => '.',
];
/**
* $config = [
* 'data' => [],
* 'file' => string,
* 'serializer' => ISerializer,
* 'provider' => IProvider,
* 'config' => []
* ]
* PConfig constructor.
* @param $config
* @throws Exception
*/
public function __construct($config)
{
if (is_string($config)) {
$this->provider = new FileProvider($config);
$this->serializer = $this->detectSerializer($config);
$this->loadData();
} elseif (is_array($config)) {
$file = null;
$this->provider = $config[self::CONFIG_KEY_PROVIDER];
$this->serializer = $config[self::CONFIG_KEY_SERIALIZER];
if (isset($config[self::CONFIG_KEY_DATA])) {
$this->data = $config[self::CONFIG_KEY_DATA];
} elseif (isset($config[self::CONFIG_KEY_FILE])) {
$file = $config[self::CONFIG_KEY_FILE];
if (is_null($this->provider)) {
$this->provider = new FileProvider($file);
}
if (is_null($this->serializer)) {
$this->serializer = $this->detectSerializer($file);
}
$this->loadData();
} else {
if (is_null($this->provider)) {
throw new Exception("must provide 'data', 'file' or 'provider'");
}
$this->loadData();
}
if (isset($config[self::CONFIG_KEY_EXTRACT_CONFIG])) {
$extraConfig = $config[self::CONFIG_KEY_EXTRACT_CONFIG];
if (is_array($extraConfig)) {
$this->config = array_merge($this->config, $extraConfig);
}
}
} else {
throw new Exception('invalid parameter, require a string or an array');
}
}
/**
* @param $file
* @return ISerializer
* @throws Exception
*/
private function detectSerializer($file)
{
$extension = pathinfo($file, PATHINFO_EXTENSION);
$fileName = strtoupper($extension) . 'Serializer';
$serializer = ClassUtil::loadClass("pconfig\\serializer\\impl\\{$fileName}");
if (!$serializer instanceof ISerializer) {
throw new Exception("Class {$fileName} is not a ISerializer!");
}
return $serializer;
}
/**
* @throws Exception
*/
private function loadData()
{
if (!$this->serializer instanceof ISerializer) {
throw new Exception('must specify a serializer');
}
$content = $this->provider->read();
$this->data = $this->serializer->deserialize($content);
}
/**
* @throws Exception
*/
public function reload()
{
$this->loadData();
}
/**
* Save config to storage
* @return bool
* @throws Exception
*/
public function save()
{
if (!$this->serializer instanceof ISerializer) {
throw new Exception('must specify a serializer');
}
$content = $this->serializer->serialize($this->data);
return $this->provider->save($content);
}
/**
* Set config file path
* @param string $file file path
*/
public function setFile($file)
{
$this->provider->setFile($file);
}
/**
* @param IProvider $provider
*/
public function setProvider($provider)
{
$this->provider = $provider;
}
/**
* @param ISerializer $serializer
*/
public function setSerializer($serializer)
{
$this->serializer = $serializer;
}
public function offsetExists($offset)
{
return $this->exists($offset);
}
/**
* SPL interface
* See `get` method
* @param mixed $key config key
* @return array|null config value
*/
public function offsetGet($key)
{
return $this->get($key);
}
/**
* SPL interface
* See `set` method
* @param mixed $key config key
* @param mixed $value config value
*/
public function offsetSet($key, $value)
{
$this->set($key, $value);
}
/**
* SPL interface
* See `delete` method
* @param mixed $key config key
*/
public function offsetUnset($key)
{
$this->delete($key);
}
/**
* Get config data
* @param null $key config key with a key-separator
* @param mixed $default default value, will be return if key does not present
* @return array|null value
*/
public function get($key = null, $default = null)
{
if ($key == null) return $this->data;
if (empty($key)) return [];
$keys = $this->parseKey($key);
$value = null;
$this->find($this->data, $keys, function ($item) use (&$value) {
$value = $item;
});
if ($default !== null && $value !== false) return $default;
return $value;
}
/**
* Remove config data
* @param null $key config key with a key-separator
* @return bool Weather remove successfully
*/
public function delete($key = null)
{
if ($key == null) {
$this->data = [];
return true;
}
if (empty($key)) return false;
$keys = $this->parseKey($key);
return $this->find($this->data, $keys,
function (/** @noinspection PhpUnusedParameterInspection */ $item, &$config, $index) {
$isAssoc = ArrayUtil::isAssocArray($config);
unset($config[$index]);
//不是关联数组的数组需要从新排序下标
if (!$isAssoc) {
$config = array_values($config);
}
});
}
/**
* Set config data
* @param string $key config key with a key-separator
* @param mixed $value config data value
* @return bool Weather set successfully
*/
public function set($key, $value)
{
if (empty($key)) return false;
$keys = $this->parseKey($key);
return $this->find($this->data, $keys, function (&$item) use ($value) {
$item = $value;
}, self::EXISTS_MODE_APPEND);
}
/**
* Get extracting config by a key
* @param null $key config key. if null, returns whole config
* @return array|mixed|null value
*/
public function getConfig($key = null)
{
if ($key == null) return $this->config;
return isset($this->config[$key]) ? $this->config[$key] : null;
}
/**
* Set extracting config
* @param string|array $key config key
* @param mixed $value config value
*/
public function setConfig($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
isset($this->config[$k]) && $this->config[$k] = $v;
}
} else {
isset($this->config[$key]) && $this->config[$key] = $value;
}
}
/**
* SPL interface
* Whether a key exists
* @param mixed $key key
* @return bool
*/
public function exists($key)
{
$keys = $this->parseKey($key);
return $this->find($this->data, $keys);
}
public function clear()
{
$this->data = [];
}
/**
* Explode config key by key-separator
* @param $key string key
* @return array exploded key in array
*/
private function parseKey($key)
{
return explode($this->config[self::CONFIG_KEY_EXTRACT_SEPARATOR], $key);
}
/**
* 寻找目标配置项
* @param array $config 配置数组
* @param array $keys $key 配置项下标数组
* @param Closure|null $callback 处理回调
* @param int $existsMode 元素不能存在的时候的处理方式
* @param int $index 当前配置下标的位置
* @return bool 是否找到配置项
*/
private function find(array &$config, array $keys, Closure $callback = null, $existsMode = self::EXISTS_MODE_BREAK, $index = 0)
{
if (!isset($config[$keys[$index]])) {
if ($existsMode == self::EXISTS_MODE_BREAK) return false;
$config[$keys[$index]] = [];
}
if (count($keys) - 1 == $index) {
is_callable($callback) && $callback($config[$keys[$index]], $config, $keys[$index]);
return true;
}
if (is_array($config[$keys[$index]])) {
return $this->find(
$config[$keys[$index]],
$keys,
$callback,
$existsMode,
$index + 1
);
}
return false;
}
}