Skip to content

Commit

Permalink
1) Supported dot-notation syntax with an asterisk.
Browse files Browse the repository at this point in the history
2) Fixed crash if file is empty.
  • Loading branch information
roquie committed May 21, 2020
1 parent 96a3f1c commit 3a281af
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ $conf->load();

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

Supported dot-notation syntax with an asterisk in `get` method.
You can read about it here: https://github.com/spacetab-io/obelix-php

2) If u would like override default values, you can pass 2 arguments to
class constructor or set up use setters.

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
"php": ">=7.4",
"symfony/yaml": "^4.2 || ^5.0",
"psr/log": "^1.0",
"symfony/console": "^4.3 || ^5.0"
"symfony/console": "^4.3 || ^5.0",
"spacetab-io/obelix": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"symfony/var-dumper": "^4.2 || ^5.0",
"humbug/box": "^3.8",
"phpstan/phpstan": "^0.12.18",
"phpstan/phpstan": "^0.12",
"spacetab-io/logger": "^2.0"
},
"suggest": {
Expand Down
34 changes: 18 additions & 16 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Spacetab\Configuration\Exception\ConfigurationException;
use Spacetab\Obelix;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -55,10 +56,8 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger

/**
* Config tree goes here.
*
* @var array<mixed>
*/
private array $config;
private Obelix\Dot $config;

/**
* @var string
Expand All @@ -75,8 +74,6 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
*
* @param null|string $path
* @param null|string $stage
*
* @throws \Spacetab\Configuration\Exception\ConfigurationException
*/
public function __construct(?string $path = null, ?string $stage = null)
{
Expand Down Expand Up @@ -109,22 +106,19 @@ public static function auto(?string $stage = null): Configuration
/**
* Get's a value from config by dot notation
* E.g get('x.y', 'foo') => returns the value of $config['x']['y']
* And if not exist, return 'foo'
* And if not exist, return 'foo'.
*
* @param mixed $key
* Supported dot-notation syntax with an asterisk.
* You can read about it here: https://github.com/spacetab-io/obelix-php
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
$config = $this->config;

array_map(function ($key) use (&$config, $default) {
$config = $config[$key] ?? $default;
}, explode('.', $key));

return $config;
return $this->config->get($key, $default)->getValue();
}

/**
Expand All @@ -134,7 +128,7 @@ public function get($key, $default = null)
*/
public function all(): array
{
return $this->config;
return $this->config->toArray();
}

/**
Expand Down Expand Up @@ -266,11 +260,13 @@ public function load(): Configuration
? $this->parseConfiguration($this->getStage())
: [];

$this->config = $this->arrayMergeRecursive(
$array = $this->arrayMergeRecursive(
$this->parseConfiguration(),
$second
);

$this->config = new Obelix\Dot($array);

$this->logger->info('Configuration loaded.');

return $this;
Expand Down Expand Up @@ -310,6 +306,12 @@ private function parseConfiguration(string $stage = self::DEFAULT_STAGE): array
$config = [];
foreach ($files as $filename) {
$content = Yaml::parseFile($filename);

if (empty($content)) {
$this->logger->info(sprintf('File %s is empty. Skip it.', $filename));
continue;
}

$directory = basename(pathinfo($filename, PATHINFO_DIRNAME));
$top = key($content);

Expand Down
5 changes: 4 additions & 1 deletion src/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ interface ConfigurationInterface
* E.g get('x.y', 'foo') => returns the value of $config['x']['y']
* And if not exist, return 'foo'
*
* @param mixed $key
* Supported dot-notation syntax with an asterisk.
* You can read about it here: https://github.com/spacetab-io/obelix-php
*
* @param string $key
* @param mixed $default
* @return mixed
*/
Expand Down

0 comments on commit 3a281af

Please sign in to comment.