Completed
Push — master ( e61258...4cb8f2 )
by Arthur
9s
created

ConfigManager::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebThumbnailer\Application;
4
5
use WebThumbnailer\Utils\DataUtils;
6
use WebThumbnailer\Utils\FileUtils;
7
8
/**
9
 * Class ConfigManager
10
 *
11
 * Load configuration from JSON files.
12
 *
13
 * @package WebThumbnailer\Application
14
 */
15
class ConfigManager
16
{
17
    /**
18
     * @var string Flag telling a setting is not found.
19
     */
20
    public static $NOT_FOUND = 'NOT_FOUND';
21
22
    /**
23
     * @var array List of JSON configuration file path.
24
     */
25
    public static $configFiles = [
26
        FileUtils::RESOURCES_PATH . 'settings.json',
27
    ];
28
29
    /**
30
     * @var array Loaded config array.
31
     */
32
    protected static $loadedConfig;
33
34
    /**
35
     * Rebuild the loaded config array from config files.
36
     */
37
    public static function reload()
38
    {
39
        self::initialize();
40
    }
41
42
    /**
43
     * Initialize loaded conf in ConfigManager.
44
     */
45
    protected static function initialize()
46
    {
47
        self::$loadedConfig = [];
48
        foreach (self::$configFiles as $configFile) {
49
            self::$loadedConfig = array_replace_recursive(self::$loadedConfig, DataUtils::loadJson($configFile));
50
        }
51
    }
52
53
    /**
54
     * Add a configuration file.
55
     *
56
     * @param string $file path.
57
     */
58
    public static function addFile($file)
59
    {
60
        self::$configFiles[] = $file;
61
        self::initialize();
62
    }
63
64
    /**
65
     * Clear the current config
66
     */
67
    public static function clear()
68
    {
69
        self::$configFiles = [
70
            FileUtils::RESOURCES_PATH . 'settings.json',
71
        ];
72
        self::reload();
73
    }
74
75
    /**
76
     * Get a setting.
77
     *
78
     * Supports nested settings with dot separated keys.
79
     * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
80
     * or in JSON:
81
     *   { "config": { "stuff": {"option": "mysetting" } } } }
82
     *
83
     * @param string $setting Asked setting, keys separated with dots.
84
     * @param mixed  $default Default value if not found.
85
     *
86
     * @return mixed Found setting, or the default value.
87
     */
88
    public static function get($setting, $default = '')
89
    {
90
        if (empty(self::$loadedConfig)) {
91
            self::initialize();
92
        }
93
94
        $settings = explode('.', $setting);
95
        $value = self::getConfig($settings, self::$loadedConfig);
96
        if ($value == self::$NOT_FOUND) {
97
            return $default;
98
        }
99
        return $value;
100
    }
101
102
    /**
103
     * Recursive function which find asked setting in the loaded config.
104
     *
105
     * @param array $settings Ordered array which contains keys to find.
106
     * @param array $config   Loaded settings, then sub-array.
107
     *
108
     * @return mixed Found setting or NOT_FOUND flag.
109
     */
110
    protected static function getConfig($settings, $config)
111
    {
112
        if (! is_array($settings) || count($settings) == 0) {
0 ignored issues
show
introduced by
The condition is_array($settings) is always true.
Loading history...
113
            return self::$NOT_FOUND;
114
        }
115
116
        $setting = array_shift($settings);
117
        if (!isset($config[$setting])) {
118
            return self::$NOT_FOUND;
119
        }
120
121
        if (count($settings) > 0) {
122
            return self::getConfig($settings, $config[$setting]);
123
        }
124
        return $config[$setting];
125
    }
126
}
127