-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_merge.php
49 lines (45 loc) · 1.53 KB
/
config_merge.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
<?php
/**
* ConfigMerge
*
* @link https://github.com/kanellov/config-merge
* @copyright Copyright (c) 2015 Vassilis Kanellopoulos - [email protected]
* @license https://raw.githubusercontent.com/kanellov/config-merge/master/LICENSE
*/
namespace Knlv;
/**
* Merges configuration arrays.
*
* Merges files in the existing path that match given suffix. Files should
* return array. Last suffix in array has greater priority than previous.
*
* @param string $path the path containing the configuration files
* @param array $suffixes array with file name suffixes
* @param array $init initial configuration
* @return array the merged configuration array
*/
function config_merge($path, array $suffixes, array $init = array())
{
$merge = function (array $merged, array $new) use (&$merge) {
foreach ($new as $key => $value) {
if (array_key_exists($key, $merged)) {
if (is_int($key)) {
$merged[] = $value;
} elseif (is_array($value) && is_array($merged[$key])) {
$merged[$key] = $merge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
} else {
$merged[$key] = $value;
}
}
return $merged;
};
return array_reduce(glob(
realpath($path) . '/{,*.}{' . implode(',', $suffixes) . '}.php',
GLOB_BRACE
), function ($config, $file) use (&$merge) {
return $merge($config, include $file);
}, $init);
}