From 70c647ac971b9bd0237c8fd0388cece399a7b7d4 Mon Sep 17 00:00:00 2001 From: fredtempez Date: Thu, 8 Aug 2019 18:08:42 +0200 Subject: [PATCH] [9.3.00] nouvelle classe de gestion des json --- core/core.php | 53 +-- core/vendor/autoload.php | 9 + core/vendor/flintstone/Cache/ArrayCache.php | 53 --- .../flintstone/Cache/CacheInterface.php | 44 --- core/vendor/flintstone/Config.php | 209 ------------ core/vendor/flintstone/Database.php | 255 -------------- core/vendor/flintstone/Exception.php | 7 - .../Formatter/FormatterInterface.php | 24 -- .../flintstone/Formatter/JsonFormatter.php | 46 --- .../Formatter/SerializeFormatter.php | 52 --- core/vendor/flintstone/Line.php | 37 -- core/vendor/flintstone/Validation.php | 34 -- core/vendor/flintstone/autoload.php | 12 - core/vendor/flintstone/flintstone.php | 285 ---------------- core/vendor/jsondb/Dot.php | 316 ++++++++++++++++++ core/vendor/jsondb/JsonDb.php | 138 ++++++++ index.php | 6 +- 17 files changed, 495 insertions(+), 1085 deletions(-) create mode 100644 core/vendor/autoload.php delete mode 100644 core/vendor/flintstone/Cache/ArrayCache.php delete mode 100644 core/vendor/flintstone/Cache/CacheInterface.php delete mode 100644 core/vendor/flintstone/Config.php delete mode 100644 core/vendor/flintstone/Database.php delete mode 100644 core/vendor/flintstone/Exception.php delete mode 100644 core/vendor/flintstone/Formatter/FormatterInterface.php delete mode 100644 core/vendor/flintstone/Formatter/JsonFormatter.php delete mode 100644 core/vendor/flintstone/Formatter/SerializeFormatter.php delete mode 100644 core/vendor/flintstone/Line.php delete mode 100644 core/vendor/flintstone/Validation.php delete mode 100644 core/vendor/flintstone/autoload.php delete mode 100644 core/vendor/flintstone/flintstone.php create mode 100644 core/vendor/jsondb/Dot.php create mode 100644 core/vendor/jsondb/JsonDb.php diff --git a/core/core.php b/core/core.php index 12657e27..44a1fe8c 100644 --- a/core/core.php +++ b/core/core.php @@ -14,13 +14,10 @@ * @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"; - + // Classes personnalisées +require "core/vendor/autoload.php"; +// Classe internes class common { const DISPLAY_RAW = 0; const DISPLAY_JSON = 1; @@ -507,13 +504,20 @@ class common { } else { $folder = self::DATA_DIR; } - $store[$stageId] = new Flintstone\Flintstone($stageId, [ - 'dir' => $folder, - 'ext' => 'json', - 'formatter' => new Flintstone\Formatter\JsonFormatter() - ]); + //$store[$stageId] = new Flintstone\Flintstone($stageId, [ + // 'dir' => $folder, + // 'ext' => 'json', + // 'formatter' => new Flintstone\Formatter\JsonFormatter() + // ]); - $store[$stageId]->set($stageId,$this->getData([$stageId])); + //$store[$stageId]->set($stageId,$this->getData([$stageId])); + + $db[$stageId] = new \Prowebcraft\JsonDb([ + 'name' => $stageId . '.json', + 'dir' => $folder + ]); + $db[$stageId]->set($stageId,$this->getData([$stageId])); + $db[$stageId]->save; } } @@ -537,12 +541,18 @@ class common { } else { $folder = self::DATA_DIR; } - $store[$stageId] = new Flintstone\Flintstone($stageId, [ - 'dir' => $folder, - 'ext' => 'json', - 'formatter' => new Flintstone\Formatter\JsonFormatter() - ]); - $tempData = $store[$stageId]->get($stageId); + //$store[$stageId] = new Flintstone\Flintstone($stageId, [ + // 'dir' => $folder, + // 'ext' => 'json', + // 'formatter' => new Flintstone\Formatter\JsonFormatter() + //]); + $db[$stageId] = new \Prowebcraft\JsonDb([ + 'name' => $stageId . '.json', + 'dir' => $folder + ]); + + //$tempData = $store[$stageId]->get($stageId); + $tempData = $db[$stageId]->get($stageId); if ($tempData) { $data [$stageId] = $tempData; } else { @@ -1209,13 +1219,6 @@ class core extends common { elseif(is_readable('core/vendor/' . $classPath)) { require 'core/vendor/' . $classPath; } - // Classes personnalisées - elseif(is_readable('core/vendor/flinstone/' . $classPath)) { - require 'core/vendor/flintstone/' . $className . '.php'; - require 'core/vendor/flintstone/Formatter/' . $className . '.php'; - } - - } /** diff --git a/core/vendor/autoload.php b/core/vendor/autoload.php new file mode 100644 index 00000000..c219b803 --- /dev/null +++ b/core/vendor/autoload.php @@ -0,0 +1,9 @@ +cache); - } - - /** - * {@inheritdoc} - */ - public function get($key) - { - return $this->cache[$key]; - } - - /** - * {@inheritdoc} - */ - public function set($key, $data) - { - $this->cache[$key] = $data; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - unset($this->cache[$key]); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - $this->cache = []; - } -} diff --git a/core/vendor/flintstone/Cache/CacheInterface.php b/core/vendor/flintstone/Cache/CacheInterface.php deleted file mode 100644 index 9459e010..00000000 --- a/core/vendor/flintstone/Cache/CacheInterface.php +++ /dev/null @@ -1,44 +0,0 @@ -normalizeConfig($config); - $this->setDir($config['dir']); - $this->setExt($config['ext']); - $this->setGzip($config['gzip']); - $this->setCache($config['cache']); - $this->setFormatter($config['formatter']); - $this->setSwapMemoryLimit($config['swap_memory_limit']); - } - - /** - * Normalize the user supplied config. - * - * @param array $config - * - * @return array - */ - protected function normalizeConfig(array $config): array - { - $defaultConfig = [ - 'dir' => getcwd(), - 'ext' => '.dat', - 'gzip' => false, - 'cache' => true, - 'formatter' => null, - 'swap_memory_limit' => 2097152, // 2MB - ]; - - return array_replace($defaultConfig, $config); - } - - /** - * Get the dir. - * - * @return string - */ - public function getDir(): string - { - return $this->config['dir']; - } - - /** - * Set the dir. - * - * @param string $dir - * - * @throws Exception - */ - public function setDir(string $dir) - { - if (!is_dir($dir)) { - throw new Exception('Directory does not exist: ' . $dir); - } - - $this->config['dir'] = rtrim($dir, '/\\') . DIRECTORY_SEPARATOR; - } - - /** - * Get the ext. - * - * @return string - */ - public function getExt(): string - { - if ($this->useGzip()) { - return $this->config['ext'] . '.gz'; - } - - return $this->config['ext']; - } - - /** - * Set the ext. - * - * @param string $ext - */ - public function setExt(string $ext) - { - if (substr($ext, 0, 1) !== '.') { - $ext = '.' . $ext; - } - - $this->config['ext'] = $ext; - } - - /** - * Use gzip? - * - * @return bool - */ - public function useGzip(): bool - { - return $this->config['gzip']; - } - - /** - * Set gzip. - * - * @param bool $gzip - */ - public function setGzip(bool $gzip) - { - $this->config['gzip'] = $gzip; - } - - /** - * Get the cache. - * - * @return CacheInterface|false - */ - public function getCache() - { - return $this->config['cache']; - } - - /** - * Set the cache. - * - * @param mixed $cache - * - * @throws Exception - */ - public function setCache($cache) - { - if (!is_bool($cache) && !$cache instanceof CacheInterface) { - throw new Exception('Cache must be a boolean or an instance of Flintstone\Cache\CacheInterface'); - } - - if ($cache === true) { - $cache = new ArrayCache(); - } - - $this->config['cache'] = $cache; - } - - /** - * Get the formatter. - * - * @return FormatterInterface - */ - public function getFormatter(): FormatterInterface - { - return $this->config['formatter']; - } - - /** - * Set the formatter. - * - * @param FormatterInterface|null $formatter - * - * @throws Exception - */ - public function setFormatter($formatter) - { - if ($formatter === null) { - $formatter = new SerializeFormatter(); - } - - if (!$formatter instanceof FormatterInterface) { - throw new Exception('Formatter must be an instance of Flintstone\Formatter\FormatterInterface'); - } - - $this->config['formatter'] = $formatter; - } - - /** - * Get the swap memory limit. - * - * @return int - */ - public function getSwapMemoryLimit(): int - { - return $this->config['swap_memory_limit']; - } - - /** - * Set the swap memory limit. - * - * @param int $limit - */ - public function setSwapMemoryLimit(int $limit) - { - $this->config['swap_memory_limit'] = $limit; - } -} diff --git a/core/vendor/flintstone/Database.php b/core/vendor/flintstone/Database.php deleted file mode 100644 index 08deb521..00000000 --- a/core/vendor/flintstone/Database.php +++ /dev/null @@ -1,255 +0,0 @@ - [ - 'mode' => 'rb', - 'operation' => LOCK_SH, - ], - self::FILE_WRITE => [ - 'mode' => 'wb', - 'operation' => LOCK_EX, - ], - self::FILE_APPEND => [ - 'mode' => 'ab', - 'operation' => LOCK_EX, - ], - ]; - - /** - * Database name. - * - * @var string - */ - protected $name; - - /** - * Config class. - * - * @var Config - */ - protected $config; - - /** - * Constructor. - * - * @param string $name - * @param Config|null $config - */ - public function __construct(string $name, Config $config = null) - { - $this->setName($name); - - if ($config) { - $this->setConfig($config); - } - } - - /** - * Get the database name. - * - * @return string - */ - public function getName(): string - { - return $this->name; - } - - /** - * Set the database name. - * - * @param string $name - * - * @throws Exception - */ - public function setName(string $name) - { - Validation::validateDatabaseName($name); - $this->name = $name; - } - - /** - * 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; - } - - /** - * Get the path to the database file. - * - * @return string - */ - public function getPath(): string - { - return $this->config->getDir() . $this->getName() . $this->config->getExt(); - } - - /** - * Open the database file. - * - * @param int $mode - * - * @throws Exception - * - * @return SplFileObject - */ - protected function openFile(int $mode): SplFileObject - { - $path = $this->getPath(); - - if (!is_file($path) && !@touch($path)) { - throw new Exception('Could not create file: ' . $path); - } - - if (!is_readable($path) || !is_writable($path)) { - throw new Exception('File does not have permission for read and write: ' . $path); - } - - if ($this->getConfig()->useGzip()) { - $path = 'compress.zlib://' . $path; - } - - $res = $this->fileAccessMode[$mode]; - $file = new SplFileObject($path, $res['mode']); - - if ($mode === self::FILE_READ) { - $file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD); - } - - if (!$this->getConfig()->useGzip() && !$file->flock($res['operation'])) { - $file = null; - throw new Exception('Could not lock file: ' . $path); - } - - return $file; - } - - /** - * Open a temporary file. - * - * @return SplTempFileObject - */ - public function openTempFile(): SplTempFileObject - { - return new SplTempFileObject($this->getConfig()->getSwapMemoryLimit()); - } - - /** - * Close the database file. - * - * @param SplFileObject $file - * - * @throws Exception - */ - protected function closeFile(SplFileObject &$file) - { - if (!$this->getConfig()->useGzip() && !$file->flock(LOCK_UN)) { - $file = null; - throw new Exception('Could not unlock file'); - } - - $file = null; - } - - /** - * Read lines from the database file. - * - * @return \Generator - */ - public function readFromFile(): \Generator - { - $file = $this->openFile(static::FILE_READ); - - try { - foreach ($file as $line) { - yield new Line($line); - } - } finally { - $this->closeFile($file); - } - } - - /** - * Append a line to the database file. - * - * @param string $line - */ - public function appendToFile(string $line) - { - $file = $this->openFile(static::FILE_APPEND); - $file->fwrite($line); - $this->closeFile($file); - } - - /** - * Flush the database file. - */ - public function flushFile() - { - $file = $this->openFile(static::FILE_WRITE); - $this->closeFile($file); - } - - /** - * Write temporary file contents to database file. - * - * @param SplTempFileObject $tmpFile - */ - public function writeTempToFile(SplTempFileObject &$tmpFile) - { - $file = $this->openFile(static::FILE_WRITE); - - foreach ($tmpFile as $line) { - $file->fwrite($line); - } - - $this->closeFile($file); - $tmpFile = null; - } -} diff --git a/core/vendor/flintstone/Exception.php b/core/vendor/flintstone/Exception.php deleted file mode 100644 index e7841979..00000000 --- a/core/vendor/flintstone/Exception.php +++ /dev/null @@ -1,7 +0,0 @@ -assoc = $assoc; - } - - /** - * {@inheritdoc} - */ - public function encode($data): string - { - $result = json_encode($data); - - if (json_last_error() === JSON_ERROR_NONE) { - return $result; - } - - throw new Exception(json_last_error_msg()); - } - - /** - * {@inheritdoc} - */ - public function decode(string $data) - { - $result = json_decode($data, $this->assoc); - - if (json_last_error() === JSON_ERROR_NONE) { - return $result; - } - - throw new Exception(json_last_error_msg()); - } -} diff --git a/core/vendor/flintstone/Formatter/SerializeFormatter.php b/core/vendor/flintstone/Formatter/SerializeFormatter.php deleted file mode 100644 index c16087ec..00000000 --- a/core/vendor/flintstone/Formatter/SerializeFormatter.php +++ /dev/null @@ -1,52 +0,0 @@ -preserveLines($data, false)); - } - - /** - * {@inheritdoc} - */ - public function decode(string $data) - { - return $this->preserveLines(unserialize($data), true); - } - - /** - * Preserve new lines, recursive function. - * - * @param mixed $data - * @param bool $reverse - * - * @return mixed - */ - protected function preserveLines($data, bool $reverse) - { - $search = ["\n", "\r"]; - $replace = ['\\n', '\\r']; - - if ($reverse) { - $search = ['\\n', '\\r']; - $replace = ["\n", "\r"]; - } - - if (is_string($data)) { - $data = str_replace($search, $replace, $data); - } elseif (is_array($data)) { - foreach ($data as &$value) { - $value = $this->preserveLines($value, $reverse); - } - unset($value); - } - - return $data; - } -} diff --git a/core/vendor/flintstone/Line.php b/core/vendor/flintstone/Line.php deleted file mode 100644 index c9f52e03..00000000 --- a/core/vendor/flintstone/Line.php +++ /dev/null @@ -1,37 +0,0 @@ -line = $line; - $this->pieces = explode('=', $line, 2); - } - - public function getLine(): string - { - return $this->line; - } - - public function getKey(): string - { - return $this->pieces[0]; - } - - public function getData(): string - { - return $this->pieces[1]; - } -} diff --git a/core/vendor/flintstone/Validation.php b/core/vendor/flintstone/Validation.php deleted file mode 100644 index 29e901e5..00000000 --- a/core/vendor/flintstone/Validation.php +++ /dev/null @@ -1,34 +0,0 @@ -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); - } -} diff --git a/core/vendor/jsondb/Dot.php b/core/vendor/jsondb/Dot.php new file mode 100644 index 00000000..3c4af643 --- /dev/null +++ b/core/vendor/jsondb/Dot.php @@ -0,0 +1,316 @@ +data = $data; + } + } + + /** + * Get value of path, default value if path doesn't exist or all data + * + * @param array $array Source Array + * @param mixed|null $key Path + * @param mixed|null $default Default value + * @return mixed Value of path + */ + public static function getValue($array, $key, $default = null) + { + if (is_string($key)) { + // Iterate path + $keys = explode('.', $key); + foreach ($keys as $key) { + if (!isset($array[$key])) { + return $default; + } + $array = &$array[$key]; + } + // Get value + return $array; + } elseif (is_null($key)) { + // Get all data + return $array; + } + return null; + } + + /** + * Set value or array of values to path + * + * @param array $array Target array with data + * @param mixed $key Path or array of paths and values + * @param mixed|null $value Value to set if path is not an array + */ + public static function setValue(&$array, $key, $value) + { + if (is_string($key)) { + // Iterate path + $keys = explode('.', $key); + foreach ($keys as $key) { + if (!isset($array[$key]) || !is_array($array[$key])) { + $array[$key] = []; + } + $array = &$array[$key]; + } + // Set value to path + $array = $value; + } elseif (is_array($key)) { + // Iterate array of paths and values + foreach ($key as $k => $v) { + self::setValue($array, $k, $v); + } + } + } + + /** + * Add value or array of values to path + * + * @param array $array Target array with data + * @param mixed $key Path or array of paths and values + * @param mixed|null $value Value to set if path is not an array + * @param boolean $pop Helper to pop out last key if value is an array + */ + public static function addValue(&$array, $key, $value = null, $pop = false) + { + if (is_string($key)) { + // Iterate path + $keys = explode('.', $key); + if ($pop === true) { + array_pop($keys); + } + foreach ($keys as $key) { + if (!isset($array[$key]) || !is_array($array[$key])) { + $array[$key] = []; + } + $array = &$array[$key]; + } + // Add value to path + $array[] = $value; + } elseif (is_array($key)) { + // Iterate array of paths and values + foreach ($key as $k => $v) { + self::addValue($array, $k, $v); + } + } + } + + /** + * Delete path or array of paths + * + * @param array $array Target array with data + * @param mixed $key Path or array of paths to delete + */ + public static function deleteValue(&$array, $key) + { + if (is_string($key)) { + // Iterate path + $keys = explode('.', $key); + $last = array_pop($keys); + foreach ($keys as $key) { + if (!isset($array[$key])) { + return; + } + $array = &$array[$key]; + } + if (isset($array[$last])) { + // Detele path + unset($array[$last]); + } + } elseif (is_array($key)) { + // Iterate array of paths + foreach ($key as $k) { + self::delete($k); + } + } + } + + + /** + * Get value of path, default value if path doesn't exist or all data + * + * @param mixed|null $key Path + * @param mixed|null $default Default value + * @return mixed Value of path + */ + public function get($key = null, $default = null) + { + return self::getValue($this->data, $key, $default); + } + + /** + * 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 + */ + public function set($key, $value = null) + { + return self::setValue($this->data, $key, $value); + } + + /** + * Add 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 + * @param boolean $pop Helper to pop out last key if value is an array + */ + public function add($key, $value = null, $pop = false) + { + return self::addValue($this->data, $key, $value, $pop); + } + + /** + * Check if path exists + * + * @param string $key Path + * @return boolean + */ + public function has($key) + { + $keys = explode('.', (string)$key); + $data = &$this->data; + foreach ($keys as $key) { + if (!isset($data[$key])) { + return false; + } + $data = &$data[$key]; + } + + return true; + } + + /** + * Delete path or array of paths + * + * @param mixed $key Path or array of paths to delete + */ + public function delete($key) + { + return self::deleteValue($this->data, $key); + } + + /** + * 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 + * @param boolean $format Format option + */ + public function clear($key = null, $format = false) + { + if (is_string($key)) { + // Iterate path + $keys = explode('.', $key); + $data = &$this->data; + foreach ($keys as $key) { + if (!isset($data[$key]) || !is_array($data[$key])) { + if ($format === true) { + $data[$key] = []; + } else { + return; + } + } + $data = &$data[$key]; + } + // Clear path + $data = []; + } elseif (is_array($key)) { + // Iterate array + foreach ($key as $k) { + $this->clear($k, $format); + } + } elseif (is_null($key)) { + // Clear all data + $this->data = []; + } + } + + /** + * Set data + * + * @param array $data + */ + public function setData(array $data) + { + $this->data = $data; + } + + /** + * Set data as a reference + * + * @param array $data + */ + public function setDataAsRef(array &$data) + { + $this->data = &$data; + } + + /** + * ArrayAccess abstract methods + */ + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + public function offsetExists($offset) + { + return $this->has($offset); + } + + public function offsetGet($offset) + { + return $this->get($offset); + } + + public function offsetUnset($offset) + { + $this->delete($offset); + } + + /** + * Magic methods + */ + public function __set($key, $value = null) + { + $this->set($key, $value); + } + + public function __get($key) + { + return $this->get($key); + } + + public function __isset($key) + { + return $this->has($key); + } + + public function __unset($key) + { + $this->delete($key); + } +} diff --git a/core/vendor/jsondb/JsonDb.php b/core/vendor/jsondb/JsonDb.php new file mode 100644 index 00000000..de755bc1 --- /dev/null +++ b/core/vendor/jsondb/JsonDb.php @@ -0,0 +1,138 @@ +config = array_merge([ + 'name' => 'data.json', + 'backup' => 5, + 'dir' => getcwd(), + 'template' => getcwd() . DIRECTORY_SEPARATOR . 'data.template.json' + ], $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 + * @param bool $save Сохранить данные в базу + * @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 + * + * @param mixed $key Path or array of paths and values + * @param mixed|null $value Value to set if path is not an array + * @param boolean $pop Helper to pop out last key if value is an array + * @param bool $save Сохранить данные в базу + * @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 + * + * @param mixed $key Path or array of paths to delete + * @param bool $save Сохранить данные в базу + * @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 + * @param boolean $format Format option + * @param bool $save Сохранить данные в базу + * @return $this + */ + public function clear($key = null, $format = false, $save = true) + { + parent::clear($key, $format); + if ($save) $this->save(); + return $this; + } + + + /** + * Загрузка локальной базы данных + * @param bool $reload + * Перезагрузить данные? + * @return array|mixed|null + */ + protected function loadData($reload = false) { + if ($this->data === null || $reload) { + $this->db = $this->config['dir'] . DIRECTORY_SEPARATOR . $this->config['name']; + if (!file_exists($this->db)) { + $templateFile = $this->config['template']; + if (file_exists($templateFile)) { + copy($templateFile, $this->db); + } else { + file_put_contents($this->db, '{}'); + } + } else { + if ($this->config['backup']) { + try { + //todo make backup of database + } catch (\Exception $e) { + + } + } + } + $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'); + } + } + return $this->data; + } + + /** + * Сохранение в локальную базу + */ + public function save() { + file_put_contents($this->db, json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT)); + } + + +} diff --git a/index.php b/index.php index 2bee724f..96adc396 100755 --- a/index.php +++ b/index.php @@ -8,6 +8,8 @@ * * @author Rémi Jean * @copyright Copyright (C) 2008-2018, Rémi Jean + * @author Frédéric Tempez + * @copyright Copyright (C) 2018-2019, Frédéric Tempez * @license GNU General Public License, version 3 * @link http://zwiicms.com/ */ @@ -15,8 +17,8 @@ /** * Vérification de la version de PHP */ -if(version_compare(PHP_VERSION, '7.0.0', '<')) { - exit('PHP 7.0+ required.'); +if(version_compare(PHP_VERSION, '5.6.0', '<')) { + exit('PHP 5.6+ required.'); } /* Set locale to French */