From 66f03c828f061006a20abb3add136e84aed63ede Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 27 Nov 2023 14:37:01 +0000 Subject: [PATCH] Merge pull request #1478 from hydephp/fix-dot-notation-support-in-yaml-configuration Expand dot notation when merging Yaml configuration https://github.com/hydephp/develop/commit/79907bd7ff8c8275bceeb7d6d18ac710504c3482 --- .../Internal/LoadYamlConfiguration.php | 5 +-- tests/Feature/LoadYamlConfigurationTest.php | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Foundation/Internal/LoadYamlConfiguration.php b/src/Foundation/Internal/LoadYamlConfiguration.php index 59fcc1da..833f47fb 100644 --- a/src/Foundation/Internal/LoadYamlConfiguration.php +++ b/src/Foundation/Internal/LoadYamlConfiguration.php @@ -6,6 +6,7 @@ use Hyde\Hyde; use Hyde\Facades\Config; +use Illuminate\Support\Arr; use Symfony\Component\Yaml\Yaml; use function array_key_first; @@ -48,7 +49,7 @@ protected function hasYamlConfigFile(): bool /** @return array */ protected function getYaml(): array { - return (array) Yaml::parse(file_get_contents($this->getFile())); + return Arr::undot((array) Yaml::parse(file_get_contents($this->getFile()))); } protected function getFile(): string @@ -67,7 +68,7 @@ protected function mergeParsedConfiguration(): void if ($this->configurationContainsNamespaces($yaml)) { /** @var array> $yaml */ foreach ($yaml as $namespace => $data) { - $this->mergeConfiguration($namespace, (array) $data); + $this->mergeConfiguration($namespace, Arr::undot((array) $data)); } return; diff --git a/tests/Feature/LoadYamlConfigurationTest.php b/tests/Feature/LoadYamlConfigurationTest.php index eb461d89..47f199eb 100644 --- a/tests/Feature/LoadYamlConfigurationTest.php +++ b/tests/Feature/LoadYamlConfigurationTest.php @@ -227,6 +227,41 @@ public function testHydeNamespaceCanBlank() $this->assertSame('baz', Config::get('foo.bar')); } + public function testDotNotationCanBeUsed() + { + config(['hyde' => []]); + + $this->file('hyde.yml', <<<'YAML' + foo.bar.baz: qux + YAML); + $this->runBootstrapper(); + + $this->assertSame(['foo' => ['bar' => ['baz' => 'qux']]], Config::get('hyde')); + $this->assertSame('qux', Config::get('hyde.foo.bar.baz')); + } + + public function testDotNotationCanBeUsedWithNamespaces() + { + config(['hyde' => []]); + + $this->file('hyde.yml', <<<'YAML' + hyde: + foo.bar.baz: qux + one: + foo: + bar: + baz: qux + two: + foo.bar.baz: qux + YAML); + $this->runBootstrapper(); + + $expected = ['foo' => ['bar' => ['baz' => 'qux']]]; + $this->assertSame($expected, Config::get('hyde')); + $this->assertSame($expected, Config::get('one')); + $this->assertSame($expected, Config::get('two')); + } + protected function runBootstrapper(): void { $this->app->bootstrapWith([LoadYamlConfiguration::class]);