szurubooru/src/Config.php

53 lines
1,014 B
PHP
Raw Normal View History

<?php
namespace Szurubooru;
class Config extends \ArrayObject
{
2014-08-30 15:03:02 +02:00
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)
{
2014-09-09 12:34:57 +02:00
if (!$ptr->offsetExists($subSection))
$ptr->offsetSet($subSection, new self());
2014-08-30 15:03:02 +02:00
$ptr = $ptr->$subSection;
}
foreach ($value as $sectionKey => $sectionValue)
2014-09-09 12:34:57 +02:00
$ptr->offsetSet($sectionKey, $sectionValue);
2014-08-30 15:03:02 +02:00
}
}
}
}