2020-05-02 23:27:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Framework;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DataProvider
|
|
|
|
|
{
|
|
|
|
|
public static $buffer_list = [];
|
|
|
|
|
|
|
|
|
|
public static function getResourceFolder() {
|
|
|
|
|
return self::getWorkingDir() . '/resources/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getWorkingDir() {
|
|
|
|
|
global $is_phar;
|
2020-05-06 17:19:59 +08:00
|
|
|
if ($is_phar === true) {
|
2020-05-02 23:27:26 +08:00
|
|
|
return realpath('.');
|
2020-05-06 17:19:59 +08:00
|
|
|
} else {
|
2020-05-02 23:27:26 +08:00
|
|
|
return WORKING_DIR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getDataConfig() {
|
|
|
|
|
return CONFIG_DIR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function addSaveBuffer($buf_name, $sub_folder = null) {
|
|
|
|
|
$name = ($sub_folder ?? "") . "/" . $buf_name . ".json";
|
|
|
|
|
self::$buffer_list[$buf_name] = $name;
|
2020-05-06 17:19:59 +08:00
|
|
|
Console::debug("Added " . $buf_name . " at $sub_folder");
|
2020-05-02 23:27:26 +08:00
|
|
|
ZMBuf::set($buf_name, self::getJsonData($name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function saveBuffer() {
|
|
|
|
|
$head = Console::setColor(date("[H:i:s] ") . "[V] Saving buffer......", "blue");
|
|
|
|
|
if (ZMBuf::$atomics["info_level"]->get() >= 3)
|
|
|
|
|
echo $head;
|
|
|
|
|
foreach (self::$buffer_list as $k => $v) {
|
2020-05-06 17:19:59 +08:00
|
|
|
Console::debug("Saving " . $k . " to " . $v);
|
2020-05-02 23:27:26 +08:00
|
|
|
self::setJsonData($v, ZMBuf::get($k));
|
|
|
|
|
}
|
|
|
|
|
if (ZMBuf::$atomics["info_level"]->get() >= 3)
|
|
|
|
|
echo Console::setColor("saved", "blue") . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getFrameworkLink() {
|
|
|
|
|
return ZMBuf::globals("http_reverse_link");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getJsonData(string $string) {
|
|
|
|
|
if (!file_exists(self::getDataConfig() . $string)) return [];
|
|
|
|
|
return json_decode(file_get_contents(self::getDataConfig() . $string), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function setJsonData($filename, array $args) {
|
2020-05-06 17:19:59 +08:00
|
|
|
$pathinfo = pathinfo($filename);
|
2020-05-10 14:11:32 +08:00
|
|
|
if (!is_dir(self::getDataConfig() . $pathinfo["dirname"])) {
|
|
|
|
|
Console::debug("Making Directory: " . self::getDataConfig() . $pathinfo["dirname"]);
|
|
|
|
|
mkdir(self::getDataConfig() . $pathinfo["dirname"]);
|
|
|
|
|
}
|
2020-05-06 17:19:59 +08:00
|
|
|
$r = file_put_contents(self::getDataConfig() . $filename, json_encode($args, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING));
|
|
|
|
|
if ($r === false) {
|
|
|
|
|
Console::warning("无法保存文件: " . $filename);
|
|
|
|
|
}
|
2020-05-02 23:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getDataFolder() {
|
|
|
|
|
return ZM_DATA;
|
|
|
|
|
}
|
|
|
|
|
}
|