Config::get()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 5
nop 2
1
<?php
2
/**
3
 * Config class.
4
 *
5
 * @package SugiPHP.Config
6
 * @author  Plamen Popov <[email protected]>
7
 * @license http://opensource.org/licenses/mit-license.php (MIT License)
8
 */
9
10
namespace SugiPHP\Config;
11
12
class Config
13
{
14
    protected $registry = array();
15
    protected $loaders = array();
16
17
    /**
18
     * Creates a Config instance.
19
     *
20
     * @param array|LoaderInterface|null $loaders array of LoaderInterface
21
     */
22
    public function __construct($loaders = null)
23
    {
24
        if (!is_null($loaders)) {
25
            if (is_array($loaders)) {
26
                foreach ($loaders as $loader) {
27
                    $this->addLoader($loader);
28
                }
29
            } else {
30
                $this->addLoader($loaders);
31
            }
32
        }
33
    }
34
35
    public function addLoader(LoaderInterface $loader)
36
    {
37
        $this->loaders[] = $loader;
38
    }
39
40
    /**
41
     * Returns loaded config option. If the key is not found it checks if registered loaders can
42
     * find the key.
43
     *
44
     * @param string $key
45
     * @param mixed $default - the value to be returned if the $key is not found
46
     *
47
     * @return mixed
48
     */
49
    public function get($key, $default = null)
50
    {
51
        if (empty($key)) {
52
            return $default;
53
        }
54
55
        $parts = explode(".", $key);
56
        $file = array_shift($parts);
57
58
        if (!isset($this->registry[$file])) {
59
            $this->registry[$file] = $this->discover($file);
60
        }
61
        $res = $this->parse($key);
62
63
        return is_null($res) ? $default : $res;
64
    }
65
66
    /**
67
     * Registers a config variable
68
     *
69
     * @param string $key
70
     * @param mixed $value
71
     *
72
     * @return void
73
     */
74
    public function set($key, $value)
75
    {
76
        if (empty($key)) {
77
            throw new Exception("Key must be set");
78
        }
79
80
        $parts = array_reverse(explode(".", $key));
81
        $file = array_pop($parts);
82
83
        // we'll try to load a configuration file (if exists)
84
        if (!isset($this->registry[$file])) {
85
            $this->registry[$file] = $this->discover($file);
86
        }
87
88
        foreach ($parts as $part) {
89
            $value = array($part => $value);
90
        }
91
92
        if (is_array($this->registry[$file]) && (is_array($value))) {
93
            $this->registry[$file] = array_replace_recursive($this->registry[$file], $value);
94
        } else {
95
            $this->registry[$file] = $value;
96
        }
97
    }
98
99
    /**
100
     * Tries to find needed resource by looping each of registered loaders.
101
     *
102
     * @param string $resource
103
     *
104
     * @return array|null Returns null if resource is not found
105
     */
106
    protected function discover($resource)
107
    {
108
        foreach ($this->loaders as $loader) {
109
            $res = $loader->load($resource);
110
            if (!is_null($res)) {
111
                return $res;
112
            }
113
        }
114
    }
115
116
    /**
117
     * Search for a key with dot notation in the array. If the key is not found NULL is returned
118
     *
119
     * @param string $key
120
     *
121
     * @return mixed|null Returns NULL if the key is not found.
122
     */
123
    protected function parse($key)
124
    {
125
        $values = $this->registry;
126
        $parts = explode(".", $key);
127
        foreach ($parts as $part) {
128
            if ($part === "") {
129
                return $values;
130
            }
131
            if (!is_array($values) || !array_key_exists($part, $values)) {
132
                return ;
133
            }
134
            $values = $values[$part];
135
        }
136
137
        return $values;
138
    }
139
}
140