|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Venta\Contracts\Config\Config; |
|
|
|
|
|
|
6
|
|
|
use Venta\Contracts\Config\ConfigBuilder as ConfigBuilderContract; |
|
7
|
|
|
use Venta\Contracts\Config\ConfigFactory as ConfigFactoryContract; |
|
8
|
|
|
use Venta\Contracts\Config\ConfigFileParser; |
|
9
|
|
|
use Venta\Contracts\Config\ConfigParser; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ConfigBuilder |
|
13
|
|
|
* |
|
14
|
|
|
* @package Venta\Config |
|
15
|
|
|
*/ |
|
16
|
|
|
class ConfigBuilder implements ConfigBuilderContract |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ConfigFactory |
|
20
|
|
|
*/ |
|
21
|
|
|
private $configFactory; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Items holder array. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $items = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ConfigFileParser[]|ConfigParser[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $parsers = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Construct function. |
|
37
|
|
|
* |
|
38
|
|
|
* @param null|ConfigFactoryContract $configFactory |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ConfigFactoryContract $configFactory = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->configFactory = $configFactory ?? new ConfigFactory(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addFileParser(ConfigFileParser $parser) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->parsers[] = $parser; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function build(): Config |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->configFactory->create($this->items); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Merges configuration data. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $config |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public function merge(array $config) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->items = array_merge_recursive($this->items, $config); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Merges configuration data form file. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $filename |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function mergeFile(string $filename) |
|
79
|
|
|
{ |
|
80
|
|
|
// TODO: extension of file can be empty. Exception in that case, or just ignore it? |
|
81
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($this->parsers as $parser) { |
|
84
|
|
|
if (($parser instanceof ConfigFileParser) && in_array($extension, $parser->supportedExtensions())) { |
|
85
|
|
|
$this->merge($parser->fromFile($filename)); |
|
86
|
|
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Appends a value to a config array. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @param mixed $value |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function push(string $path, $value) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
$keys = explode('.', $path); |
|
101
|
|
|
$array = &$this->items; |
|
102
|
|
|
|
|
103
|
|
|
while (count($keys) > 0) { |
|
104
|
|
|
$activeKey = array_shift($keys); |
|
105
|
|
|
|
|
106
|
|
|
if (!isset($array[$activeKey]) || !is_array($array[$activeKey])) { |
|
107
|
|
|
$array[$activeKey] = [$array[$activeKey]]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$array = &$array[$activeKey]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
array_push($array, $value); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets value to the configuration data. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param $value |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function set(string $path, $value) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$keys = explode('.', $path); |
|
126
|
|
|
$array = &$this->items; |
|
127
|
|
|
|
|
128
|
|
|
while (count($keys) > 1) { |
|
129
|
|
|
$activeKey = array_shift($keys); |
|
130
|
|
|
|
|
131
|
|
|
if (!isset($array[$activeKey]) || !is_array($array[$activeKey])) { |
|
132
|
|
|
$array[$activeKey] = []; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$array = &$array[$activeKey]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$array[array_shift($keys)] = $value; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: