phplibrary/src/settings.php
2017-02-09 14:57:40 +03:00

165 lines
4.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
}
}