Skip to content

Commit

Permalink
Add parameter $compiled to Config::getArrayCopy() to get the comp…
Browse files Browse the repository at this point in the history
…iled version

Fixed multiple function calls in a value, or concatenation of function result and string
Fixed way of merge from multiple configuration file
  • Loading branch information
ElGigi committed Apr 14, 2021
1 parent 761f9b6 commit b0089b5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.0.0-beta2] - In progress
## [2.0.0-beta2] - 2021-04-14

### Added

- New `YamlAdapter` adapter
- Add parameter `$compiled` to `Config::getArrayCopy()` to get the compiled version

### Fixed

- Fixed multiple function calls in a value, or concatenation of function result and string
- Fixed way of merge from multiple configuration file

## [2.0.0-beta1] - 2021-04-07

Expand Down
45 changes: 28 additions & 17 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function get(string $key = null, mixed $default = null): mixed
$found = false;

foreach ($this->configs as $config) {
if (!$config->has($key)) {
if (false === $config->has($key)) {
continue;
}

Expand All @@ -125,7 +125,7 @@ public function get(string $key = null, mixed $default = null): mixed
return $value;
}

$arrayValue = array_merge($arrayValue ?? [], $value);
$arrayValue = b_array_merge_recursive($value, $arrayValue ?? []);
}

if (false === $found) {
Expand Down Expand Up @@ -155,12 +155,17 @@ public function has(string $key): bool
/**
* @inheritDoc
*/
public function getArrayCopy(): array
public function getArrayCopy(bool $compiled = false): array
{
$configArrays = array_map(fn(ConfigInterface $config) => $config->getArrayCopy(), $this->configs);
rsort($configArrays);
$configArray = b_array_merge_recursive(...$configArrays);
unset($configArrays);

if (false === $compiled) {
return $configArray;
}

$this->treatValue($configArray);

return $configArray;
Expand All @@ -186,27 +191,33 @@ protected function treatValue(mixed &$value): void
return;
}

// Not to treat
if (!str_starts_with($value, static::ENCAPSULATION_START) &&
!str_ends_with($value, static::ENCAPSULATION_END)) {
$matches = [];
if (!preg_match_all(
'#{\s*(?:=|(?<function>\w+)\s*:\s*)(?<value>[^}]+)\s*}#',
$value,
$matches,
PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL
)) {
return;
}

// If it's an asked value {= varName}
if (str_starts_with($value, static::ENCAPSULATION_START . '=')) {
$value = $this->functions->execute('var', trim(substr($value, 2, -1)));
if (empty($matches)) {
return;
}

$tmpValue = substr($value, 1, -1);
$tmpValue = explode(':', $tmpValue, 2);
$tmpValue = array_map('trim', $tmpValue);
$shift = 0;
foreach ($matches[0] as $key => $match) {
$function = $matches['function'][$key][0] ?? 'var';
$result = $this->functions->execute($function, trim($matches['value'][$key][0]));

// Not to treat
if (2 !== count($tmpValue)) {
return;
}
if (strlen($match[0]) == strlen($value)) {
$value = $result;
return;
}

$value = $this->functions->execute($tmpValue[0], $tmpValue[1]);
$result = (string)$result;
$value = substr_replace($value, $result, $match[1] + $shift, $length = strlen($match[0]));
$shift = strlen($result) - $length;
}
}
}
17 changes: 10 additions & 7 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,19 @@ public function testGet()
$config = new FakeConfig(
[
new JsonAdapter(__DIR__ . '/config.json5', true, priority: 0),
new JsonAdapter(__DIR__ . '/config2.json5', true, priority: 1),
new JsonAdapter(__DIR__ . '/config3.json5', true, priority: 10),
new JsonAdapter(__DIR__ . '/config2.json5', true, priority: 1),
],
variables: ['QUX' => 'QUX QUX QUX']
);

$this->assertEquals('ERASE ARRAY', $config->get('foo'));
$this->assertEquals(['ERASE STRING'], $config->get('bar'));
$this->assertEquals('QUX VALUE', $config->get('qux'));
$this->assertEquals('value', $config->get('section2.bar'));
$this->assertEquals(['QUX QUX QUX', 'QUX QUX QUX', 'value2', '{not}'], $config->get('section.qux'));
$this->assertEquals('value-test', $config->get('section2.bar'));
$this->assertSame(123456, $config->get('section2.qux'));
$this->assertEquals(['value2', '{not}', 'QUX QUX QUX', 'QUX QUX QUX'], $config->get('section.qux'));
$this->assertEquals(['bar' => 'value-test', 'baz' => 123456, 'qux' => 123456], $config->get('section2'));
$this->assertSame(true, $config->get('baz'));
}

Expand All @@ -101,8 +103,8 @@ public function testHas()
$config = new FakeConfig(
[
new JsonAdapter(__DIR__ . '/config.json5', true, priority: 0),
new JsonAdapter(__DIR__ . '/config2.json5', true, priority: 1),
new JsonAdapter(__DIR__ . '/config3.json5', true, priority: 10),
new JsonAdapter(__DIR__ . '/config2.json5', true, priority: 1),
],
variables: ['QUX' => 'QUX QUX QUX']
);
Expand All @@ -117,8 +119,8 @@ public function testGetArrayCopy()
$config = new FakeConfig(
[
new JsonAdapter(__DIR__ . '/config.json5', true, priority: 0),
new JsonAdapter(__DIR__ . '/config2.json5', true, priority: 1),
new JsonAdapter(__DIR__ . '/config3.json5', true, priority: 10),
new JsonAdapter(__DIR__ . '/config2.json5', true, priority: 1),
],
variables: ['QUX' => 'QUX QUX QUX']
);
Expand All @@ -136,8 +138,9 @@ public function testGetArrayCopy()
]
],
"section2" => [
"bar" => "value",
"baz" => null,
"bar" => "value-test",
"baz" => 123456,
"qux" => 123456,
],
"baz" => true
];
Expand Down
4 changes: 3 additions & 1 deletion tests/config3.json5
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
]
},
"section2": {
"bar": "{config:section.foo}",
"bar": "{config:section.foo}-test",
"baz": 123456,
"qux": "{config:section2.baz}"
}
}

0 comments on commit b0089b5

Please sign in to comment.