1
|
|
|
<?php |
2
|
|
|
namespace ConfigTree\Builder; |
3
|
|
|
|
4
|
|
|
use ConfigTree\Tree\ConfigTree; |
5
|
|
|
use ConfigTree\FileParsing\ArrayableFileFactory; |
6
|
|
|
use ConfigTree\Exception\FileFormatNotParsable; |
7
|
|
|
use ConfigTree\Exception\FileNotReadable; |
8
|
|
|
|
9
|
|
|
class ConfigTreeBuilder |
10
|
|
|
{ |
11
|
|
|
/** @var ArrayableFileFactory */ |
12
|
|
|
private $arrayableFileFactory; |
13
|
|
|
|
14
|
|
|
/** @var ConfigTree */ |
15
|
|
|
private $configTreeToReturn; |
16
|
|
|
|
17
|
|
|
public function __construct(ArrayableFileFactory $arrayableFileFactory = null) |
18
|
|
|
{ |
19
|
|
|
$this->arrayableFileFactory = $arrayableFileFactory ?: new ArrayableFileFactory(); |
20
|
|
|
$this->configTreeToReturn = new ConfigTree([]); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws FileFormatNotParsable |
25
|
|
|
* @throws FileNotReadable |
26
|
|
|
* |
27
|
|
|
* @param string[] $pathsToFiles |
28
|
|
|
*/ |
29
|
|
|
public function addSettingsFromPaths(array $pathsToFiles) |
30
|
|
|
{ |
31
|
|
|
foreach ($pathsToFiles as $pathToFile) { |
32
|
|
|
$this->addSettingsFromPath($pathToFile); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws FileFormatNotParsable |
38
|
|
|
* @throws FileNotReadable |
39
|
|
|
*/ |
40
|
|
|
private function addSettingsFromPath(string $pathToFile) |
41
|
|
|
{ |
42
|
|
|
$arrayableFile = $this->arrayableFileFactory->getArrayableFileFromPath($pathToFile); |
43
|
|
|
$fileContentsAsArray = $arrayableFile->toArray(); |
44
|
|
|
$config = new ConfigTree($fileContentsAsArray); |
45
|
|
|
$this->configTreeToReturn = $this->configTreeToReturn->withAnotherConfigTreeMergedIn($config); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function buildConfigTreeAndReset() : ConfigTree |
49
|
|
|
{ |
50
|
|
|
$configToReturn = $this->configTreeToReturn; |
51
|
|
|
$this->configTreeToReturn = new ConfigTree([]); |
52
|
|
|
return $configToReturn; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|