Completed
Push — master ( 34879e...d9f679 )
by Zlatin
01:46
created

Config::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace DevOp\Core;
3
4
class Config
5
{
6
7
    /**
8
     * @var mixed
9
     */
10
    private static $data = [];
11
12
    /**
13
     * @var mixed
14
     */
15
    private static $values = [];
16
17
    /**
18
     * @param string|array $resource
19
     * @param string $environment
20
     * @param array|string|null $params
21
     */
22 15
    public function __construct($resource, $environment = '', $params = [])
23
    {
24 15
        self::$data = self::load($resource, $environment, $params);
25 12
    }
26
27
    /**
28
     * @param string $name
29
     * @return boolean
30
     */
31 6
    public function has($name)
32
    {
33 6
        return isset(self::$data[$name]);
34
    }
35
36
    /**
37
     * @param string $name
38
     * @param mixed|null $default
39
     * @return mixed
40
     */
41 6
    public function get($name, $default = null)
42
    {
43 6
        if ($this->has($name)) {
44 6
            return self::$data[$name];
45
        }
46
47
        return $default;
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param mixed $args
53
     */
54 3
    public function set($name, $args)
55
    {
56 3
        self::$data[$name] = $args;
57
58 3
        return $this;
59
    }
60
    
61
    /**
62
     * 
63
     * @return mixed
64
     */
65 3
    public function all()
66
    {
67 3
        return self::$data;
68
    }
69
70
    /**
71
     * @param array|string $resource
72
     * @param string $environment
73
     * @param mixed $params
74
     * @return mixed
75
     * @throws \RuntimeException
76
     */
77 15
    public static function load($resource, $environment = '', $params = [])
78
    {
79
80 15
        if (is_string($resource) && !file_exists($resource)) {
81 3
            throw new \RuntimeException("Invalid configuration file <{$resource}>.");
82
        }
83
84 12
        if (is_array($params)) {
85 12
            $values = $params;
86 8
        } else if (file_exists($params)) {
87
            $values = self::load($params, $environment);
88
        } else if (!empty($params)) {
89
            throw new \RuntimeException("Invalid parameters file <{$params}>.");
90
        }
91
92 12
        if (!empty($values)) {
93 6
            foreach ($values AS $key => $value) {
94 6
                self::$values["{{$key}}"] = $value;
95 4
            }
96 4
        }
97
98 12
        if (is_array($resource)) {
99 12
            $config = array_replace_recursive(self::$data, $resource);
100 8
        } else {
101
            $config = array_replace_recursive(self::$data, include_once $resource);
102
        }
103
104 12
        if (!empty($environment) && is_string($resource) && false !== $pathinfo = pathinfo($resource)) {
105
            $filename = implode(DIRECTORY_SEPARATOR, [$pathinfo['dirname'], "{$pathinfo['filename']}_{$environment}.{$pathinfo['extension']}"]);
106
            if (file_exists($filename)) {
107
                $config = array_replace_recursive($config, include_once $filename);
108
            }
109
        }
110
111 12
        return self::transform($config);
112
    }
113
114
    /**
115
     * @param mixed $data
116
     * @param string $prefix
117
     * @return mixed
118
     */
119 12
    public static function transform($data, $prefix = '')
120
    {
121 12
        $values = [];
122 12
        foreach ($data AS $key => $value) {
123 12
            if (is_array($value)) {
124 12
                $values = array_merge($values, self::transform($value, "{$prefix}{$key}."));
125 8
            } else {
126 12
                $values["{$prefix}{$key}"] = isset(self::$values[$value]) ? self::$values[$value] : $value;
127
            }
128 8
        }
129 12
        return $values;
130
    }
131
}
132