1313use Symfony \Component \Yaml \Yaml ;
1414
1515/**
16- * Class ConfigAbstract
16+ * Class AbstractConfig
1717 *
1818 * @package Asm\Config
1919 * @author marc aschmann <maschmann@gmail.com>
2020 * @codeCoverageIgnore
2121 * @uses Asm\Data\Data
2222 * @uses Symfony\Component\Yaml\Yaml
2323 */
24- abstract class ConfigAbstract extends Data
24+ abstract class AbstractConfig extends Data
2525{
2626 /**
2727 * @var bool
2828 */
2929 protected $ filecheck = true ;
3030
3131 /**
32- * @var null
32+ * @var array
3333 */
34- protected $ imports ;
34+ protected $ imports = [] ;
3535
3636 /**
37- * @var null
37+ * @var array
3838 */
39- protected $ default ;
39+ protected $ default = [] ;
4040
4141 /**
4242 * Default constructor.
@@ -73,8 +73,9 @@ public function addConfig($name, $file)
7373 public function readConfig ($ file )
7474 {
7575 $ config = $ this ->readFile ($ file );
76- $ config = $ this ->extractDefault ($ config );
7776 $ config = $ this ->extractImports ($ config );
77+ $ config = $ this ->extractDefault ($ config );
78+ $ this ->mergeDefault ();
7879
7980 return $ config ;
8081 }
@@ -86,7 +87,12 @@ public function readConfig($file)
8687 */
8788 public function setConfig ($ file )
8889 {
89- $ this ->setByArray ($ this ->readConfig ($ file ));
90+ $ this ->setByArray (
91+ array_replace_recursive (
92+ $ this ->default ,
93+ $ this ->readConfig ($ file )
94+ )
95+ );
9096 }
9197
9298 /**
@@ -107,19 +113,23 @@ private function readFile($file)
107113 }
108114
109115 /**
116+ * get all import files from config, if set and remove node.
117+ *
110118 * @param array $config
111119 * @return array
112120 */
113- protected function extractImports (array $ config )
121+ private function extractImports (array $ config )
114122 {
115123 if (array_key_exists ('imports ' , $ config ) && 0 < count ($ config ['imports ' ])) {
116124 $ 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 (
120128 $ this ->imports ,
121129 $ this ->readFile ($ import ['resource ' ])
122130 );
131+
132+ unset($ config ['resource ' ][$ key ]);
123133 }
124134 }
125135 }
@@ -128,11 +138,26 @@ protected function extractImports(array $config)
128138 }
129139
130140 /**
141+ * Get default values if set and remove node from config.
142+ *
131143 * @param array $config
132144 * @return array
133145 */
134- protected function extractDefault ($ config )
146+ private function extractDefault ($ config )
135147 {
148+ if (array_key_exists ('default ' , $ config )) {
149+ $ this ->default = $ config ['default ' ];
150+ unset($ config ['default ' ]);
151+ }
152+
136153 return $ config ;
137154 }
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+ }
138163}
0 commit comments