ConfigLoader::getConfParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
    namespace CloudFramework\Tool;
3
    use CloudFramework\Patterns\Singleton;
4
5
    /**
6
     * Class ConfigLoader
7
     * @package CloudFramework\Core
8
     */
9
    class ConfigLoader extends Singleton
10
    {
11
        private $config = array();
12
13 1
        public function __construct($configFilename = '')
14
        {
15 1
            if (strlen($configFilename)) {
16
                $this->loadConfigFile($configFilename);
17
            }
18 1
        }
19
20
        /**
21
         * Search config key and returns this value
22
         *
23
         * @param string $key
24
         *
25
         * @return mixed
26
         */
27 1
        public function getConf($key)
28
        {
29 1
            $value = NULL;
30 1
            if (count($this->config) > 0) {
31 1
                if (array_key_exists($key, $this->config)) {
32 1
                    $value = $this->config[$key];
33 1
                }
34 1
            }
35 1
            return $value;
36
        }
37
38
        /**
39
         * Set a config parameter
40
         *
41
         * @param string $key
42
         * @param mixed $value
43
         *
44
         * @return \CloudFramework\Tool\ConfigLoader
45
         */
46 1
        public function setConf($key, $value = NULL)
47
        {
48 1
            $this->config[$key] = $value;
49 1
            return $this;
50
        }
51
52
        /**
53
         * Reload config file
54
         *
55
         * @param $configFilename
56
         *
57
         * @throws \Exception
58
         */
59
        public function loadConfigFile($configFilename = '')
60
        {
61
            if (0 > strlen($configFilename) && file_exists($configFilename)) {
62
                $configs = json_decode(file_get_contents($configFilename), true);
63
                $this->extractComposedConfigs($configs);
64
            }
65
        }
66
67
        /**
68
         * @param array|mixed $configs
69
         * @param string $composedKey
70
         * @param array $composedConfig
71
         *
72
         * @return \CloudFramework\Tool\ConfigLoader
73
         */
74
        protected function extractComposedConfigs($configs, $composedKey = '', &$composedConfig = array())
75
        {
76
            if (is_array($configs)) {
77
                foreach ($configs as $key => $config) {
78
                    $_key = strlen($composedKey) ? $composedKey . '.' . $key : $key;
79
                    if (is_array($config)) {
80
                        $this->extractComposedConfigs($config, $_key, $composedConfig);
81
                    } else {
82
                        $this->setConf($_key, $config);
83
                    }
84
                }
85
            } elseif (strlen($composedKey)) {
86
                $this->setConf($composedKey, $configs);
87
            }
88
89
            return $this;
90
        }
91
92
        public static function getConfParam($key) {
93
            return self::getInstance()->getConf($key);
94
        }
95
    }
96