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-09-19 16:49:58 +02:00
|
|
|
'name' => 'data.json',
|
|
|
|
'backup' => 5,
|
2019-12-10 10:53:31 +01:00
|
|
|
'dir' => getcwd(),
|
2020-09-19 16:49:58 +02:00
|
|
|
'template' => getcwd() . DIRECTORY_SEPARATOR . 'data.template.json'
|
2019-12-10 10:53:31 +01:00
|
|
|
], $config);
|
|
|
|
$this->loadData();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-09-18 03:55:22 +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);
|
|
|
|
if ($save) $this->save();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add value or array of values to path
|
|
|
|
*
|
2020-09-18 03:55:22 +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
|
2020-09-18 03:55:22 +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);
|
|
|
|
if ($save) $this->save();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete path or array of paths
|
|
|
|
*
|
2020-09-18 03:55:22 +02:00
|
|
|
* @param mixed $key Path or array of paths to delete
|
|
|
|
* @param bool $save Save data to database
|
2019-12-10 10:53:31 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function delete($key, $save = true)
|
|
|
|
{
|
|
|
|
parent::delete($key);
|
|
|
|
if ($save) $this->save();
|
|
|
|
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
|
2020-09-18 03:55:22 +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);
|
|
|
|
if ($save) $this->save();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-18 03:55:22 +02:00
|
|
|
* Local database upload
|
|
|
|
* @param bool $reload Reboot data?
|
2019-12-10 10:53:31 +01:00
|
|
|
* @return array|mixed|null
|
|
|
|
*/
|
|
|
|
protected function loadData($reload = false) {
|
|
|
|
if ($this->data === null || $reload) {
|
2020-09-19 16:49:58 +02:00
|
|
|
// $this->db = $this->config['dir'] . DIRECTORY_SEPARATOR . $this->config['name'];
|
|
|
|
$this->db = $this->config['dir'] . $this->config['name'];
|
2020-11-03 07:48:56 +01:00
|
|
|
|
2019-12-10 10:53:31 +01:00
|
|
|
if (!file_exists($this->db)) {
|
2020-11-03 07:48:56 +01:00
|
|
|
return null;
|
2019-12-10 10:53:31 +01:00
|
|
|
} else {
|
2020-11-03 07:48:56 +01:00
|
|
|
$this->data = json_decode(file_get_contents($this->db), true);
|
|
|
|
if (!$this->data === null) {
|
|
|
|
throw new \InvalidArgumentException('Database file ' . $this->db
|
|
|
|
. ' contains invalid json object. Please validate or remove file');
|
2019-12-10 10:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-18 03:55:22 +02:00
|
|
|
* Saving to local database
|
2019-12-10 10:53:31 +01:00
|
|
|
*/
|
|
|
|
public function save() {
|
|
|
|
file_put_contents($this->db, json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
|
|
|
|
}
|
|
|
|
}
|