2019-12-10 10:53:31 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: Andrey Mistulov
|
|
|
|
* Company: Aristos
|
|
|
|
* Date: 14.03.2017
|
|
|
|
* Time: 15:25
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Prowebcraft;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Data
|
|
|
|
* @package Aristos
|
|
|
|
*/
|
|
|
|
class JsonDb extends \Prowebcraft\Dot
|
|
|
|
{
|
|
|
|
protected $db = '';
|
|
|
|
protected $data = null;
|
|
|
|
protected $config = [];
|
|
|
|
|
|
|
|
public function __construct($config = [])
|
|
|
|
{
|
|
|
|
$this->config = array_merge([
|
2020-12-25 20:15:56 +01:00
|
|
|
'name' => 'data.json',
|
2022-04-08 14:23:58 +02:00
|
|
|
'backup' => false,
|
|
|
|
'dir' => getcwd()
|
2019-12-10 10:53:31 +01:00
|
|
|
], $config);
|
|
|
|
$this->loadData();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:42:11 +02:00
|
|
|
/**
|
|
|
|
* Reload data from file
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function reload()
|
|
|
|
{
|
|
|
|
$this->loadData(true);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-10 10:53:31 +01:00
|
|
|
/**
|
|
|
|
* Set value or array of values to path
|
|
|
|
*
|
|
|
|
* @param mixed $key Path or array of paths and values
|
|
|
|
* @param mixed|null $value Value to set if path is not an array
|
2022-04-08 11:42:11 +02:00
|
|
|
* @param bool $save Save data to database
|
2019-12-10 10:53:31 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function set($key, $value = null, $save = true)
|
|
|
|
{
|
|
|
|
parent::set($key, $value);
|
2023-03-02 21:07:25 +01:00
|
|
|
if ($save)
|
|
|
|
$this->save();
|
2019-12-10 10:53:31 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add value or array of values to path
|
|
|
|
*
|
2022-04-08 11:42:11 +02:00
|
|
|
* @param mixed $key Path or array of paths and values
|
2019-12-10 10:53:31 +01:00
|
|
|
* @param mixed|null $value Value to set if path is not an array
|
2022-04-08 11:42:11 +02:00
|
|
|
* @param boolean $pop Helper to pop out last key if value is an array
|
|
|
|
* @param bool $save Save data to database
|
2019-12-10 10:53:31 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function add($key, $value = null, $pop = false, $save = true)
|
|
|
|
{
|
|
|
|
parent::add($key, $value, $pop);
|
2023-03-02 21:07:25 +01:00
|
|
|
if ($save)
|
|
|
|
$this->save();
|
2019-12-10 10:53:31 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete path or array of paths
|
|
|
|
*
|
2022-04-08 11:42:11 +02:00
|
|
|
* @param mixed $key Path or array of paths to delete
|
|
|
|
* @param bool $save Save data to database
|
|
|
|
* @return $thisurn $this
|
2019-12-10 10:53:31 +01:00
|
|
|
*/
|
|
|
|
public function delete($key, $save = true)
|
|
|
|
{
|
|
|
|
parent::delete($key);
|
2023-03-02 21:07:25 +01:00
|
|
|
if ($save)
|
|
|
|
$this->save();
|
2019-12-10 10:53:31 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all data, data from path or array of paths and
|
|
|
|
* optionally format path if it doesn't exist
|
|
|
|
*
|
|
|
|
* @param mixed|null $key Path or array of paths to clean
|
2022-04-08 11:42:11 +02:00
|
|
|
* @param boolean $format Format option
|
|
|
|
* @param bool $save Save data to database
|
2019-12-10 10:53:31 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function clear($key = null, $format = false, $save = true)
|
|
|
|
{
|
|
|
|
parent::clear($key, $format);
|
2023-03-02 21:07:25 +01:00
|
|
|
if ($save)
|
|
|
|
$this->save();
|
2019-12-10 10:53:31 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-04-08 11:42:11 +02:00
|
|
|
* Local database upload
|
|
|
|
* @param bool $reload Reboot data?
|
2019-12-10 10:53:31 +01:00
|
|
|
* @return array|mixed|null
|
|
|
|
*/
|
2023-03-02 21:07:25 +01:00
|
|
|
protected function loadData($reload = false)
|
|
|
|
{
|
2019-12-10 10:53:31 +01:00
|
|
|
if ($this->data === null || $reload) {
|
2023-04-26 21:40:09 +02:00
|
|
|
$this->db = $this->config['dir'] . $this->config['name'];
|
2019-12-10 10:53:31 +01:00
|
|
|
if (!file_exists($this->db)) {
|
2022-04-08 14:23:58 +02:00
|
|
|
return null; // Rebuild database manage by CMS
|
2022-03-27 18:13:43 +02:00
|
|
|
} else {
|
|
|
|
if ($this->config['backup']) {
|
2023-03-02 21:07:25 +01:00
|
|
|
try {
|
|
|
|
copy($this->config['dir'] . DIRECTORY_SEPARATOR . $this->config['name'], $this->config['dir'] . DIRECTORY_SEPARATOR . $this->config['name'] . '.backup');
|
|
|
|
} catch (\Exception $e) {
|
2024-03-30 09:16:31 +01:00
|
|
|
error_log('Erreur de chargement : ' . $e);
|
|
|
|
exit('Erreur de chargement : ' . $e);
|
2023-03-02 21:07:25 +01:00
|
|
|
}
|
2019-12-10 10:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 18:13:43 +02:00
|
|
|
$this->data = json_decode(file_get_contents($this->db), true);
|
2024-04-03 12:56:06 +02:00
|
|
|
if (!$this->data === null) {
|
|
|
|
throw new \InvalidArgumentException('Database file ' . $this->db
|
|
|
|
. ' contains invalid json object. Please validate or remove file');
|
2022-03-27 18:13:43 +02:00
|
|
|
}
|
2019-12-10 10:53:31 +01:00
|
|
|
}
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 14:23:58 +02:00
|
|
|
* Save database
|
2019-12-10 10:53:31 +01:00
|
|
|
*/
|
2023-03-02 21:07:25 +01:00
|
|
|
public function save()
|
|
|
|
{
|
2024-04-03 12:56:06 +02:00
|
|
|
$v = json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT );
|
2024-03-14 19:14:13 +01:00
|
|
|
// $v = json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
|
2023-03-10 13:48:27 +01:00
|
|
|
$l = strlen($v);
|
|
|
|
$t = 0;
|
|
|
|
while ($t < 5) {
|
|
|
|
$w = file_put_contents($this->db, $v); // Multi user get a locker
|
|
|
|
if ($w == $l) {
|
2023-02-01 21:16:55 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-04-26 21:40:09 +02:00
|
|
|
$t++;
|
2023-02-01 21:16:55 +01:00
|
|
|
}
|
2023-03-10 13:48:27 +01:00
|
|
|
if ($w !== $l) {
|
2024-02-06 20:08:40 +01:00
|
|
|
error_log('Erreur d\'écriture, les données n\'ont pas été sauvegardées.');
|
|
|
|
exit('Erreur d\'écriture, les données n\'ont pas été sauvegardées.');
|
2023-02-01 21:16:55 +01:00
|
|
|
}
|
|
|
|
|
2019-12-10 10:53:31 +01:00
|
|
|
}
|
2023-03-02 21:07:25 +01:00
|
|
|
}
|