From f819922d8abd012a2972e543c3d4ec84e40a0427 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Sat, 20 Aug 2022 17:43:06 +0800 Subject: [PATCH] add basic usable config --- src/ZM/Config/RefactoredConfig.php | 16 +++- tests/ZM/Config/RefactoredConfigTest.php | 101 +++++++++++++++++++++++ 2 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 tests/ZM/Config/RefactoredConfigTest.php diff --git a/src/ZM/Config/RefactoredConfig.php b/src/ZM/Config/RefactoredConfig.php index 42859390..6268e7af 100644 --- a/src/ZM/Config/RefactoredConfig.php +++ b/src/ZM/Config/RefactoredConfig.php @@ -72,20 +72,31 @@ class RefactoredConfig foreach ($this->config_paths as $config_path) { $files = scandir($config_path); foreach ($files as $file) { + // 略过不支持的文件 if (!in_array(pathinfo($file, PATHINFO_EXTENSION), self::ALLOWED_FILE_EXTENSIONS, true)) { continue; } + $file_path = $config_path . '/' . $file; if (is_dir($file_path)) { // TODO: 支持子目录(待定) continue; } + $file = pathinfo($file, PATHINFO_FILENAME); + + // 略过不应加载的文件 + if (!$this->shouldLoadFile($file)) { + continue; + } + + // 略过加载顺序未知的文件 $load_type = $this->getFileLoadType($file); if (!in_array($load_type, self::LOAD_ORDER, true)) { continue; } + // 将文件加入到对应的加载阶段 $stages[$load_type][] = $file_path; } } @@ -93,10 +104,7 @@ class RefactoredConfig // 按照加载顺序加载配置文件 foreach (self::LOAD_ORDER as $load_type) { foreach ($stages[$load_type] as $file_path) { - $file = pathinfo($file_path, PATHINFO_FILENAME); - if ($this->shouldLoadFile($file)) { - $this->loadConfigFromPath($file); - } + $this->loadConfigFromPath($file_path); } } } diff --git a/tests/ZM/Config/RefactoredConfigTest.php b/tests/ZM/Config/RefactoredConfigTest.php new file mode 100644 index 00000000..ae1bffd7 --- /dev/null +++ b/tests/ZM/Config/RefactoredConfigTest.php @@ -0,0 +1,101 @@ + 'bar', + 'bar' => 'baz', + 'baz' => 'bat', + 'null' => null, + 'boolean' => true, + 'associate' => [ + 'x' => 'xxx', + 'y' => 'yyy', + ], + 'array' => [ + 'aaa', + 'zzz', + ], + 'x' => [ + 'z' => 'zoo', + ], + 'a.b' => 'c', + 'a' => [ + 'b.c' => 'd', + ], + 'from' => 'global', + ]; + + // 下方测试需要临时写入的文件 + file_put_contents($mock_dir . '/test.php', ' "environment", "env" => "dev"];'); + file_put_contents($mock_dir . '/test.production.php', + ' "environment", "env" => "prod"];'); + file_put_contents($mock_dir . '/test.invalid.php', ' "invalid"];'); + + $config = new RefactoredConfig([ + __DIR__ . '/config_mock', + ], 'development'); + self::$config = $config; + } + + public static function tearDownAfterClass(): void + { + foreach (scandir(__DIR__ . '/config_mock') as $file) { + if ($file !== '.' && $file !== '..') { + unlink(__DIR__ . '/config_mock/' . $file); + } + } + rmdir(__DIR__ . '/config_mock'); + } + + public function testGetValueWhenKeyContainsDot(): void + { + $this->markTestSkipped('should it be supported?'); + $this->assertEquals('c', self::$config->get('test.a.b')); + $this->assertEquals('d', self::$config->get('test.a.b.c')); + } + + public function testGetBooleanValue(): void + { + $this->assertTrue(self::$config->get('test.boolean')); + } + + public function testGetValue(): void + { + $this->assertSame('bar', self::$config->get('test.foo')); + } + + public function testGetWithDefault(): void + { + $this->assertSame('default', self::$config->get('not_exist', 'default')); + } + + public function testSetValue(): void + { + self::$config->set('key', 'value'); + $this->assertSame('value', self::$config->get('key')); + } + + public function testSetArrayValue(): void + { + self::$config->set('array', ['a', 'b']); + $this->assertSame(['a', 'b'], self::$config->get('array')); + $this->assertSame('a', self::$config->get('array.0')); + } +}