13
13
use Symfony \Component \Yaml \Yaml ;
14
14
15
15
/**
16
- * Class ConfigAbstract
16
+ * Class AbstractConfig
17
17
*
18
18
* @package Asm\Config
19
19
* @author marc aschmann <[email protected] >
20
20
* @codeCoverageIgnore
21
21
* @uses Asm\Data\Data
22
22
* @uses Symfony\Component\Yaml\Yaml
23
23
*/
24
- abstract class ConfigAbstract extends Data
24
+ abstract class AbstractConfig extends Data
25
25
{
26
26
/**
27
27
* @var bool
28
28
*/
29
29
protected $ filecheck = true ;
30
30
31
31
/**
32
- * @var null
32
+ * @var array
33
33
*/
34
- protected $ imports ;
34
+ protected $ imports = [] ;
35
35
36
36
/**
37
- * @var null
37
+ * @var array
38
38
*/
39
- protected $ default ;
39
+ protected $ default = [] ;
40
40
41
41
/**
42
42
* Default constructor.
@@ -73,8 +73,9 @@ public function addConfig($name, $file)
73
73
public function readConfig ($ file )
74
74
{
75
75
$ config = $ this ->readFile ($ file );
76
- $ config = $ this ->extractDefault ($ config );
77
76
$ config = $ this ->extractImports ($ config );
77
+ $ config = $ this ->extractDefault ($ config );
78
+ $ this ->mergeDefault ();
78
79
79
80
return $ config ;
80
81
}
@@ -86,7 +87,12 @@ public function readConfig($file)
86
87
*/
87
88
public function setConfig ($ file )
88
89
{
89
- $ this ->setByArray ($ this ->readConfig ($ file ));
90
+ $ this ->setByArray (
91
+ array_replace_recursive (
92
+ $ this ->default ,
93
+ $ this ->readConfig ($ file )
94
+ )
95
+ );
90
96
}
91
97
92
98
/**
@@ -107,19 +113,23 @@ private function readFile($file)
107
113
}
108
114
109
115
/**
116
+ * get all import files from config, if set and remove node.
117
+ *
110
118
* @param array $config
111
119
* @return array
112
120
*/
113
- protected function extractImports (array $ config )
121
+ private function extractImports (array $ config )
114
122
{
115
123
if (array_key_exists ('imports ' , $ config ) && 0 < count ($ config ['imports ' ])) {
116
124
$ this ->imports = [];
117
- foreach ($ config ['imports ' ] as $ import ) {
118
- if (false ! == empty ($ import ['resource ' ])) {
119
- array_replace_recursive (
125
+ foreach ($ config ['imports ' ] as $ key => $ import ) {
126
+ if (false = == empty ($ import ['resource ' ])) {
127
+ $ this -> imports = array_replace_recursive (
120
128
$ this ->imports ,
121
129
$ this ->readFile ($ import ['resource ' ])
122
130
);
131
+
132
+ unset($ config ['resource ' ][$ key ]);
123
133
}
124
134
}
125
135
}
@@ -128,11 +138,26 @@ protected function extractImports(array $config)
128
138
}
129
139
130
140
/**
141
+ * Get default values if set and remove node from config.
142
+ *
131
143
* @param array $config
132
144
* @return array
133
145
*/
134
- protected function extractDefault ($ config )
146
+ private function extractDefault ($ config )
135
147
{
148
+ if (array_key_exists ('default ' , $ config )) {
149
+ $ this ->default = $ config ['default ' ];
150
+ unset($ config ['default ' ]);
151
+ }
152
+
136
153
return $ config ;
137
154
}
155
+
156
+ /**
157
+ * Prepare the defaults and replace recursively.
158
+ */
159
+ private function mergeDefault ()
160
+ {
161
+ $ this ->default = array_replace_recursive ($ this ->imports , $ this ->default );
162
+ }
138
163
}
0 commit comments