Синхронизировал частично с CMS2

This commit is contained in:
origami11 2017-02-09 17:14:11 +03:00
parent 6b412f5d6f
commit f2938b1353
30 changed files with 1447 additions and 1099 deletions

View file

@ -12,23 +12,31 @@
class Settings extends Collection
{
protected $file;
protected $format = 'php';
public function __construct ($file = null)
{
$this->format = pathinfo($file, PATHINFO_EXTENSION);
$this->file = $file;
}
/**
* Чтение настроек из файла
*
* @param File $file
*
* @return Boolean
*/
public function read()
{
if ( !file_exists ($this->file)) return false;
if (!file_exists ($this->file)) {
return false;
}
// Не include_once т.к читать настройки можно несколько раз
include($this->file);
$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);
}
@ -51,7 +59,9 @@ class Settings extends Collection
// assert(count($key) == 1);
$name = array_shift($key);
if (is_array($value)) {
if (! isset($data[$name])) $data[$name] = array();
if (!isset($data[$name])) {
$data[$name] = array();
}
$this->merge($data[$name], $value);
} else {
$data[$name] = $value;
@ -107,7 +117,7 @@ class Settings extends Collection
* Чтение ключа из реестра (Собирает все ключи с определенным значением во всех модулях)
* @param $key Путь к значению ключа внутри модуля
*/
public function readKeyList()
public function readKeyList($_rest)
{
$key = func_get_args();
$result = array();
@ -149,10 +159,15 @@ class Settings extends Collection
*
* @return void
*/
public function write($file = false)
public function write($file = null)
{
$result = var_export ($this->data, true);
file_put_contents (($file) ? $file : $this->file, "<?php\n\$settings = ".$result.";\n?>");
if ($this->format == 'json') {
$result = json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {
$result = var_export($this->data, true);
$result = "<?php\n\$settings = ".$result.";\n?>";
}
file_put_contents (($file) ? $file : $this->file, $result);
}
/**