From 01e36edbf33c9c1c793d994ff3c309cf58f3636b Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sat, 10 Jul 2021 14:28:20 +0200 Subject: [PATCH] Make extensible the config readers --- src/Framework/Config.php | 42 +++++++++----- src/Framework/Config/GacelaJsonConfigItem.php | 3 +- tests/Unit/Framework/Container/ConfigTest.php | 57 +++++++++++++++++++ 3 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 tests/Unit/Framework/Container/ConfigTest.php diff --git a/src/Framework/Config.php b/src/Framework/Config.php index a8b8ee2f..ebcd1040 100644 --- a/src/Framework/Config.php +++ b/src/Framework/Config.php @@ -24,15 +24,42 @@ final class Config private array $config = []; + /** @var array */ + private array $configReaders; + + /** + * @param array $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 $configReaders + */ + public function setConfigReaders(array $configReaders = []): void + { + $this->configReaders = $configReaders; + } + public static function setApplicationRootDir(string $dir): void { self::$applicationRootDir = $dir; @@ -80,7 +107,7 @@ public function init(): void self::getApplicationRootDir(), $this->createGacelaJsonConfigCreator(), $this->createPathFinder(), - $this->createConfigReaders() + $this->configReaders ))->readAll(); } @@ -97,17 +124,6 @@ private function createPathFinder(): PathFinderInterface return new PathFinder(); } - /** - * @return array - */ - private function createConfigReaders(): array - { - return [ - 'php' => new PhpConfigReader(), - 'env' => new EnvConfigReader(), - ]; - } - private function hasValue(string $key): bool { return isset($this->config[$key]); diff --git a/src/Framework/Config/GacelaJsonConfigItem.php b/src/Framework/Config/GacelaJsonConfigItem.php index 7bc64f45..b91fc402 100644 --- a/src/Framework/Config/GacelaJsonConfigItem.php +++ b/src/Framework/Config/GacelaJsonConfigItem.php @@ -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'; diff --git a/tests/Unit/Framework/Container/ConfigTest.php b/tests/Unit/Framework/Container/ConfigTest.php new file mode 100644 index 00000000..fd5aa51b --- /dev/null +++ b/tests/Unit/Framework/Container/ConfigTest.php @@ -0,0 +1,57 @@ +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')); + } +}