-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfig.php
More file actions
65 lines (60 loc) · 1.69 KB
/
Copy pathConfig.php
File metadata and controls
65 lines (60 loc) · 1.69 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
<?php
namespace coco\config;
/**
* Class Config
* @package coco\config
* @date 2017-2-10
*/
class Config
{
protected static $configDir;
public static function setConfigDir($dir)
{
if (!is_dir($dir)) {
throw new Exception('This config dir is invalided');
}
static::$configDir = realpath($dir);
}
/**
* get a config
* @param string $key
* @param bool $throw
* @return mixed|null
* @throws Exception
*/
public static function get($key, $throw = false)
{
$arr = explode('.', $key);
$configFile = static::$configDir . DIRECTORY_SEPARATOR . $arr[0] . '.php';
if (!file_exists($configFile)) {
if ($throw) {
throw new Exception("Config file `$configFile` is not exists");
} else {
return null;
}
}
unset ($arr[0]);
$config = require $configFile;
$count = count($arr);
if ($count == 0) {
return $config;
} else {
$arr = array_values($arr);
$path = '';
$value = $config;
for ($i = 0; $i < $count; $i++) {
$key = $arr[$i];
$path .= '[' . $key . ']';
if (!isset($value[$key])) {
if ($throw) {
throw new Exception("Offset `Array $path` is not defined in config file `$configFile`");
} else {
return null;
}
}
$value = $value[$key];
}
}
return $value;
}
}