diff --git a/CHANGELOG.md b/CHANGELOG.md index 94ed075..182c697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Config.php b/src/Config.php index ee5b2ea..33597e6 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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; } @@ -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) { @@ -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; @@ -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*(?:=|(?\w+)\s*:\s*)(?[^}]+)\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; + } } } \ No newline at end of file diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index e463dad..d711dab 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -82,8 +82,8 @@ 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'] ); @@ -91,8 +91,10 @@ public function testGet() $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')); } @@ -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'] ); @@ -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'] ); @@ -136,8 +138,9 @@ public function testGetArrayCopy() ] ], "section2" => [ - "bar" => "value", - "baz" => null, + "bar" => "value-test", + "baz" => 123456, + "qux" => 123456, ], "baz" => true ]; diff --git a/tests/config3.json5 b/tests/config3.json5 index 2a6e1bb..57c7d04 100644 --- a/tests/config3.json5 +++ b/tests/config3.json5 @@ -9,6 +9,8 @@ ] }, "section2": { - "bar": "{config:section.foo}", + "bar": "{config:section.foo}-test", + "baz": 123456, + "qux": "{config:section2.baz}" } } \ No newline at end of file