<?php
/**
* Moduł jądra do zarządzania konfiguracją
*
*/
class cConfig implements ArrayAccess
{
private $configuration = array();
/**
* Ładuje plik konfiguracyjny
*
* @param string $fileName
*/
public function load($fileName)
{
$data = new cConfigFile($fileName);
$this -> configuration[$data -> section] = &$data;
}
/**
* Implementacja ArrayAccess
*/
public function offsetExists($key)
{
return (key_exists($key, $this -> configuration
)); }
/**
* Implementacja ArrayAccess
*/
public function &offsetGet($key)
{
return $this -> configuration [$key];
}
/**
* Implementacja ArrayAccess
*/
public function offsetSet($key, $value)
{
$this -> configuration [$key] = $value;
}
/**
* Implementacja ArrayAccess
*/
public function offsetUnset ($key)
{
unset($this -> configuration
[$key]); }
}
class cConfigFile implements ArrayAccess
{
private $sectionName;
private $fileName;
/**
* Tablica zawierająca konfigurację
*
* Tablica może być 'wielopoziomowa'
*
* @var array
*/
private $params = array();
/**
* Konstruktor. Wczytuje plik konfiguracyjny do obiektu
*
* Sprawdza najpierw czy plik może nie jest już w cache,
* wtedy można ominąć powolne parsowanie pliku XML.
* W przeciwnym wypadku wczytujemy plik i uruchamiamy parser
* XML.
* Użyłem fopen+fread, ponieważ z mojego benchmarka wyniklo,
* że dla małych plików file_get_contens() jest wolniejsze.
*
*
* @param string $fileName
* @todo działanie, gdy nie można odczytać pliku.
*
*/
public function __construct($fileName)
{
{
$app = cKernel::getInstance();
$cacheID = $app['cache'] -> makeFileID ('ConfigFile', $fileName);
if ($app['cache'] -> cacheExists($cacheID) && _PHPEAX_CACHE_ON)
{
$data = &$app['cache'] -> fromCache($cacheID);
}
else
{
$file_handler = fopen($fileName,'r'); if (_PHPEAX_CACHE_ON)
{
$app['cache'] -> toCache($cacheID, &$data);
}
}
$this -> sectionName = $data ['section'];
$this -> params = &$data ['config'];
$this -> fileName = $fileName;
}
}
/**
* Implementacja ArrayAccess
*/
public function offsetExists($key)
{
}
/**
* Implementacja ArrayAccess
*/
public function &offsetGet($key)
{
return $this -> params [$key];
}
/**
* Implementacja ArrayAccess
*/
public function offsetSet($key, $value)
{
$this -> params [$key] = $value;
}
/**
* Implementacja ArrayAccess
*/
public function offsetUnset ($key)
{
unset($this -> params
[$key]); }
/**
* Zapisuje konfigurację do pliku
*
* Do przetestowania.
*
*/
public function save()
{
$app = cKernel::getInstance();
$config['section'] = $this -> sectionName;
$config['config'] = $this -> params;
$serialized = $app['xml'] -> serialize($config); file_put_contents($this -> fileName, $serialized);
}
/**
* Przeciążenie właściwości
*
* cConfigFile::sectionName nie jest public, ponieważ
* ma być tylko do odczytu.
*
* @param string $property
* @return unknown
*/
public function __get($property)
{
{
case 'section':
return $this -> sectionName;
break;
default:
return null;
break;
}
}
}
?>