Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ArrayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stefna\Config;

final class ArrayConfig implements Config
final class ArrayConfig implements Config, ExportableConfig
{
use GetConfigTrait;

Expand Down Expand Up @@ -37,4 +37,9 @@ public function getRawValue(string $key): mixed
}
return $root;
}

public function getArrayCopy(): array
{
return $this->config;
}
}
11 changes: 11 additions & 0 deletions src/ExportableConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Stefna\Config;

interface ExportableConfig
{
/**
* @return array<mixed>
*/
public function getArrayCopy(): array;
}
18 changes: 14 additions & 4 deletions src/FileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Stefna\Config\Exception\FileNotFound;

final class FileConfig implements Config
final class FileConfig implements Config, ExportableConfig
{
use GetConfigTrait;

Expand All @@ -15,16 +15,26 @@ public function __construct(
) {}

public function getRawValue(string $key): mixed
{
$this->loadFile();
return $this->loadedConfig->getRawValue($key);
}

public function getArrayCopy(): array
{
$this->loadFile();
return $this->loadedConfig->getArrayCopy();
}

private function loadFile(): void
{
if (isset($this->loadedConfig)) {
return $this->loadedConfig->getRawValue($key);
return;
}

if (!file_exists($this->configFile)) {
throw new FileNotFound('Configuration not found: ' . $this->configFile);
}
$this->loadedConfig = ArrayConfig::fromArray(require $this->configFile);

return $this->loadedConfig->getRawValue($key);
}
}
26 changes: 23 additions & 3 deletions tests/AbstractConfigTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Stefna\Config\ArrayConfig;
use Stefna\Config\Config;
use Stefna\Config\ExportableConfig;
use Stefna\Config\Tests\Stub\TestStub;

abstract class AbstractConfigTestCase extends TestCase
Expand Down Expand Up @@ -117,9 +118,22 @@ public function testDotKeyReturnEmptyOnExtraDots(): void
$this->assertNull($config->get('.nesting'));
}

protected function getDefaultArrayConfig(): ArrayConfig
public function testExportableConfig(): void
{
$config = $this->getConfig();
if (!$config instanceof ExportableConfig) {
$this->markTestSkipped('Config does not implement ExportableConfig');
}

$this->assertSame($this->getDefaultConfigAsArray(), $config->getArrayCopy());
}

/**
* @return array<string, mixed>
*/
protected function getDefaultConfigAsArray(): array
{
return new ArrayConfig([
return [
'testString' => 'string',
'testNumberAsString' => '43',
'testNumber' => 42,
Expand All @@ -140,7 +154,13 @@ protected function getDefaultArrayConfig(): ArrayConfig
'value' => 2,
],
],
]);
];
}


protected function getDefaultArrayConfig(): ArrayConfig
{
return new ArrayConfig($this->getDefaultConfigAsArray());
}

abstract protected function getConfig(): Config;
Expand Down
Loading