-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
153 lines (128 loc) · 3.15 KB
/
Config.php
File metadata and controls
153 lines (128 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Hackphp\Config;
use Hackphp\Config\Contracts\Parser;
class Config
{
/**
* The parsed config.
*
* @var array
*/
protected array $config;
/**
* The parsers classes.
*
* @var Parser[]
*/
protected array $parsers = [];
/**
* Create new Config.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
}
/**
* Add Config Parser.
*
* @param Parser $parser
* @return $this
*/
public function addParser(Parser $parser)
{
if (!in_array($parser, $this->parsers)) {
$this->parsers[] = $parser;
}
return $this;
}
/**
* Load the config loader.
*
* @return $this
*/
public function load()
{
foreach ($this->parsers as $parser) {
$this->config = array_merge($this->config, $parser->parse());
}
return $this;
}
/**
* Get the given config value.
*
* @param string $key
* @param mixed|null $default
* @return mixed|null
*/
public function get($key, $default = null)
{
$filtered = $this->config;
if (isset($filtered[$key])) {
return $filtered[$key];
}
$this->catchSearchFile($filtered, $key);
return $this->getFilteredValue($filtered, $key, $default);
}
/**
* Catch the file that has the key we search about it.
*
* @param array $filtered
* @param string $key
* @return array
*/
private function catchSearchFile(array &$filtered, $key)
{
return array_reduce(explode(".", $key), function ($carry, $segment) use (&$filtered) {
if ($carry != null) {
$key = $carry . "." . $segment;
} else {
$key = $segment;
}
$values = $this->filterStartsWith($filtered, $key);
if ($values) {
$filtered = $values;
}
return $key;
});
}
/**
* Get the array items that it's key starts with the given name.
*
* @param array $haystack
* @param string $name
* @return array
*/
private function filterStartsWith(array $haystack, string $name)
{
$filtered = [];
foreach ($haystack as $key => $value) {
if (strpos($key, $name) === 0) {
$filtered[$key] = $value;
}
}
return $filtered;
}
/**
* Get the given key value from the filtered configs.
*
* @param array $filtered
* @param string $key
* @param mixed $default
* @return mixed
*/
private function getFilteredValue($filtered, $key, $default)
{
if (empty($filtered)) {
return $default;
}
$file = array_keys($filtered)[0];
$filtered = array_values($filtered)[0];
$key = str_replace($file . ".", "", $key);
foreach (explode(".", $key) as $segment) {
$filtered = $filtered[$segment] ?? $default;
}
return $filtered ?? $default;
}
}