fix Не записывать файл если он небыл прочтен, иначе можем потерять данные
This commit is contained in:
parent
fff5b7b5c4
commit
8321ec244f
1 changed files with 7 additions and 0 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue