Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

Config::share()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 8
nop 2
dl 0
loc 20
rs 7.7777
c 0
b 0
f 0
ccs 13
cts 13
cp 1
crap 8
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
     * @param bool|string $systemPath
31
     * @return array
32
     */
33 1
    public static function system($forceLoad = false, $systemPath = false) {
34 1
        if (!$forceLoad && isset(self::$_configs['system'])) {
35 1
            return self::$_configs['system'];
36
        }
37 1
        if ($systemPath === false) {
38 1
            $systemPath = INJI_SYSTEM_DIR;
39
        }
40 1
        if (!file_exists($systemPath . '/config/config.php')) {
0 ignored issues
show
Bug introduced by
Are you sure $systemPath of type mixed|string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        if (!file_exists(/** @scrutinizer ignore-type */ $systemPath . '/config/config.php')) {
Loading history...
41 1
            return [];
42
        }
43 1
        return self::$_configs['system'] = include $systemPath . '/config/config.php';
44
    }
45
46
    /**
47
     * Load custom config
48
     *
49
     * @param string $path
50
     * @param bool $forceLoad
51
     * @return array
52
     */
53 9
    public static function custom($path, $forceLoad = false) {
54 9
        if (!$forceLoad && isset(self::$_configs['custom'][$path])) {
55 5
            return self::$_configs['custom'][$path];
56
        }
57
58 6
        if (!file_exists($path)) {
59 6
            return [];
60
        }
61
62 1
        return self::$_configs['custom'][$path] = include $path;
63
    }
64
65
    /**
66
     * Load app config
67
     *
68
     * @param App $app
69
     * @param bool $forceLoad
70
     * @return array
71
     */
72 1
    public static function app($app = null, $forceLoad = false) {
73 1
        if (!$app) {
74 1
            $app = App::$primary;
75
        }
76 1
        if (!$forceLoad && isset(self::$_configs['app'][$app->name])) {
77 1
            return self::$_configs['app'][$app->name];
78
        }
79
80 1
        $path = $app->path . "/config/config.php";
81 1
        if (!file_exists($path)) {
82 1
            return [];
83
        }
84
85 1
        return self::$_configs['app'][$app->name] = include $path;
86
    }
87
88
    /**
89
     * Load share config
90
     *
91
     * @param string $module
92
     * @param bool $forceLoad
93
     * @return array
94
     */
95 1
    public static function share($module = '', $forceLoad = false) {
96 1
        if ($module) {
97 1
            if (!$forceLoad && isset(self::$_configs['shareModules'][$module])) {
98 1
                return self::$_configs['shareModules'][$module];
99
            }
100 1
            $path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php";
101
        } else {
102 1
            if (!$forceLoad && isset(self::$_configs['share'])) {
103 1
                return self::$_configs['share'];
104
            }
105 1
            $path = INJI_PROGRAM_DIR . "/config/config.php";
106
        }
107 1
        if (!file_exists($path)) {
108 1
            return [];
109
        }
110
111 1
        if ($module) {
112 1
            return self::$_configs['shareModules'][$module] = include $path;
113
        } else {
114 1
            return self::$_configs['share'] = include $path;
115
        }
116
    }
117
118
    /**
119
     * Load module config
120
     *
121
     * @param string $module_name
122
     * @param App $app
123
     * @param bool $forceLoad
124
     * @return array
125
     */
126 2
    public static function module($module_name, $app = null, $forceLoad = false) {
127
128 2
        if (!$app) {
129 2
            $app = App::$primary;
130
        }
131
132 2
        if (!$forceLoad && isset(self::$_configs['module'][$app->name][$module_name])) {
133 1
            return self::$_configs['module'][$app->name][$module_name];
134
        }
135
136 2
        $path = $app->path . "/config/modules/{$module_name}.php";
137 2
        if (!file_exists($path)) {
138 2
            $path = INJI_SYSTEM_DIR . "/modules/{$module_name}/defaultConfig.php";
139
        }
140
141 2
        if (!file_exists($path)) {
142 2
            return [];
143
        }
144 1
        return self::$_configs['module'][$app->name][$module_name] = include $path;
145
    }
146
147
    /**
148
     * Save config
149
     *
150
     * @param string $type
151
     * @param array $data
152
     * @param string $module
153
     * @param App $app
154
     */
155 9
    public static function save($type, $data, $module = '', $app = null) {
156 9
        if (!$app) {
157 9
            $app = App::$primary;
158
        }
159 9
        switch ($type) {
160
            case 'system':
161 1
                $path = INJI_SYSTEM_DIR . '/config/config.php';
162 1
                self::$_configs['system'] = $data;
163 1
                \Inji::$inst->event('Config-change-system', $data);
164 1
                break;
165
            case 'app':
166 1
                $path = $app->path . "/config/config.php";
167 1
                self::$_configs['app'][$app->name] = $data;
168 1
                \Inji::$inst->event('Config-change-app-' . $app->name, $data);
169 1
                break;
170
            case 'module':
171 1
                $path = $app->path . "/config/modules/{$module}.php";
172 1
                self::$_configs['module'][$app->name][$module] = $data;
173 1
                \Inji::$inst->event('Config-change-module-' . $app->name . '-' . $module, $data);
174 1
                break;
175
            case 'share':
176 1
                if ($module) {
177 1
                    $path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php";
178 1
                    self::$_configs['shareModules'][$module] = $data;
179 1
                    \Inji::$inst->event('Config-change-shareModules-' . $module, $data);
180
                } else {
181 1
                    $path = INJI_PROGRAM_DIR . "/config/config.php";
182 1
                    self::$_configs['share'] = $data;
183 1
                    \Inji::$inst->event('Config-change-share', $data);
184
                }
185 1
                break;
186
            default:
187 5
                $path = $type;
188 5
                self::$_configs['custom'][$path] = $data;
189 5
                break;
190
        }
191 9
        $text = "<?php\nreturn " . CodeGenerator::genArray($data);
192 9
        Tools::createDir(substr($path, 0, strripos($path, '/')));
193 9
        file_put_contents($path, $text);
194 9
    }
195
196
}
197