-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.php
213 lines (167 loc) · 6.75 KB
/
Config.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Tale;
use Tale\Config\Format\Ini;
use Tale\Config\Format\Json;
use Tale\Config\Format\Php;
use Tale\Config\Format\Xml;
use Tale\Config\Format\Yaml;
use Tale\Config\FormatInterface;
use Tale\Factory\SingletonFactory;
final class Config
{
private static $formats = [
'ini' => Ini::class,
'json' => Json::class,
'php' => Php::class,
'xml' => Xml::class,
'yaml' => Yaml::class
];
private static $formatFactory = null;
public static function createFormatFactory(array $formats = null)
{
return new SingletonFactory(FormatInterface::class, $formats);
}
public static function getFormatFactory()
{
if (self::$formatFactory === null)
self::$formatFactory = self::createFormatFactory(
self::$formats
);
return self::$formatFactory;
}
public static function resolve($path)
{
$factory = self::getFormatFactory();
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (!empty($ext) && file_exists($path))
return $path;
/** @var FormatInterface[] $aliases */
$aliases = $factory->getAliases();
foreach ($aliases as $extension => $className) {
$extPath = "$path.$extension";
if (file_exists($extPath))
return $extPath;
}
return null;
}
public static function load($path, $optional = false)
{
$path = self::resolve($path);
$factory = self::getFormatFactory();
if (!$path) {
if ($optional)
return [];
throw new ConfigException(
"Failed to resolve configuration file $path: ".
"The file was not found, even with any of the following ".
"registered extensions: ".implode(', ', array_keys($factory->getAliases()))
);
}
return $factory->get(pathinfo($path, PATHINFO_EXTENSION))->load($path);
}
/**
* Resolves a key.subKey.subSubKey-style string to a deep array value
*
* The function accesses multi-dimensional keys with a delimiter given (Default: Dot (.))
*
* @protip If you want to throw an exception if no key is found, pass the exception as the default value
* and throw it, if the result is an Exception-type
*
* @param string $key The input key to operate on
* @param array $source The array to search values in
* @param mixed $defaultValue The default value if no key is found
* @param string|null $delimiter The delimeter to access dimensions (Default: Dot (.))
*
* @return array|null The found value or the default value, if none found (Default: null)
*/
public static function get($key, array $source, $defaultValue = null, $delimiter = null)
{
$delimiter = $delimiter ?: '.';
$current = &$source;
$keys = explode($delimiter, $key);
foreach ($keys as $key) {
if (is_numeric($key))
$key = intval($key);
if (!isset($current[$key]))
return $defaultValue;
$current = &$current[$key];
}
return $current;
}
/**
* Resolves a key.subKey.subSubKey-style string to a deep array value
*
* The function accesses multi-dimensional keys with a delimiter given (Default: Dot (.))
*
* @protip If you want to throw an exception if no key is found, pass the exception as the default value
* and throw it, if the result is an Exception-type
*
* @param string $key The input key to operate on
* @param mixed $value The default value if no key is found
* @param array $source The array to search values in
* @param string|null $delimiter The delimeter to access dimensions (Default: Dot (.))
*
* @return array|null The found value or the default value, if none found (Default: null)
*/
public static function set($key, $value, array &$source, $delimiter = null)
{
$delimiter = $delimiter ?: '.';
$current = &$source;
$keys = explode($delimiter, $key);
$lastKey = array_pop($keys);
foreach ($keys as $key) {
if (is_numeric($key))
$key = intval($key);
if (!isset($current[$key]))
$current[$key] = [];
$current = &$current[$key];
}
$current[$lastKey] = $value;
}
/**
* Interpolates {{var.subVar}}-style based on a source array given
*
* Dimensions in the source array are accessed with a passed delimeter (Default: Dot (.))
*
* @param string $string The input string to operate on
* @param array $source The associative source array
* @param mixed $defaultValue The default value for indices that dont exist
* @param string|null $delimiter The delimeter for multi-dimension access (Default: Dot (.))
*
* @return string The interpolated string with the variables replaced with their values
*/
public static function interpolate($string, array $source, $defaultValue = null, $delimiter = null)
{
$defaultValue = $defaultValue ?: '';
return preg_replace_callback('/\{\{([^\}]+)\}\}/i', function ($m) use ($source, $defaultValue, $delimiter) {
$key = $m[1];
if (defined($key))
return constant($key);
return self::get($key, $source, $defaultValue, $delimiter);
}, $string);
}
/**
* Interpolates a multi-dimensional array with another array recursively
*
* If no source is given, you get a live interpolation where you can directly interpolate
* variables that have just been interpolated before
*
* This is mostly used for option arrays, e.g. config-files
*
* @param array $array The array to interpolate (Passed by reference)
* @param array|null $source The source array for variables. If none given, the input array is taken
* @param null $defaultValue The default value for indices that couldnt be resolved
* @param string $delimiter The delimeter used for multi-dimension access (Default: Dot (.))
*/
public static function interpolateArray(array &$array, array &$source = null, $defaultValue = null, $delimiter = null)
{
if (!$source)
$source = &$array;
foreach ($array as $key => &$val) {
if (is_array($val))
self::interpolateArray($val, $source, $defaultValue, $delimiter);
else if (is_string($val))
$array[$key] = self::interpolate($val, $source, $defaultValue, $delimiter);
}
}
}