Skip to content

Commit

Permalink
Make extensible the config readers
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Jul 10, 2021
1 parent 7773a0d commit 01e36ed
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
42 changes: 29 additions & 13 deletions src/Framework/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,42 @@ final class Config

private array $config = [];

/** @var array<string, ConfigReaderInterface> */
private array $configReaders;

/**
* @param array<string, ConfigReaderInterface> $configReaders
*/
private function __construct(array $configReaders)
{
$this->configReaders = $configReaders;
}

public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
self::$instance = new self([
'php' => new PhpConfigReader(),
'env' => new EnvConfigReader(),
]);
}

return self::$instance;
}

public static function resetInstance(): void
{
self::$instance = null;
}

/**
* @param array<string, ConfigReaderInterface> $configReaders
*/
public function setConfigReaders(array $configReaders = []): void
{
$this->configReaders = $configReaders;
}

public static function setApplicationRootDir(string $dir): void
{
self::$applicationRootDir = $dir;
Expand Down Expand Up @@ -80,7 +107,7 @@ public function init(): void
self::getApplicationRootDir(),
$this->createGacelaJsonConfigCreator(),
$this->createPathFinder(),
$this->createConfigReaders()
$this->configReaders
))->readAll();
}

Expand All @@ -97,17 +124,6 @@ private function createPathFinder(): PathFinderInterface
return new PathFinder();
}

/**
* @return array<string, ConfigReaderInterface>
*/
private function createConfigReaders(): array
{
return [
'php' => new PhpConfigReader(),
'env' => new EnvConfigReader(),
];
}

private function hasValue(string $key): bool
{
return isset($this->config[$key]);
Expand Down
3 changes: 2 additions & 1 deletion src/Framework/Config/GacelaJsonConfigItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

final class GacelaJsonConfigItem
{
private const DEFAULT_TYPE = 'php';
public const DEFAULT_TYPE = 'php';

private const DEFAULT_PATH = 'config/*.php';
private const DEFAULT_PATH_LOCAL = 'config/local.php';

Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Framework/Container/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace GacelaTest\Unit\Framework\Container;

use Gacela\Framework\Config;
use Gacela\Framework\Config\ConfigReaderInterface;
use PHPUnit\Framework\TestCase;

final class ConfigTest extends TestCase
{
private Config $config;

public function setUp(): void
{
Config::resetInstance();
$this->config = Config::getInstance();
}

public function tearDown(): void
{
Config::resetInstance();
}

public function test_get_undefined_key(): void
{
$this->expectExceptionMessageMatches('/Could not find config key "key"/');
$this->config->get('key');
}

public function test_get_default_value_from_undefined_key(): void
{
self::assertSame('default', $this->config->get('key', 'default'));
}

public function test_get_using_custom_reader(): void
{
$this->config->setConfigReaders([
Config\GacelaJsonConfigItem::DEFAULT_TYPE => new class() implements ConfigReaderInterface {
public function read(string $absolutePath): array
{
return ['key' => 'value'];
}

public function canRead(string $absolutePath): bool
{
return true;
}
},
]);

$this->config->init();

self::assertSame('value', $this->config->get('key'));
}
}

0 comments on commit 01e36ed

Please sign in to comment.