Finstone installé - import et lecture écriture
This commit is contained in:
parent
d181895cef
commit
4e1d38d7ec
251
core/core.php
251
core/core.php
@ -14,6 +14,12 @@
|
|||||||
* @link http://zwiicms.com/
|
* @link http://zwiicms.com/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Chargement de classes personnalisées
|
||||||
|
require "core/vendor/sitemap/SitemapGenerator.php";
|
||||||
|
require "core/vendor/flintstone/autoload.php";
|
||||||
|
require "core/vendor/phpmailer/phpmailer.php";
|
||||||
|
require "core/vendor/phpmailer/exception.php";
|
||||||
|
|
||||||
|
|
||||||
class common {
|
class common {
|
||||||
const DISPLAY_RAW = 0;
|
const DISPLAY_RAW = 0;
|
||||||
@ -45,6 +51,14 @@ class common {
|
|||||||
'theme',
|
'theme',
|
||||||
'user'
|
'user'
|
||||||
];
|
];
|
||||||
|
public static $dataStage = [
|
||||||
|
'config',
|
||||||
|
'core',
|
||||||
|
'page',
|
||||||
|
'module',
|
||||||
|
'user',
|
||||||
|
'theme'
|
||||||
|
];
|
||||||
private $data = [];
|
private $data = [];
|
||||||
private $hierarchy = [
|
private $hierarchy = [
|
||||||
'all' => [],
|
'all' => [],
|
||||||
@ -123,6 +137,7 @@ class common {
|
|||||||
* Constructeur commun
|
* Constructeur commun
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
|
||||||
// Extraction des données http
|
// Extraction des données http
|
||||||
if(isset($_POST)) {
|
if(isset($_POST)) {
|
||||||
$this->input['_POST'] = $_POST;
|
$this->input['_POST'] = $_POST;
|
||||||
@ -132,11 +147,15 @@ class common {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Import des données d'une version 8
|
// Import des données d'une version 8
|
||||||
$this->importData();
|
$this->importDataV8();
|
||||||
|
$this->importDataV9();
|
||||||
|
|
||||||
// Génère le fichier de données lorque les deux fichiers sont absents ou seulement le thème est - installation fraîche par défaut
|
// Génère le fichier de données lorque les deux fichiers sont absents ou seulement le thème est - installation fraîche par défaut
|
||||||
if(file_exists(self::DATA_DIR.'core.json') === false OR
|
if(
|
||||||
file_exists(self::DATA_DIR.'theme.json') === false) {
|
file_exists(self::DATA_DIR.'config.json') === false ||
|
||||||
|
file_exists(self::DATA_DIR.'core.json') === false ||
|
||||||
|
file_exists(self::DATA_DIR.'theme.json') === false ||
|
||||||
|
file_exists(self::DATA_DIR.'user.json') === false ) {
|
||||||
include_once('core/module/install/ressource/defaultdata.php');
|
include_once('core/module/install/ressource/defaultdata.php');
|
||||||
$this->setData([install::$defaultData]);
|
$this->setData([install::$defaultData]);
|
||||||
$this->saveData();
|
$this->saveData();
|
||||||
@ -155,7 +174,8 @@ class common {
|
|||||||
// Utilisateur connecté
|
// Utilisateur connecté
|
||||||
if($this->user === []) {
|
if($this->user === []) {
|
||||||
$this->user = $this->getData(['user', $this->getInput('ZWII_USER_ID')]);
|
$this->user = $this->getData(['user', $this->getInput('ZWII_USER_ID')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construit la liste des pages parents/enfants
|
// Construit la liste des pages parents/enfants
|
||||||
if($this->hierarchy['all'] === []) {
|
if($this->hierarchy['all'] === []) {
|
||||||
$pages = helper::arrayCollumn($this->getData(['page']), 'position', 'SORT_ASC');
|
$pages = helper::arrayCollumn($this->getData(['page']), 'position', 'SORT_ASC');
|
||||||
@ -221,61 +241,6 @@ class common {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Lecture des fichiers de données
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function readData() {
|
|
||||||
// Trois tentatives
|
|
||||||
for($i = 0; $i < 3; $i++) {
|
|
||||||
$this->setData([json_decode(file_get_contents(self::DATA_DIR.'core.json'), true) + json_decode(file_get_contents(self::DATA_DIR.'theme.json'), true)]);
|
|
||||||
if($this->data) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
elseif($i === 2) {
|
|
||||||
exit('Unable to read data file.');
|
|
||||||
}
|
|
||||||
// Pause de 10 millisecondes
|
|
||||||
usleep(10000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Import des données de la version 8
|
|
||||||
* Converti un fichier de données data.json puis le renomme
|
|
||||||
*/
|
|
||||||
public function importData() {
|
|
||||||
if(file_exists(self::DATA_DIR.'data.json')) {
|
|
||||||
// Trois tentatives
|
|
||||||
for($i = 0; $i < 3; $i++) {
|
|
||||||
$tempData = [json_decode(file_get_contents(self::DATA_DIR.'data.json'), true)];
|
|
||||||
if($tempData) {
|
|
||||||
for($i = 0; $i < 3; $i++) {
|
|
||||||
if(file_put_contents(self::DATA_DIR.'core.json', json_encode(array_slice($tempData[0],0,5)), LOCK_EX) !== false) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Pause de 10 millisecondes
|
|
||||||
usleep(10000);
|
|
||||||
}
|
|
||||||
for($i = 0; $i < 3; $i++) {
|
|
||||||
if(file_put_contents(self::DATA_DIR.'theme.json', json_encode(array_slice($tempData[0],5)), LOCK_EX) !== false) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Pause de 10 millisecondes
|
|
||||||
usleep(10000);
|
|
||||||
}
|
|
||||||
rename (self::DATA_DIR.'data.json',self::DATA_DIR.'imported_data.json');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
elseif($i === 2) {
|
|
||||||
exit('Unable to read data file.');
|
|
||||||
}
|
|
||||||
// Pause de 10 millisecondes
|
|
||||||
usleep(10000);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ajoute les valeurs en sortie
|
* Ajoute les valeurs en sortie
|
||||||
@ -524,49 +489,140 @@ class common {
|
|||||||
* Enregistre les données dans deux fichiers séparés
|
* Enregistre les données dans deux fichiers séparés
|
||||||
*/
|
*/
|
||||||
public function saveData() {
|
public function saveData() {
|
||||||
// Save config core page module et user
|
|
||||||
// 5 premières clés principales
|
|
||||||
// Trois tentatives
|
|
||||||
|
|
||||||
$users = new Flintstone\Flintstone('users', ['dir' => self::DATA_DIR]);
|
// Langue du frontend en cours d'édition
|
||||||
|
// $lang = $this->readI18nData('frontend');
|
||||||
// Set a key
|
$lang = 'fr';
|
||||||
$users->set('bob', ['email' => 'bob@site.com', 'password' => '123456']);
|
// Sauvegarde par niveau
|
||||||
|
foreach (self::$dataStage as $stageId) {
|
||||||
// Get a key
|
// Stockage dans un sous-dossier localisé
|
||||||
$user = $users->get('bob');
|
// Le dossier de langue existe t-il ?
|
||||||
echo 'Bob, your email is ' . $user['email'];
|
if (!file_exists(self::DATA_DIR . '/' . $lang)) {
|
||||||
|
mkdir (self::DATA_DIR . '/' . $lang);
|
||||||
// Retrieve all key names
|
|
||||||
$keys = $users->getKeys(); // returns array('bob')
|
|
||||||
|
|
||||||
// Retrieve all data
|
|
||||||
$data = $users->getAll(); // returns array('bob' => array('email' => 'bob@site.com', 'password' => '123456'));
|
|
||||||
|
|
||||||
|
|
||||||
for($i = 0; $i < 3; $i++) {
|
|
||||||
if(file_put_contents(self::DATA_DIR.'core.json', json_encode(array_slice($this->getData(),0,5)) , LOCK_EX) !== false) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
// Pause de 10 millisecondes
|
// Sous-dossier localisé
|
||||||
usleep(10000);
|
if ($stageId === 'page' ||
|
||||||
|
$stageId === 'module') {
|
||||||
|
$folder = self::DATA_DIR . $lang . '/';
|
||||||
|
} else {
|
||||||
|
$folder = self::DATA_DIR;
|
||||||
|
}
|
||||||
|
$store[$stageId] = new Flintstone\Flintstone($stageId, [
|
||||||
|
'dir' => $folder,
|
||||||
|
'ext' => 'json',
|
||||||
|
'formatter' => new Flintstone\Formatter\JsonFormatter()
|
||||||
|
]);
|
||||||
|
|
||||||
|
$store[$stageId]->set($stageId,$this->getData([$stageId]));
|
||||||
}
|
}
|
||||||
// Save theme
|
}
|
||||||
// dernière clé principale
|
|
||||||
// Trois tentatives
|
|
||||||
for($i = 0; $i < 3; $i++) {
|
/**
|
||||||
if(file_put_contents(self::DATA_DIR.'theme.json', json_encode(array_slice($this->getData(),5)), LOCK_EX) !== false) {
|
* Lecture des fichiers de données
|
||||||
break;
|
*
|
||||||
|
*/
|
||||||
|
public function readData() {
|
||||||
|
|
||||||
|
// reset du tableau
|
||||||
|
$lang = 'fr';
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
// Boucle des modules
|
||||||
|
foreach (self::$dataStage as $stageId) {
|
||||||
|
// Sauf pour les pages et les modules
|
||||||
|
if ($stageId === 'page' ||
|
||||||
|
$stageId === 'module') {
|
||||||
|
$folder = self::DATA_DIR . $lang . '/';
|
||||||
|
} else {
|
||||||
|
$folder = self::DATA_DIR;
|
||||||
}
|
}
|
||||||
// Pause de 10 millisecondes
|
$store[$stageId] = new Flintstone\Flintstone($stageId, [
|
||||||
usleep(10000);
|
'dir' => $folder,
|
||||||
|
'ext' => 'json',
|
||||||
|
'formatter' => new Flintstone\Formatter\JsonFormatter()
|
||||||
|
]);
|
||||||
|
$tempData = $store[$stageId]->get($stageId);
|
||||||
|
if ($tempData) {
|
||||||
|
$data [$stageId] = $tempData;
|
||||||
|
} else {
|
||||||
|
$data [$stageId] = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import des données de la version 8
|
||||||
|
* Converti un fichier de données data.json puis le renomme
|
||||||
|
*/
|
||||||
|
public function importDataV8() {
|
||||||
|
if(file_exists(self::DATA_DIR.'data.json')) {
|
||||||
|
// Trois tentatives
|
||||||
|
for($i = 0; $i < 3; $i++) {
|
||||||
|
$tempData = [json_decode(file_get_contents(self::DATA_DIR.'data.json'), true)];
|
||||||
|
if($tempData) {
|
||||||
|
for($i = 0; $i < 3; $i++) {
|
||||||
|
if(file_put_contents(self::DATA_DIR.'core.json', json_encode(array_slice($tempData[0],0,5)), LOCK_EX) !== false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Pause de 10 millisecondes
|
||||||
|
usleep(10000);
|
||||||
|
}
|
||||||
|
for($i = 0; $i < 3; $i++) {
|
||||||
|
if(file_put_contents(self::DATA_DIR.'theme.json', json_encode(array_slice($tempData[0],5)), LOCK_EX) !== false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Pause de 10 millisecondes
|
||||||
|
usleep(10000);
|
||||||
|
}
|
||||||
|
rename (self::DATA_DIR.'data.json',self::DATA_DIR.'imported_data.json');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
elseif($i === 2) {
|
||||||
|
exit('Unable to read data file.');
|
||||||
|
}
|
||||||
|
// Pause de 10 millisecondes
|
||||||
|
usleep(10000);
|
||||||
|
}
|
||||||
|
$this->saveData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import des données de la version 9
|
||||||
|
* Converti un fichier de données data.json puis le renomme
|
||||||
|
*/
|
||||||
|
public function importDataV9() {
|
||||||
|
|
||||||
|
// Détecter les fichiers d'une V9
|
||||||
|
if (file_exists(self::DATA_DIR . 'core.php') &&
|
||||||
|
file_exists(self::DATA_DIR . 'theme.php') &&
|
||||||
|
!file_exists(self::DATA_DIR . 'config.php') &&
|
||||||
|
!file_exists(self::DATA_DIR . 'user.php') ) {
|
||||||
|
|
||||||
|
// Trois tentatives
|
||||||
|
for($i = 0; $i < 3; $i++) {
|
||||||
|
$this->setData([json_decode(file_get_contents(self::DATA_DIR.'core.json'), true) + json_decode(file_get_contents(self::DATA_DIR.'theme.json'), true)]);
|
||||||
|
if($this->data) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
elseif($i === 2) {
|
||||||
|
exit('Unable to import data file.');
|
||||||
|
}
|
||||||
|
// Pause de 10 millisecondes
|
||||||
|
usleep(10000);
|
||||||
|
}
|
||||||
|
rename (self::DATA_DIR.'data.json',self::DATA_DIR.'imported_data.json');
|
||||||
|
rename (self::DATA_DIR.'theme.json',self::DATA_DIR.'imported_theme.json');
|
||||||
|
$this->saveData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Génére un fichier json avec la liste des pages
|
* Génére un fichier json avec la liste des pages
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function pages2Json() {
|
public function pages2Json() {
|
||||||
// Sauve la liste des pages pour TinyMCE
|
// Sauve la liste des pages pour TinyMCE
|
||||||
$parents = [];
|
$parents = [];
|
||||||
@ -753,8 +809,6 @@ $data = $users->getAll(); // returns array('bob' => array('email' => 'bob@site.c
|
|||||||
*/
|
*/
|
||||||
public function sendMail($to, $subject, $content) {
|
public function sendMail($to, $subject, $content) {
|
||||||
// Utilisation de PHPMailer version 6.0.6
|
// Utilisation de PHPMailer version 6.0.6
|
||||||
require "core/vendor/phpmailer/phpmailer.php";
|
|
||||||
require "core/vendor/phpmailer/exception.php";
|
|
||||||
|
|
||||||
// Layout
|
// Layout
|
||||||
ob_start();
|
ob_start();
|
||||||
@ -1157,11 +1211,12 @@ class core extends common {
|
|||||||
require 'core/vendor/' . $classPath;
|
require 'core/vendor/' . $classPath;
|
||||||
}
|
}
|
||||||
// Classes personnalisées
|
// Classes personnalisées
|
||||||
if (!class_exists('flintstone')) {
|
elseif(is_readable('core/vendor/flinstone/' . $classPath)) {
|
||||||
require 'core/vendor/flintstone/flintstone.php'; }
|
require 'core/vendor/flintstone/' . $className . '.php';
|
||||||
|
require 'core/vendor/flintstone/Formatter/' . $className . '.php';
|
||||||
|
}
|
||||||
|
|
||||||
if (!class_exists('sitemapgenerator')) {
|
|
||||||
require_once "core/vendor/sitemap/SitemapGenerator.php"; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
12
core/vendor/flintstone/autoload.php
vendored
Normal file
12
core/vendor/flintstone/autoload.php
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
require 'core/vendor/flintstone/Flintstone.php';
|
||||||
|
require 'core/vendor/flintstone/Config.php';
|
||||||
|
require 'core/vendor/flintstone/Database.php';
|
||||||
|
require 'core/vendor/flintstone/Line.php';
|
||||||
|
require 'core/vendor/flintstone/Validation.php';
|
||||||
|
require 'core/vendor/flintstone/Exception.php';
|
||||||
|
require 'core/vendor/flintstone/Formatter/FormatterInterface.php';
|
||||||
|
require 'core/vendor/flintstone/Formatter/JsonFormatter.php';
|
||||||
|
require 'core/vendor/flintstone/Formatter/SerializeFormatter.php';
|
||||||
|
require 'core/vendor/flintstone/Cache/CacheInterface.php';
|
||||||
|
require 'core/vendor/flintstone/Cache/ArrayCache.php';
|
296
core/vendor/flintstone/flintstone.php
vendored
296
core/vendor/flintstone/flintstone.php
vendored
@ -1,13 +1,285 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once "core/vendor/flintstone/src/Cache/ArrayCache.php";
|
namespace Flintstone;
|
||||||
require_once "core/vendor/flintstone/src/Cache/CacheInterface.php";
|
|
||||||
require_once "core/vendor/flintstone/src/Flintstone.php";
|
class Flintstone
|
||||||
require_once "core/vendor/flintstone/src/Database.php";
|
{
|
||||||
require_once "core/vendor/flintstone/src/Validation.php";
|
/**
|
||||||
require_once "core/vendor/flintstone/src/Config.php";
|
* Flintstone version.
|
||||||
require_once "core/vendor/flintstone/src/Exception.php";
|
*
|
||||||
require_once "core/vendor/flintstone/src/Line.php";
|
* @var string
|
||||||
require_once "core/vendor/flintstone/src/Formatter/JsonFormatter.php";
|
*/
|
||||||
require_once "core/vendor/flintstone/src/Formatter/FormatterInterface.php";
|
const VERSION = '2.2';
|
||||||
require_once "core/vendor/flintstone/src/Formatter/SerializeFormatter.php";
|
|
||||||
|
/**
|
||||||
|
* Database class.
|
||||||
|
*
|
||||||
|
* @var Database
|
||||||
|
*/
|
||||||
|
protected $database;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Config class.
|
||||||
|
*
|
||||||
|
* @var Config
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param Database|string $database
|
||||||
|
* @param Config|array $config
|
||||||
|
*/
|
||||||
|
public function __construct($database, $config)
|
||||||
|
{
|
||||||
|
if (is_string($database)) {
|
||||||
|
$database = new Database($database);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($config)) {
|
||||||
|
$config = new Config($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setDatabase($database);
|
||||||
|
$this->setConfig($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the database.
|
||||||
|
*
|
||||||
|
* @return Database
|
||||||
|
*/
|
||||||
|
public function getDatabase(): Database
|
||||||
|
{
|
||||||
|
return $this->database;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the database.
|
||||||
|
*
|
||||||
|
* @param Database $database
|
||||||
|
*/
|
||||||
|
public function setDatabase(Database $database)
|
||||||
|
{
|
||||||
|
$this->database = $database;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the config.
|
||||||
|
*
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function getConfig(): Config
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the config.
|
||||||
|
*
|
||||||
|
* @param Config $config
|
||||||
|
*/
|
||||||
|
public function setConfig(Config $config)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$this->getDatabase()->setConfig($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a key from the database.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function get(string $key)
|
||||||
|
{
|
||||||
|
Validation::validateKey($key);
|
||||||
|
|
||||||
|
// Fetch the key from cache
|
||||||
|
if ($cache = $this->getConfig()->getCache()) {
|
||||||
|
if ($cache->contains($key)) {
|
||||||
|
return $cache->get($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the key from database
|
||||||
|
$file = $this->getDatabase()->readFromFile();
|
||||||
|
$data = false;
|
||||||
|
|
||||||
|
foreach ($file as $line) {
|
||||||
|
/** @var Line $line */
|
||||||
|
if ($line->getKey() == $key) {
|
||||||
|
$data = $this->decodeData($line->getData());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the data to cache
|
||||||
|
if ($cache && $data !== false) {
|
||||||
|
$cache->set($key, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a key in the database.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $data
|
||||||
|
*/
|
||||||
|
public function set(string $key, $data)
|
||||||
|
{
|
||||||
|
Validation::validateKey($key);
|
||||||
|
|
||||||
|
// If the key already exists we need to replace it
|
||||||
|
if ($this->get($key) !== false) {
|
||||||
|
$this->replace($key, $data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the key to the database
|
||||||
|
$this->getDatabase()->appendToFile($this->getLineString($key, $data));
|
||||||
|
|
||||||
|
// Delete the key from cache
|
||||||
|
if ($cache = $this->getConfig()->getCache()) {
|
||||||
|
$cache->delete($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a key from the database.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function delete(string $key)
|
||||||
|
{
|
||||||
|
Validation::validateKey($key);
|
||||||
|
|
||||||
|
if ($this->get($key) !== false) {
|
||||||
|
$this->replace($key, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flush the database.
|
||||||
|
*/
|
||||||
|
public function flush()
|
||||||
|
{
|
||||||
|
$this->getDatabase()->flushFile();
|
||||||
|
|
||||||
|
// Flush the cache
|
||||||
|
if ($cache = $this->getConfig()->getCache()) {
|
||||||
|
$cache->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all keys from the database.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getKeys(): array
|
||||||
|
{
|
||||||
|
$keys = [];
|
||||||
|
$file = $this->getDatabase()->readFromFile();
|
||||||
|
|
||||||
|
foreach ($file as $line) {
|
||||||
|
/** @var Line $line */
|
||||||
|
$keys[] = $line->getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all data from the database.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAll(): array
|
||||||
|
{
|
||||||
|
$data = [];
|
||||||
|
$file = $this->getDatabase()->readFromFile();
|
||||||
|
|
||||||
|
foreach ($file as $line) {
|
||||||
|
/** @var Line $line */
|
||||||
|
$data[$line->getKey()] = $this->decodeData($line->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace a key in the database.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $data
|
||||||
|
*/
|
||||||
|
protected function replace(string $key, $data)
|
||||||
|
{
|
||||||
|
// Write a new database to a temporary file
|
||||||
|
$tmpFile = $this->getDatabase()->openTempFile();
|
||||||
|
$file = $this->getDatabase()->readFromFile();
|
||||||
|
|
||||||
|
foreach ($file as $line) {
|
||||||
|
/** @var Line $line */
|
||||||
|
if ($line->getKey() == $key) {
|
||||||
|
if ($data !== false) {
|
||||||
|
$tmpFile->fwrite($this->getLineString($key, $data));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$tmpFile->fwrite($line->getLine() . "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$tmpFile->rewind();
|
||||||
|
|
||||||
|
// Overwrite the database with the temporary file
|
||||||
|
$this->getDatabase()->writeTempToFile($tmpFile);
|
||||||
|
|
||||||
|
// Delete the key from cache
|
||||||
|
if ($cache = $this->getConfig()->getCache()) {
|
||||||
|
$cache->delete($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the line string to write.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $data
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getLineString(string $key, $data): string
|
||||||
|
{
|
||||||
|
return $key . '=' . $this->encodeData($data) . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a string into data.
|
||||||
|
*
|
||||||
|
* @param string $data
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function decodeData(string $data)
|
||||||
|
{
|
||||||
|
return $this->getConfig()->getFormatter()->decode($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode data into a string.
|
||||||
|
*
|
||||||
|
* @param mixed $data
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function encodeData($data): string
|
||||||
|
{
|
||||||
|
return $this->getConfig()->getFormatter()->encode($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
285
core/vendor/flintstone/src/Flintstone.php
vendored
285
core/vendor/flintstone/src/Flintstone.php
vendored
@ -1,285 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Flintstone;
|
|
||||||
|
|
||||||
class Flintstone
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Flintstone version.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const VERSION = '2.2';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Database class.
|
|
||||||
*
|
|
||||||
* @var Database
|
|
||||||
*/
|
|
||||||
protected $database;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Config class.
|
|
||||||
*
|
|
||||||
* @var Config
|
|
||||||
*/
|
|
||||||
protected $config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param Database|string $database
|
|
||||||
* @param Config|array $config
|
|
||||||
*/
|
|
||||||
public function __construct($database, $config)
|
|
||||||
{
|
|
||||||
if (is_string($database)) {
|
|
||||||
$database = new Database($database);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_array($config)) {
|
|
||||||
$config = new Config($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setDatabase($database);
|
|
||||||
$this->setConfig($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the database.
|
|
||||||
*
|
|
||||||
* @return Database
|
|
||||||
*/
|
|
||||||
public function getDatabase(): Database
|
|
||||||
{
|
|
||||||
return $this->database;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the database.
|
|
||||||
*
|
|
||||||
* @param Database $database
|
|
||||||
*/
|
|
||||||
public function setDatabase(Database $database)
|
|
||||||
{
|
|
||||||
$this->database = $database;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the config.
|
|
||||||
*
|
|
||||||
* @return Config
|
|
||||||
*/
|
|
||||||
public function getConfig(): Config
|
|
||||||
{
|
|
||||||
return $this->config;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the config.
|
|
||||||
*
|
|
||||||
* @param Config $config
|
|
||||||
*/
|
|
||||||
public function setConfig(Config $config)
|
|
||||||
{
|
|
||||||
$this->config = $config;
|
|
||||||
$this->getDatabase()->setConfig($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a key from the database.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function get(string $key)
|
|
||||||
{
|
|
||||||
Validation::validateKey($key);
|
|
||||||
|
|
||||||
// Fetch the key from cache
|
|
||||||
if ($cache = $this->getConfig()->getCache()) {
|
|
||||||
if ($cache->contains($key)) {
|
|
||||||
return $cache->get($key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch the key from database
|
|
||||||
$file = $this->getDatabase()->readFromFile();
|
|
||||||
$data = false;
|
|
||||||
|
|
||||||
foreach ($file as $line) {
|
|
||||||
/** @var Line $line */
|
|
||||||
if ($line->getKey() == $key) {
|
|
||||||
$data = $this->decodeData($line->getData());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the data to cache
|
|
||||||
if ($cache && $data !== false) {
|
|
||||||
$cache->set($key, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a key in the database.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $data
|
|
||||||
*/
|
|
||||||
public function set(string $key, $data)
|
|
||||||
{
|
|
||||||
Validation::validateKey($key);
|
|
||||||
|
|
||||||
// If the key already exists we need to replace it
|
|
||||||
if ($this->get($key) !== false) {
|
|
||||||
$this->replace($key, $data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the key to the database
|
|
||||||
$this->getDatabase()->appendToFile($this->getLineString($key, $data));
|
|
||||||
|
|
||||||
// Delete the key from cache
|
|
||||||
if ($cache = $this->getConfig()->getCache()) {
|
|
||||||
$cache->delete($key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a key from the database.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
*/
|
|
||||||
public function delete(string $key)
|
|
||||||
{
|
|
||||||
Validation::validateKey($key);
|
|
||||||
|
|
||||||
if ($this->get($key) !== false) {
|
|
||||||
$this->replace($key, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flush the database.
|
|
||||||
*/
|
|
||||||
public function flush()
|
|
||||||
{
|
|
||||||
$this->getDatabase()->flushFile();
|
|
||||||
|
|
||||||
// Flush the cache
|
|
||||||
if ($cache = $this->getConfig()->getCache()) {
|
|
||||||
$cache->flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all keys from the database.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getKeys(): array
|
|
||||||
{
|
|
||||||
$keys = [];
|
|
||||||
$file = $this->getDatabase()->readFromFile();
|
|
||||||
|
|
||||||
foreach ($file as $line) {
|
|
||||||
/** @var Line $line */
|
|
||||||
$keys[] = $line->getKey();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all data from the database.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getAll(): array
|
|
||||||
{
|
|
||||||
$data = [];
|
|
||||||
$file = $this->getDatabase()->readFromFile();
|
|
||||||
|
|
||||||
foreach ($file as $line) {
|
|
||||||
/** @var Line $line */
|
|
||||||
$data[$line->getKey()] = $this->decodeData($line->getData());
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replace a key in the database.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $data
|
|
||||||
*/
|
|
||||||
protected function replace(string $key, $data)
|
|
||||||
{
|
|
||||||
// Write a new database to a temporary file
|
|
||||||
$tmpFile = $this->getDatabase()->openTempFile();
|
|
||||||
$file = $this->getDatabase()->readFromFile();
|
|
||||||
|
|
||||||
foreach ($file as $line) {
|
|
||||||
/** @var Line $line */
|
|
||||||
if ($line->getKey() == $key) {
|
|
||||||
if ($data !== false) {
|
|
||||||
$tmpFile->fwrite($this->getLineString($key, $data));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$tmpFile->fwrite($line->getLine() . "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$tmpFile->rewind();
|
|
||||||
|
|
||||||
// Overwrite the database with the temporary file
|
|
||||||
$this->getDatabase()->writeTempToFile($tmpFile);
|
|
||||||
|
|
||||||
// Delete the key from cache
|
|
||||||
if ($cache = $this->getConfig()->getCache()) {
|
|
||||||
$cache->delete($key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the line string to write.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $data
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getLineString(string $key, $data): string
|
|
||||||
{
|
|
||||||
return $key . '=' . $this->encodeData($data) . "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decode a string into data.
|
|
||||||
*
|
|
||||||
* @param string $data
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
protected function decodeData(string $data)
|
|
||||||
{
|
|
||||||
return $this->getConfig()->getFormatter()->decode($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode data into a string.
|
|
||||||
*
|
|
||||||
* @param mixed $data
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function encodeData($data): string
|
|
||||||
{
|
|
||||||
return $this->getConfig()->getFormatter()->encode($data);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user