9
9
use Psr \Log \LoggerAwareTrait ;
10
10
use Psr \Log \NullLogger ;
11
11
use Spacetab \Configuration \Exception \ConfigurationException ;
12
+ use Spacetab \Obelix ;
12
13
use Symfony \Component \Yaml \Yaml ;
13
14
14
15
/**
@@ -55,10 +56,8 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
55
56
56
57
/**
57
58
* Config tree goes here.
58
- *
59
- * @var array<mixed>
60
59
*/
61
- private array $ config ;
60
+ private Obelix \ Dot $ config ;
62
61
63
62
/**
64
63
* @var string
@@ -75,8 +74,6 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
75
74
*
76
75
* @param null|string $path
77
76
* @param null|string $stage
78
- *
79
- * @throws \Spacetab\Configuration\Exception\ConfigurationException
80
77
*/
81
78
public function __construct (?string $ path = null , ?string $ stage = null )
82
79
{
@@ -109,22 +106,19 @@ public static function auto(?string $stage = null): Configuration
109
106
/**
110
107
* Get's a value from config by dot notation
111
108
* E.g get('x.y', 'foo') => returns the value of $config['x']['y']
112
- * And if not exist, return 'foo'
109
+ * And if not exist, return 'foo'.
113
110
*
114
- * @param mixed $key
111
+ * Supported dot-notation syntax with an asterisk.
112
+ * You can read about it here: https://github.com/spacetab-io/obelix-php
113
+ *
114
+ * @param string $key
115
115
* @param mixed $default
116
116
*
117
117
* @return mixed
118
118
*/
119
119
public function get ($ key , $ default = null )
120
120
{
121
- $ config = $ this ->config ;
122
-
123
- array_map (function ($ key ) use (&$ config , $ default ) {
124
- $ config = $ config [$ key ] ?? $ default ;
125
- }, explode ('. ' , $ key ));
126
-
127
- return $ config ;
121
+ return $ this ->config ->get ($ key , $ default )->getValue ();
128
122
}
129
123
130
124
/**
@@ -134,7 +128,7 @@ public function get($key, $default = null)
134
128
*/
135
129
public function all (): array
136
130
{
137
- return $ this ->config ;
131
+ return $ this ->config -> toArray () ;
138
132
}
139
133
140
134
/**
@@ -266,11 +260,13 @@ public function load(): Configuration
266
260
? $ this ->parseConfiguration ($ this ->getStage ())
267
261
: [];
268
262
269
- $ this -> config = $ this ->arrayMergeRecursive (
263
+ $ array = $ this ->arrayMergeRecursive (
270
264
$ this ->parseConfiguration (),
271
265
$ second
272
266
);
273
267
268
+ $ this ->config = new Obelix \Dot ($ array );
269
+
274
270
$ this ->logger ->info ('Configuration loaded. ' );
275
271
276
272
return $ this ;
@@ -310,6 +306,12 @@ private function parseConfiguration(string $stage = self::DEFAULT_STAGE): array
310
306
$ config = [];
311
307
foreach ($ files as $ filename ) {
312
308
$ content = Yaml::parseFile ($ filename );
309
+
310
+ if (empty ($ content )) {
311
+ $ this ->logger ->info (sprintf ('File %s is empty. Skip it. ' , $ filename ));
312
+ continue ;
313
+ }
314
+
313
315
$ directory = basename (pathinfo ($ filename , PATHINFO_DIRNAME ));
314
316
$ top = key ($ content );
315
317
0 commit comments