Config can now parse .ini files

This commit is contained in:
Marcin Kurczewski 2014-08-30 15:03:02 +02:00
parent ebc4fbba61
commit 78f57e5fc4
2 changed files with 131 additions and 0 deletions

View file

@ -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;
}
}
}
}

85
tests/ConfigTest.php Normal file
View file

@ -0,0 +1,85 @@
<?php
namespace Szurubooru\Tests;
final class ConfigTest extends \PHPUnit_Framework_TestCase
{
private static $testFileName1;
private static $testFileName2;
public function setUp()
{
self::$testFileName1 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-config1.ini';
self::$testFileName2 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-config2.ini';
}
public function testReadingNonSections()
{
file_put_contents(self::$testFileName1, 'test=value');
$config = new \Szurubooru\Config([self::$testFileName1]);
$this->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);
}
}
}