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