Skip to content

Commit 3a281af

Browse files
committed
1) Supported dot-notation syntax with an asterisk.
2) Fixed crash if file is empty.
1 parent 96a3f1c commit 3a281af

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ $conf->load();
2828

2929
var_dump($conf->all()); // get all config
3030
echo $conf->get('foo.bar'); // get nested key use dot notation
31-
echo $conf['foo.bar']; // thie same, but use ArrayAccess interface.
31+
echo $conf['foo.bar']; // the same, but use ArrayAccess interface.
3232
```
3333

34+
Supported dot-notation syntax with an asterisk in `get` method.
35+
You can read about it here: https://github.com/spacetab-io/obelix-php
36+
3437
2) If u would like override default values, you can pass 2 arguments to
3538
class constructor or set up use setters.
3639

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
"php": ">=7.4",
2020
"symfony/yaml": "^4.2 || ^5.0",
2121
"psr/log": "^1.0",
22-
"symfony/console": "^4.3 || ^5.0"
22+
"symfony/console": "^4.3 || ^5.0",
23+
"spacetab-io/obelix": "^1.0"
2324
},
2425
"require-dev": {
2526
"phpunit/phpunit": "^9",
2627
"symfony/var-dumper": "^4.2 || ^5.0",
2728
"humbug/box": "^3.8",
28-
"phpstan/phpstan": "^0.12.18",
29+
"phpstan/phpstan": "^0.12",
2930
"spacetab-io/logger": "^2.0"
3031
},
3132
"suggest": {

src/Configuration.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Log\LoggerAwareTrait;
1010
use Psr\Log\NullLogger;
1111
use Spacetab\Configuration\Exception\ConfigurationException;
12+
use Spacetab\Obelix;
1213
use Symfony\Component\Yaml\Yaml;
1314

1415
/**
@@ -55,10 +56,8 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
5556

5657
/**
5758
* Config tree goes here.
58-
*
59-
* @var array<mixed>
6059
*/
61-
private array $config;
60+
private Obelix\Dot $config;
6261

6362
/**
6463
* @var string
@@ -75,8 +74,6 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
7574
*
7675
* @param null|string $path
7776
* @param null|string $stage
78-
*
79-
* @throws \Spacetab\Configuration\Exception\ConfigurationException
8077
*/
8178
public function __construct(?string $path = null, ?string $stage = null)
8279
{
@@ -109,22 +106,19 @@ public static function auto(?string $stage = null): Configuration
109106
/**
110107
* Get's a value from config by dot notation
111108
* 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'.
113110
*
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
115115
* @param mixed $default
116116
*
117117
* @return mixed
118118
*/
119119
public function get($key, $default = null)
120120
{
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();
128122
}
129123

130124
/**
@@ -134,7 +128,7 @@ public function get($key, $default = null)
134128
*/
135129
public function all(): array
136130
{
137-
return $this->config;
131+
return $this->config->toArray();
138132
}
139133

140134
/**
@@ -266,11 +260,13 @@ public function load(): Configuration
266260
? $this->parseConfiguration($this->getStage())
267261
: [];
268262

269-
$this->config = $this->arrayMergeRecursive(
263+
$array = $this->arrayMergeRecursive(
270264
$this->parseConfiguration(),
271265
$second
272266
);
273267

268+
$this->config = new Obelix\Dot($array);
269+
274270
$this->logger->info('Configuration loaded.');
275271

276272
return $this;
@@ -310,6 +306,12 @@ private function parseConfiguration(string $stage = self::DEFAULT_STAGE): array
310306
$config = [];
311307
foreach ($files as $filename) {
312308
$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+
313315
$directory = basename(pathinfo($filename, PATHINFO_DIRNAME));
314316
$top = key($content);
315317

src/ConfigurationInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ interface ConfigurationInterface
1616
* E.g get('x.y', 'foo') => returns the value of $config['x']['y']
1717
* And if not exist, return 'foo'
1818
*
19-
* @param mixed $key
19+
* Supported dot-notation syntax with an asterisk.
20+
* You can read about it here: https://github.com/spacetab-io/obelix-php
21+
*
22+
* @param string $key
2023
* @param mixed $default
2124
* @return mixed
2225
*/

0 commit comments

Comments
 (0)