Config::create()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 8
nc 5
nop 1
dl 0
loc 11
rs 10
c 1
b 1
f 0
1
<?php
2
/**
3
 * Kore : Simple And Minimal Framework
4
 *
5
 */
6
7
namespace Kore;
8
9
/**
10
 * Config class
11
 *
12
 * Managing the configurations
13
 */
14
class Config
15
{
16
    /**
17
     * configurations
18
     *
19
     * An array of configuration values loaded from the configuration file.
20
     * @var array<mixed>
21
     */
22
    private static $config;
23
24
    /**
25
     * Creating the configurations
26
     *
27
     * Load the configurations from CONFIG_DIR/config-config-$env.php.
28
     * If the environment is not specified, load the configurations from CONFIG_DIR/config.php.
29
     * Environment-independent configurations are defined in CONFIG_DIR/config-common.php.
30
     * @param string|null $env environment
31
     * @return void
32
     * @throws \Exception thrown when the configuration file does not exist
33
     */
34
    public static function create($env = null)
35
    {
36
        if (empty(self::$config)) {
37
            $env = empty($env) ? '' : '-'.$env;
38
            $configfile = CONFIG_DIR.'/config'.$env.'.php';
39
            if (!file_exists($configfile)) {
40
                throw new \Exception('Unable to find config file -> ' . $configfile);
41
            }
42
            self::$config = require $configfile;
43
            $commonConfig = require CONFIG_DIR.'/config-common.php';
44
            self::$config = array_merge(self::$config, $commonConfig);
45
        }
46
    }
47
48
    /**
49
     * Get the configurations
50
     *
51
     * If no key is specified, all configurations are returned.
52
     * If the key is passed dot-delimited, it is interpreted as an array path.
53
     * @param string|null $key configuration key
54
     * @param mixed $default default value if there is no value specified in the key
55
     * @return mixed configurations
56
     */
57
    public static function get($key = null, $default = null)
58
    {
59
        if ($key === null) {
60
            return self::$config;
61
        }
62
        return array_get_recursive(self::$config, $key, $default);
63
    }
64
}
65