@@ -42,34 +42,42 @@ public function read($configAlias)
4242 $ masterFilename = isset ($ files [0 ]) ? $ files [0 ] : null ;
4343 $ localFilename = isset ($ files [1 ]) ? $ files [1 ] : null ;
4444
45- if (!is_readable ($ masterFilename ) || !is_file ($ masterFilename )) {
46- throw new RuntimeException ($ this ->translate (
47- 'Master configuration file "{file}" not found. ' ,
48- [
49- 'file ' => $ masterFilename
50- ]
51- ));
52- }
45+ $ masterConfigExists = file_exists ($ masterFilename );
5346
54- /** @noinspection PhpIncludeInspection */
55- $ config = require $ masterFilename ;
47+ if ($ masterConfigExists ) {
5648
57- if (!is_array ( $ config )) {
58- throw new UnexpectedValueException (
59- $ this -> translate (
60- ' Configuration file "{file}" should return an array. ' ,
61- [ 'file ' => $ masterFilename]
62- )
63- );
64- }
49+ if (!is_readable ( $ masterFilename ) || ! is_file ( $ masterFilename )) {
50+ throw new RuntimeException ( $ this -> translate (
51+ ' Cannot read configuration from "{file}". ' ,
52+ [
53+ 'file ' => $ masterFilename
54+ ]
55+ ) );
56+ }
6557
66- array_walk_recursive (
67- $ config ,
68- function (&$ v ) {
69- $ value = $ this ->createEntity ($ v );
70- $ v = $ value ;
58+ /** @noinspection PhpIncludeInspection */
59+ $ config = require $ masterFilename ;
60+
61+ if (!is_array ($ config )) {
62+ throw new UnexpectedValueException (
63+ $ this ->translate (
64+ 'Configuration file "{file}" should return an array. ' ,
65+ ['file ' => $ masterFilename ]
66+ )
67+ );
7168 }
72- );
69+
70+ array_walk_recursive (
71+ $ config ,
72+ function (&$ v ) {
73+ $ value = $ this ->createMasterEntity ($ v );
74+ $ v = $ value ;
75+ }
76+ );
77+
78+ } else {
79+ $ config = [];
80+ }
7381
7482 if (is_readable ($ localFilename ) && is_file ($ localFilename )) {
7583 /** @noinspection PhpIncludeInspection */
@@ -84,7 +92,22 @@ function (&$v) {
8492 );
8593 }
8694
95+ array_walk_recursive (
96+ $ localSource ,
97+ function (&$ v ) {
98+ $ value = $ this ->createLocalEntity ($ v );
99+ $ v = $ value ;
100+ }
101+ );
102+
87103 $ this ->mergeConfig ($ config , $ localSource );
104+ } elseif (!$ masterConfigExists ) {
105+ throw new RuntimeException ($ this ->translate (
106+ 'Master configuration file "{file}" not found. ' ,
107+ [
108+ 'file ' => $ masterFilename
109+ ]
110+ ));
88111 }
89112
90113 return $ this ->createConfigSource ($ configAlias , $ config );
@@ -102,11 +125,14 @@ protected function mergeConfig(array &$master, array &$local)
102125 if (isset ($ master [$ key ])) {
103126 $ masterValue = & $ master [$ key ];
104127
105- //TODO: refactoring
106128 if ($ masterValue instanceof IConfigSource) {
107129 $ masterValue = $ masterValue ->getSource ();
108130 }
109131
132+ if ($ localValue instanceof IConfigSource) {
133+ $ localValue = $ localValue ->getSource ();
134+ }
135+
110136 if (is_array ($ masterValue )) {
111137 if (!is_array ($ localValue )) {
112138 throw new UnexpectedValueException ($ this ->translate (
@@ -119,6 +145,10 @@ protected function mergeConfig(array &$master, array &$local)
119145
120146 $ this ->mergeConfig ($ masterValue , $ localValue );
121147 } elseif ($ masterValue instanceof IConfigValue) {
148+
149+ if ($ localValue instanceof IConfigValue) {
150+ $ localValue = $ localValue ->get ();
151+ }
122152 try {
123153 $ masterValue ->set ($ localValue , IConfigValue::KEY_LOCAL )
124154 ->save ();
@@ -141,7 +171,9 @@ protected function mergeConfig(array &$master, array &$local)
141171 ));
142172 }
143173 } else {
144- if (is_array ($ localValue )) {
174+ if ($ localValue instanceof IConfigSource || $ localValue instanceof IConfigValue) {
175+ $ master [$ key ] = $ localValue ;
176+ } elseif (is_array ($ localValue )) {
145177 array_walk_recursive (
146178 $ localValue ,
147179 function (&$ v ) {
@@ -167,23 +199,22 @@ function (&$v) {
167199
168200 /**
169201 * Создает сущности на основе конфигурации.
170- * @param string $masterValue значение
202+ * @param string $value значение
171203 * @throws UnexpectedValueException если значение не скалярное
172204 * @return IConfigSource|ISeparateConfigSource|IConfigValue
173205 */
174- protected function createEntity ( $ masterValue )
206+ protected function createMasterEntity ( $ value )
175207 {
176- if (!is_scalar ($ masterValue ) && !is_null ($ masterValue )) {
208+ if (!is_scalar ($ value ) && !is_null ($ value )) {
177209 throw new UnexpectedValueException ($ this ->translate (
178210 'Unexpected configuration value. Configuration can contain only scalar values. '
179211 ));
180212 }
181- if (preg_match ('/^{#(\S+):(.+)}$/ ' , $ masterValue , $ matches )) {
213+ if (preg_match ('/^{#(\S+):(.+)}$/ ' , $ value , $ matches )) {
182214 list (, $ command , $ value ) = $ matches ;
183215
184216 switch ($ command ) {
185217 case self ::COMMAND_PART :
186- // TODO: подумать, мб через config factory?
187218 return $ this ->read ($ value );
188219 case self ::COMMAND_LAZY :
189220 return $ this ->createSeparateConfigSource ('lazy ' , $ value );
@@ -208,11 +239,55 @@ protected function createEntity($masterValue)
208239
209240 return $ this ->createConfigValue (
210241 [
211- IConfigValue::KEY_MASTER => $ masterValue
242+ IConfigValue::KEY_MASTER => $ value
212243 ]
213244 );
214245 }
215246
247+ /**
248+ * Создает сущности на основе конфигурации.
249+ * @param string $value значение
250+ * @throws UnexpectedValueException если значение не скалярное
251+ * @return IConfigSource|ISeparateConfigSource|IConfigValue
252+ */
253+ protected function createLocalEntity ($ value )
254+ {
255+ if (!is_scalar ($ value ) && !is_null ($ value )) {
256+ throw new UnexpectedValueException ($ this ->translate (
257+ 'Unexpected configuration value. Configuration can contain only scalar values. '
258+ ));
259+ }
260+ if (preg_match ('/^{#(\S+):(.+)}$/ ' , $ value , $ matches )) {
261+ list (, $ command , $ value ) = $ matches ;
262+
263+ switch ($ command ) {
264+ case self ::COMMAND_PART :
265+ return $ this ->read ($ value );
266+ case self ::COMMAND_LAZY :
267+ return $ this ->createSeparateConfigSource ('lazy ' , $ value );
268+ case self ::LOCAL_DIR : {
269+ $ files = $ this ->getFilesByAlias ($ value );
270+ return $ this ->createConfigValue (
271+ [
272+ IConfigValue::KEY_LOCAL => $ files [IConfigValue::KEY_LOCAL ]
273+ ]
274+ );
275+ }
276+ case self ::MASTER_DIR : {
277+ $ files = $ this ->getFilesByAlias ($ value );
278+ return $ this ->createConfigValue (
279+ [
280+ IConfigValue::KEY_MASTER => $ files [IConfigValue::KEY_MASTER ]
281+ ]
282+ );
283+ }
284+ }
285+ }
286+
287+ return $ value ;
288+
289+ }
290+
216291 /**
217292 * @param array $values
218293 * @return ConfigValue
0 commit comments