diff --git a/src/Config.php b/src/Config.php index a0329f45..ac678aff 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,4 +3,50 @@ namespace Szurubooru; final class Config extends \ArrayObject { + public function __construct(array $configPaths = []) + { + parent::setFlags(parent::ARRAY_AS_PROPS | parent::STD_PROP_LIST); + + foreach ($configPaths as $configPath) + { + if (file_exists($configPath)) + $this->loadFromIni($configPath); + } + } + + public function offsetGet($index) + { + if (!parent::offsetExists($index)) + return null; + return parent::offsetGet($index); + } + + public function loadFromIni($configPath) + { + $array = parse_ini_file($configPath, true, INI_SCANNER_RAW); + + foreach ($array as $key => $value) + { + if (!is_array($value)) + { + $this->$key = $value; + } + else + { + $section = $key; + $ptr = $this; + + foreach (explode('.', $section) as $subSection) + { + if (!isset($ptr->$subSection)) + $ptr->$subSection = new self(); + + $ptr = $ptr->$subSection; + } + + foreach ($value as $sectionKey => $sectionValue) + $ptr->$sectionKey = $sectionValue; + } + } + } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 00000000..1806bbd0 --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,85 @@ +assertEquals('value', $config->test); + } + + public function testReadingUnnestedSections() + { + file_put_contents(self::$testFileName1, '[test]' . PHP_EOL . 'key=value'); + $config = new \Szurubooru\Config([self::$testFileName1]); + $this->assertEquals('value', $config->test->key); + } + + public function testReadingNestedSections() + { + file_put_contents(self::$testFileName1, '[test.subtest]' . PHP_EOL . 'key=value'); + $config = new \Szurubooru\Config([self::$testFileName1]); + $this->assertEquals('value', $config->test->subtest->key); + } + + public function testReadingMultipleNestedSections() + { + file_put_contents( + self::$testFileName1, + '[test.subtest]' . PHP_EOL . 'key=value' . PHP_EOL . + '[test.subtest.deeptest]' . PHP_EOL . 'key=zombie'); + $config = new \Szurubooru\Config([self::$testFileName1]); + $this->assertEquals('value', $config->test->subtest->key); + $this->assertEquals('zombie', $config->test->subtest->deeptest->key); + } + + public function testReadingNonExistentFiles() + { + $config = new \Szurubooru\Config([self::$testFileName1]); + $this->assertEquals(0, count((array) $config)); + } + + public function testMultipleFiles() + { + file_put_contents(self::$testFileName1, 'test=trash'); + file_put_contents(self::$testFileName2, 'test=overridden'); + $config = new \Szurubooru\Config([self::$testFileName1, self::$testFileName2]); + $this->assertEquals('overridden', $config->test); + } + + public function testReadingUnexistingProperties() + { + file_put_contents(self::$testFileName1, 'meh=value'); + $config = new \Szurubooru\Config([self::$testFileName1]); + $this->assertNull($config->unexistingSection); + } + + public function testOverwritingValues() + { + file_put_contents(self::$testFileName1, 'meh=value'); + $config = new \Szurubooru\Config([self::$testFileName1]); + $config->newKey = 'fast'; + $this->assertEquals('fast', $config->newKey); + } + + protected function tearDown() + { + foreach ([self::$testFileName1, self::$testFileName2] as $temporaryFileName) + { + if (file_exists($temporaryFileName)) + unlink($temporaryFileName); + } + } + +}