Completed
Push — master ( 6a2a76...b9eb7f )
by Arthur
11s
created

ConfigManager::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
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
     * Get a setting.
66
     *
67
     * Supports nested settings with dot separated keys.
68
     * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
69
     * or in JSON:
70
     *   { "config": { "stuff": {"option": "mysetting" } } } }
71
     *
72
     * @param string $setting Asked setting, keys separated with dots.
73
     * @param mixed  $default Default value if not found.
74
     *
75
     * @return mixed Found setting, or the default value.
76
     */
77
    public static function get($setting, $default = '')
78
    {
79
        if (empty(self::$loadedConfig)) {
80
            self::initialize();
81
        }
82
83
        $settings = explode('.', $setting);
84
        $value = self::getConfig($settings, self::$loadedConfig);
85
        if ($value == self::$NOT_FOUND) {
86
            return $default;
87
        }
88
        return $value;
89
    }
90
91
    /**
92
     * Recursive function which find asked setting in the loaded config.
93
     *
94
     * @param array $settings Ordered array which contains keys to find.
95
     * @param array $config   Loaded settings, then sub-array.
96
     *
97
     * @return mixed Found setting or NOT_FOUND flag.
98
     */
99
    protected static function getConfig($settings, $config)
100
    {
101
        if (! is_array($settings) || count($settings) == 0) {
0 ignored issues
show
introduced by
The condition is_array($settings) is always true.
Loading history...
102
            return self::$NOT_FOUND;
103
        }
104
105
        $setting = array_shift($settings);
106
        if (!isset($config[$setting])) {
107
            return self::$NOT_FOUND;
108
        }
109
110
        if (count($settings) > 0) {
111
            return self::getConfig($settings, $config[$setting]);
112
        }
113
        return $config[$setting];
114
    }
115
}
116