| Total Complexity | 9 |
| Total Lines | 61 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | class Config |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Config |
||
| 16 | */ |
||
| 17 | protected static $instance; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array 配置 |
||
| 21 | */ |
||
| 22 | protected $allConfig; |
||
| 23 | |||
| 24 | public function __construct() |
||
| 25 | { |
||
| 26 | $this->allConfig = require ROOT_PATH . '/config.php'; |
||
|
|
|||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * 获取配置 |
||
| 31 | * |
||
| 32 | * @param string $key |
||
| 33 | * @param string $default 默认值 |
||
| 34 | * |
||
| 35 | * @return array|mixed|null |
||
| 36 | */ |
||
| 37 | public function get($key = '', $default = null) |
||
| 38 | { |
||
| 39 | $allConfig = $this->allConfig; |
||
| 40 | |||
| 41 | if (strlen($key)) { |
||
| 42 | if (strpos($key, '.')) { |
||
| 43 | $keys = explode('.', $key); |
||
| 44 | $val = $allConfig; |
||
| 45 | foreach ($keys as $k) { |
||
| 46 | if (!isset($val[$k])) { |
||
| 47 | return $default; // 任一下标不存在就返回默认值 |
||
| 48 | } |
||
| 49 | |||
| 50 | $val = $val[$k]; |
||
| 51 | } |
||
| 52 | |||
| 53 | return $val; |
||
| 54 | } else { |
||
| 55 | if (isset($allConfig[$key])) { |
||
| 56 | return $allConfig[$key]; |
||
| 57 | } |
||
| 58 | |||
| 59 | return $default; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | return $allConfig; |
||
| 64 | } |
||
| 65 | |||
| 66 | public static function instance() |
||
| 73 | } |
||
| 74 | } |