1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
7
|
|
|
use Symfony\Component\Yaml\Yaml; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Config helper class |
11
|
|
|
* |
12
|
|
|
* @package Brendt\Stitcher |
13
|
|
|
*/ |
14
|
|
|
class Config |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param array $config |
19
|
|
|
* |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public static function parseImports(array $config) : array { |
23
|
|
|
if (!isset($config['imports'])) { |
24
|
|
|
return $config; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$mergedConfig = []; |
28
|
|
|
|
29
|
|
|
foreach ($config['imports'] as $import) { |
30
|
|
|
$importConfig = self::parseImports(Yaml::parse(self::getConfigFile($import)->getContents())); |
31
|
|
|
|
32
|
|
|
$mergedConfig = array_replace_recursive($mergedConfig, $importConfig); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$mergedConfig = array_replace_recursive($mergedConfig, $config); |
36
|
|
|
|
37
|
|
|
return $mergedConfig; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $path |
42
|
|
|
* |
43
|
|
|
* @return null|SplFileInfo |
44
|
|
|
*/ |
45
|
|
|
public static function getConfigFile(string $path) : ?SplFileInfo { |
46
|
|
|
$pathParts = explode('/', $path); |
47
|
|
|
$configFileName = array_pop($pathParts); |
48
|
|
|
$configPath = implode('/', $pathParts) . '/'; |
49
|
|
|
|
50
|
|
|
$configFiles = Finder::create()->files()->in($configPath)->name($configFileName)->depth(0)->getIterator(); |
51
|
|
|
$configFiles->rewind(); |
52
|
|
|
|
53
|
|
|
return $configFiles->current(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $config |
58
|
|
|
* @param string $prefix |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public static function flatten(array $config, string $prefix = '') : array { |
63
|
|
|
$result = []; |
64
|
|
|
|
65
|
|
|
foreach ($config as $key => $value) { |
66
|
|
|
$new_key = $prefix . (empty($prefix) ? '' : '.') . $key; |
67
|
|
|
|
68
|
|
|
if (is_array($value) && count($value)) { |
69
|
|
|
$result = array_merge($result, self::flatten($value, $new_key)); |
70
|
|
|
} else { |
71
|
|
|
$result[$new_key] = $value; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|