Completed
Push — master ( 59763f...201c15 )
by Fran
05:04 queued 15s
created

ConfigLoader::createDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace CloudFramework\Tool;
3
4
use CloudFramework\Exceptions\LoaderException;
5
use CloudFramework\Patterns\Singleton;
6
7
/**
8
 * Class ConfigLoader
9
 * @package CloudFramework\Core
10
 */
11
class ConfigLoader extends Singleton
12
{
13
    private $config = array();
14
15
    public function __construct($configFilename = '')
16
    {
17
        parent::__construct();
18
        $this->loadConfigFile($configFilename);
19
    }
20
21
    /**
22
     * Create directory
23
     * @param string $directory
24
     * @return bool
25
     */
26
    public static function createDir($directory)
27
    {
28
        return (false !== @mkdir($directory));
29
    }
30
31
32
33
    /**
34
     * Search config key and returns this value
35
     * @param string $key
36
     * @param array $config
37
     * @return mixed
38
     */
39
    public function getConfigParam($key, $config = array())
40
    {
41
        $config = (count($config) == 0) ? $this->config : $config;
42
        $value = null;
43
        if (count($config) > 0) {
44
            $configKey = explode(".", trim($key));
45
            $parentKey = reset($configKey);
46
            $existsKeyInConfig = array_key_exists($parentKey, $config);
47
            if (count($configKey) > 1 && $existsKeyInConfig) {
48
                $value = $this->getConfigParam($this->getChildConfigKey($key), $config[$parentKey]);
49
            } elseif ($existsKeyInConfig) {
50
                $value = $config[$parentKey];
51
            }
52
        }
53
        return $value;
54
    }
55
56
    /**
57
     * Extract child keys from parent config key
58
     * @param string $key
59
     * @return string
60
     */
61
    private function getChildConfigKey($key)
62
    {
63
        $childKey = '';
64
        $dotPosition = strpos($key, '.');
65
        if (false !== $dotPosition) {
66
            $childKey = substr($key, $dotPosition + 1, strlen($key) - $dotPosition);
67
        }
68
        return $childKey;
69
    }
70
71
    /**
72
     * Set a config parameter
73
     * @param string $key
74
     * @param mixed $value
75
     * @return \CloudFramework\ConfigLoader
76
     */
77
    public function setConfigParam($key, $value = null)
78
    {
79
        $this->config = $this->setConfigValue($key, $value, $this->config);
80
        return $this;
81
    }
82
83
    /**
84
     * Recursive process that set a config value
85
     * @param string $key
86
     * @param mixed $value
87
     * @param mixed $config
88
     * @return array
89
     */
90
    private function setConfigValue($key, $value = null, $config = null)
91
    {
92
        $keys = explode('.', $key);
93
        if (count($keys) === 0) {
94
            $keys = array($key);
95
        }
96
        $keyCount = count($keys);
97
        $aux = $config;
98
        foreach ($keys as $_key) {
99
            $keyCount--;
100
            if (!array_key_exists($_key, $aux)) {
101
                $aux[$_key] = array();
102
            }
103
            if ($keyCount > 0) {
104
                $aux[$_key]  = $this->setConfigValue($this->getChildConfigKey($key), $value, $aux[$_key]);
105
            } else {
106
                $aux[$_key] = $value;
107
            }
108
            break;
109
        }
110
        return $aux;
111
    }
112
113
    /**
114
     * Reload config file
115
     * @param $configFilename
116
     * @throws \Exception
117
     */
118
    public function loadConfigFile($configFilename = '')
119
    {
120
        if (strlen($configFilename) > 0 && file_exists($configFilename)) {
121
            $this->config = array();
122
        }
123
    }
124
}
125