szurubooru/src/Config.php
2014-10-18 18:48:20 +02:00

74 lines
1.5 KiB
PHP

<?php
namespace Szurubooru;
class Config extends \ArrayObject
{
private $dataDirectory;
public function __construct($dataDirectory)
{
$this->setFlags($this->getArrayObjectFlags());
$this->dataDirectory = $dataDirectory;
$this->tryLoadFromIni([
$dataDirectory . DIRECTORY_SEPARATOR . 'config.ini',
$dataDirectory . DIRECTORY_SEPARATOR . 'local.ini']);
}
public function tryLoadFromIni($configPaths)
{
if (!is_array($configPaths))
$configPaths = [$configPaths];
foreach ($configPaths as $configPath)
{
if (file_exists($configPath))
$this->loadFromIni($configPath);
}
}
public function getDataDirectory()
{
return $this->dataDirectory;
}
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->offsetSet($key, $value);
}
else
{
$section = $key;
$ptr = $this;
foreach (explode('.', $section) as $subSection)
{
if (!$ptr->offsetExists($subSection))
$ptr->offsetSet($subSection, new \ArrayObject([], $this->getArrayObjectFlags()));
$ptr = $ptr->$subSection;
}
foreach ($value as $sectionKey => $sectionValue)
$ptr->offsetSet($sectionKey, $sectionValue);
}
}
}
private function getArrayObjectFlags()
{
return parent::ARRAY_AS_PROPS | parent::STD_PROP_LIST;
}
}