Config::load()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 2
b 0
f 1
nc 2
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
    namespace rAPId\Config;
4
5
    use rAPId\Data\ArrayFileReader;
6
7
    class Config
8
    {
9
        protected static $config = [];
10
        public static    $err    = '';
11
12
        /**
13
         * @param string $filename
14
         * @param string $prefix [optional]
15
         *                       If prefix is used, all config options loaded will be indexed
16
         *                       in dot notation ie prefix.option
17
         */
18
        public static function load($filename, $prefix = '') {
19
            $reader = new ArrayFileReader($filename);
20
            $new_config = $reader->loadArray();
21
22
            if (!empty($prefix)) {
23
                foreach ($new_config as $key => $value) {
24
                    $new_config[ $prefix . '.' . $key ] = $value;
25
                    unset($new_config[ $key ]);
26
                }
27
            }
28
29
            self::$config = merge(self::$config, $new_config);
30
        }
31
32
        /**
33
         * Get the value of a config var
34
         *
35
         * @param string $var_name
36
         * @param mixed  $default
37
         *
38
         * @return mixed|null
39
         */
40
        public static function val($var_name, $default = null) {
41
            return array_get(self::$config, strtolower($var_name), $default);
42
        }
43
    }