Passed
Push — v5 ( 6adbf6...c0775f )
by Alexey
06:03
created

Config::system()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
crap 4.074
1
<?php
2
/**
3
 * Config
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2015-2018 Alexey Krupskiy
8
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
9
 */
10
11
namespace Inji;
12
13
/**
14
 * Class Config
15
 * @package Inji
16
 */
17
class Config {
18
19
    /**
20
     * Static config storage
21
     *
22
     * @var array
23
     */
24
    private static $_configs = [];
25
26
    /**
27
     * Load system config
28
     *
29
     * @param bool $forceLoad
30
     * @return array
31
     */
32 1
    public static function system($forceLoad = false) {
33 1
        if (!$forceLoad && isset(self::$_configs['system'])) {
34 1
            return self::$_configs['system'];
35
        }
36 1
        if (!file_exists(INJI_SYSTEM_DIR . '/config/config.php')) {
37
            return [];
38
        }
39 1
        return self::$_configs['system'] = include INJI_SYSTEM_DIR . '/config/config.php';
40
    }
41
42
    /**
43
     * Load custom config
44
     *
45
     * @param string $path
46
     * @param bool $forceLoad
47
     * @return array
48
     */
49 9
    public static function custom($path, $forceLoad = false) {
50 9
        if (!$forceLoad && isset(self::$_configs['custom'][$path])) {
51 5
            return self::$_configs['custom'][$path];
52
        }
53
54 6
        if (!file_exists($path)) {
55 6
            return [];
56
        }
57
58 1
        return self::$_configs['custom'][$path] = include $path;
59
    }
60
61
    /**
62
     * Load app config
63
     *
64
     * @param App $app
65
     * @param bool $forceLoad
66
     * @return array
67
     */
68 1
    public static function app($app = null, $forceLoad = false) {
69 1
        if (!$app) {
70 1
            $app = App::$primary;
71
        }
72 1
        if (!$forceLoad && isset(self::$_configs['app'][$app->name])) {
73 1
            return self::$_configs['app'][$app->name];
74
        }
75
76 1
        $path = $app->path . "/config/config.php";
77 1
        if (!file_exists($path)) {
78 1
            return [];
79
        }
80
81 1
        return self::$_configs['app'][$app->name] = include $path;
82
    }
83
84
    /**
85
     * Load share config
86
     *
87
     * @param string $module
88
     * @param bool $forceLoad
89
     * @return array
90
     */
91 1
    public static function share($module = '', $forceLoad = false) {
92 1
        if ($module) {
93 1
            if (!$forceLoad && isset(self::$_configs['shareModules'][$module])) {
94 1
                return self::$_configs['shareModules'][$module];
95
            }
96 1
            $path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php";
97
        } else {
98 1
            if (!$forceLoad && isset(self::$_configs['share'])) {
99 1
                return self::$_configs['share'];
100
            }
101 1
            $path = INJI_PROGRAM_DIR . "/config/config.php";
102
        }
103 1
        if (!file_exists($path)) {
104 1
            return [];
105
        }
106
107 1
        if ($module) {
108 1
            return self::$_configs['shareModules'][$module] = include $path;
109
        } else {
110 1
            return self::$_configs['share'] = include $path;
111
        }
112
    }
113
114
    /**
115
     * Load module config
116
     *
117
     * @param string $module_name
118
     * @param App $app
119
     * @param bool $forceLoad
120
     * @return array
121
     */
122 2
    public static function module($module_name, $app = null, $forceLoad = false) {
123
124 2
        if (!$app) {
125 2
            $app = App::$primary;
126
        }
127
128 2
        if (!$forceLoad && isset(self::$_configs['module'][$app->name][$module_name])) {
129 1
            return self::$_configs['module'][$app->name][$module_name];
130
        }
131
132 2
        $path = $app->path . "/config/modules/{$module_name}.php";
133 2
        if (!file_exists($path)) {
134 2
            $path = INJI_SYSTEM_DIR . "/modules/{$module_name}/defaultConfig.php";
135
        }
136
137 2
        if (!file_exists($path)) {
138 2
            return [];
139
        }
140 1
        return self::$_configs['module'][$app->name][$module_name] = include $path;
141
    }
142
143
    /**
144
     * Save config
145
     *
146
     * @param string $type
147
     * @param array $data
148
     * @param string $module
149
     * @param App $app
150
     */
151 9
    public static function save($type, $data, $module = '', $app = null) {
152 9
        if (!$app) {
153 9
            $app = App::$primary;
154
        }
155 9
        switch ($type) {
156
            case 'system':
157 1
                $path = INJI_SYSTEM_DIR . '/config/config.php';
158 1
                self::$_configs['system'] = $data;
159 1
                \Inji::$inst->event('Config-change-system', $data);
160 1
                break;
161
            case 'app':
162 1
                $path = $app->path . "/config/config.php";
163 1
                self::$_configs['app'][$app->name] = $data;
164 1
                \Inji::$inst->event('Config-change-app-' . $app->name, $data);
165 1
                break;
166
            case 'module':
167 1
                $path = $app->path . "/config/modules/{$module}.php";
168 1
                self::$_configs['module'][$app->name][$module] = $data;
169 1
                \Inji::$inst->event('Config-change-module-' . $app->name . '-' . $module, $data);
170 1
                break;
171
            case 'share':
172 1
                if ($module) {
173 1
                    $path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php";
174 1
                    self::$_configs['shareModules'][$module] = $data;
175 1
                    \Inji::$inst->event('Config-change-shareModules-' . $module, $data);
176
                } else {
177 1
                    $path = INJI_PROGRAM_DIR . "/config/config.php";
178 1
                    self::$_configs['share'] = $data;
179 1
                    \Inji::$inst->event('Config-change-share', $data);
180
                }
181 1
                break;
182
            default:
183 5
                $path = $type;
184 5
                self::$_configs['custom'][$path] = $data;
185 5
                break;
186
        }
187 9
        $text = "<?php\nreturn " . CodeGenerator::genArray($data);
188 9
        Tools::createDir(substr($path, 0, strripos($path, '/')));
189 9
        file_put_contents($path, $text);
190 9
    }
191
192
}
193