fix Не записывать файл если он небыл прочтен, иначе можем потерять данные

This commit is contained in:
origami11@yandex.ru 2023-02-16 16:18:29 +03:00
parent fff5b7b5c4
commit 8321ec244f

View file

@ -18,6 +18,7 @@ class Settings
public $data = [];
protected $file;
protected $format = 'php';
protected $is_read = false;
public function __construct ($file = null, $format = false)
{
@ -35,6 +36,7 @@ class Settings
public function read()
{
if (!file_exists ($this->file)) {
$this->is_read = true;
return false;
}
// Не include_once т.к читать настройки можно несколько раз
@ -49,6 +51,7 @@ class Settings
throw new Exception('no data in ' . $this->file);
}
$this->is_read = true;
$this->data = $settings;
return true;
}
@ -165,6 +168,10 @@ class Settings
*/
public function write($file = null)
{
if (!$this->is_read) {
throw new Exception('read settings before write');
}
if ($this->format == 'json') {
$result = json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {