1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/interop-config for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017-2020 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Interop\Config\Tool; |
11
|
|
|
|
12
|
|
|
class AbstractConfig |
13
|
|
|
{ |
14
|
1 |
|
protected function prepareConfig(iterable $config, int $indentLevel = 1): string |
15
|
|
|
{ |
16
|
1 |
|
$indent = str_repeat(' ', $indentLevel * 4); |
17
|
1 |
|
$entries = []; |
18
|
|
|
|
19
|
1 |
|
foreach ($config as $key => $value) { |
20
|
1 |
|
$key = $this->createConfigKey($key); |
21
|
1 |
|
$entries[] = sprintf( |
22
|
1 |
|
'%s%s%s,', |
23
|
|
|
$indent, |
24
|
1 |
|
$key ? sprintf('%s => ', $key) : '', |
25
|
1 |
|
$this->createConfigValue($value, $indentLevel) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
$outerIndent = str_repeat(' ', ($indentLevel - 1) * 4); |
30
|
|
|
|
31
|
1 |
|
return sprintf("[\n%s\n%s]", implode("\n", $entries), $outerIndent); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
private function createConfigKey($key): string |
35
|
|
|
{ |
36
|
1 |
|
return sprintf("'%s'", $key); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
private function createConfigValue($value, int $indentLevel): string |
40
|
|
|
{ |
41
|
1 |
|
if (is_array($value) || $value instanceof \Traversable) { |
42
|
1 |
|
return $this->prepareConfig($value, $indentLevel + 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if (is_string($value) && class_exists($value)) { |
46
|
1 |
|
return sprintf('\\%s::class', ltrim($value, '\\')); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return var_export($value, true); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|