phplibrary/core/settings.php
2016-06-29 18:51:32 +03:00

167 lines
4 KiB
PHP

<?php
require_once 'core/collection.php';
/**
* Êëàññ ðååñòðà
* Ðååñòð îðãàíèçîâàí êàê àññîöèàòèâíûé ìíîãîìåðíûé ìàññèâ
* array( 'name1' => parameters1, 'name2' => parameters1, ... )
*
* name1, name2 ... - Èìåíà ìîäóëåé
* parameters1, parameters1 - Ìàññèâû ñ ïàðàìåòðàìè ìîäóëÿ
* Èìÿ íåîáõîäèìî ÷òîáû ïîòîì ëåãêî áûëî óäàëèòü íåíóæíûå âåòêè äåðåâà
*/
class Settings extends Collection
{
protected $file;
public function __construct ($file = null)
{
$this->file = $file;
}
/**
* ×òåíèå íàñòðîåê èç ôàéëà
*
* @param File $file
*
* @return Boolean
*/
public function read()
{
if ( !file_exists ($this->file)) return false;
// Íå include_once ò.ê ÷èòàòü íàñòðîéêè ìîæíî íåñêîëüêî ðàç
include($this->file);
if (!is_array($settings)) {
throw new Exception($this->file);
}
return $this->import($settings);
}
/**
* Çàïèñü êëþ÷à â ðååñòð (Ðååñòð ýòî ìîãîìåðíûé ìàññèâ)
*/
public function writeKey(array $key, $value)
{
// assert(count($key) >= 1);
$data = &$this->data;
while (count($key) > 1) {
$name = array_shift($key);
$data = &$data[$name];
}
// assert(count($key) == 1);
$name = array_shift($key);
if (is_array($value)) {
if (! isset($data[$name])) $data[$name] = array();
$this->merge($data[$name], $value);
} else {
$data[$name] = $value;
}
}
/**
* Îáíîâëÿåò ìàññèâ â ñîîòâåòñòâèè ñî çíà÷åíèåì
*/
protected function merge(array &$data, $value)
{
foreach ($value as $key => $subvalue) {
if (is_array($subvalue)) {
if (! isset($data[$key])) $data[$key] = array();
$this->merge($data[$key], $subvalue);
} else {
$data[$key] = $subvalue;
}
}
}
/**
* ×òåíèå êëþ÷à èç ðååñòðà
* @param $args Ïóòü ê çíà÷åíèþ êëþ÷à
*/
public function readKey(array $key)
{
return $this->readKeyData($key, $this->data);
}
protected function readKeyData(array $key, $data)
{
// assert(count($key) >= 1);
while (count($key) > 1) {
$name = array_shift($key);
if (isset($data[$name])) {
$data = $data[$name];
} else {
return null;
}
}
// assert(count($key) == 1);
$name = array_shift($key);
if (isset($data[$name])) {
return $data[$name];
} else {
return null;
}
}
/**
* ×òåíèå êëþ÷à èç ðååñòðà (Ñîáèðàåò âñå êëþ÷è ñ îïðåäåëåííûì çíà÷åíèåì âî âñåõ ìîäóëÿõ)
* @param $key Ïóòü ê çíà÷åíèþ êëþ÷à âíóòðè ìîäóëÿ
*/
public function readKeyList()
{
$key = func_get_args();
$result = array();
foreach ($this->data as $name => $value) {
$output = $this->readKeyData($key, $value);
if ($output) {
$result[$name] = $output;
}
}
return $result;
}
public function removeKey($name)
{
unset($this->data[$name]);
}
public function removeNode(array $key)
{
$data = &$this->data;
while (count($key) > 1) {
$name = array_shift($key);
$data = &$data[$name];
}
$name = array_shift($key);
unset($data[$name]);
}
public function getOwner()
{
return array_keys($this->data);
}
/**
* Çàïèñü íàñòðîåê â ôàéë (Ìîæåò ïåðåèìåíîâàòü â store)
*
* @param File $file
*
* @return void
*/
public function write($file = false)
{
$result = var_export ($this->data, true);
file_put_contents (($file) ? $file : $this->file, "<?php\n\$settings = ".$result.";\n?>");
}
/**
* Ñïèñîê ìîäóëåé
*/
public function getModules()
{
return array_keys($this->data);
}
}