2014-08-30 12:07:34 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru;
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
class Config extends \ArrayObject
|
2014-08-30 12:07:34 +02:00
|
|
|
{
|
2014-09-10 17:41:47 +02:00
|
|
|
private $dataDirectory;
|
2014-09-17 14:32:26 +02:00
|
|
|
private $publicDataDirectory;
|
2014-09-10 17:41:47 +02:00
|
|
|
|
2014-09-17 14:32:26 +02:00
|
|
|
public function __construct($dataDirectory, $publicDataDirectory)
|
2014-08-30 15:03:02 +02:00
|
|
|
{
|
2014-09-10 17:41:47 +02:00
|
|
|
$this->setFlags($this->getArrayObjectFlags());
|
|
|
|
$this->dataDirectory = $dataDirectory;
|
2014-09-17 14:32:26 +02:00
|
|
|
$this->publicDataDirectory = $publicDataDirectory;
|
2014-09-10 17:41:47 +02:00
|
|
|
$this->tryLoadFromIni([
|
|
|
|
$dataDirectory . DIRECTORY_SEPARATOR . 'config.ini',
|
|
|
|
$dataDirectory . DIRECTORY_SEPARATOR . 'local.ini']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tryLoadFromIni($configPaths)
|
|
|
|
{
|
|
|
|
if (!is_array($configPaths))
|
|
|
|
$configPaths = [$configPaths];
|
2014-08-30 15:03:02 +02:00
|
|
|
|
|
|
|
foreach ($configPaths as $configPath)
|
|
|
|
{
|
|
|
|
if (file_exists($configPath))
|
|
|
|
$this->loadFromIni($configPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:41:47 +02:00
|
|
|
public function getDataDirectory()
|
|
|
|
{
|
|
|
|
return $this->dataDirectory;
|
|
|
|
}
|
|
|
|
|
2014-09-17 14:32:26 +02:00
|
|
|
public function getPublicDataDirectory()
|
|
|
|
{
|
|
|
|
return $this->publicDataDirectory;
|
|
|
|
}
|
|
|
|
|
2014-08-30 15:03:02 +02:00
|
|
|
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))
|
|
|
|
{
|
2014-09-10 17:41:47 +02:00
|
|
|
$this->offsetSet($key, $value);
|
2014-08-30 15:03:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$section = $key;
|
|
|
|
$ptr = $this;
|
|
|
|
|
|
|
|
foreach (explode('.', $section) as $subSection)
|
|
|
|
{
|
2014-09-09 12:34:57 +02:00
|
|
|
if (!$ptr->offsetExists($subSection))
|
2014-09-10 17:41:47 +02:00
|
|
|
$ptr->offsetSet($subSection, new \ArrayObject([], $this->getArrayObjectFlags()));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 17:41:47 +02:00
|
|
|
|
|
|
|
private function getArrayObjectFlags()
|
|
|
|
{
|
|
|
|
return parent::ARRAY_AS_PROPS | parent::STD_PROP_LIST;
|
|
|
|
}
|
2014-08-30 12:07:34 +02:00
|
|
|
}
|