parameters1, 'name2' => parameters1, ... ) * * name1, name2 ... - Имена модулей * parameters1, parameters1 - Массивы с параметрами модуля * Имя необходимо чтобы потом легко было удалить ненужные ветки дерева */ class Settings extends Collection { protected $file; protected $format = 'php'; public function __construct ($file = null) { $this->format = pathinfo($file, PATHINFO_EXTENSION); $this->file = $file; } /** * Чтение настроек из файла * @return Boolean */ public function read() { if (!file_exists ($this->file)) { return false; } // Не include_once т.к читать настройки можно несколько раз $settings = array(); if ($this->format == 'json') { $settings = json_decode(file_get_contents($this->file), true); } else { 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($_rest) { $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 = null) { if ($this->format == 'json') { $result = json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } else { $result = var_export($this->data, true); $result = ""; } file_put_contents (($file) ? $file : $this->file, $result); } /** * Список модулей */ public function getModules() { return array_keys($this->data); } }