zhamao-framework/src/ZM/Utils/DataProvider.php

150 lines
4.5 KiB
PHP
Raw Normal View History

<?php /** @noinspection PhpUnused */
2020-05-02 23:27:26 +08:00
2020-08-31 10:11:06 +08:00
namespace ZM\Utils;
2020-05-02 23:27:26 +08:00
2020-08-31 10:11:06 +08:00
use ZM\Config\ZMConfig;
use ZM\Console\Console;
2020-05-23 17:23:29 +08:00
2020-05-02 23:27:26 +08:00
class DataProvider
{
public static $buffer_list = [];
2021-06-16 00:17:30 +08:00
/**
* 返回资源目录
* @return string
*/
public static function getResourceFolder(): string {
2020-05-02 23:27:26 +08:00
return self::getWorkingDir() . '/resources/';
}
2021-06-16 00:17:30 +08:00
/**
* 返回工作目录,不带最右边文件夹的斜杠(/
* @return false|string
*/
2020-05-02 23:27:26 +08:00
public static function getWorkingDir() {
return WORKING_DIR;
2020-05-02 23:27:26 +08:00
}
2021-06-16 00:17:30 +08:00
/**
* 获取框架所在根目录
* @return false|string
*/
public static function getFrameworkRootDir() {
return FRAMEWORK_ROOT_DIR;
}
/**
* 获取源码根目录除Phar模式外均与工作目录相同
* @return false|string
*/
public static function getSourceRootDir() {
return defined("SOURCE_ROOT_DIR") ? SOURCE_ROOT_DIR : WORKING_DIR;
}
/**
* 获取框架反代链接
* @return array|false|mixed|null
*/
2020-05-02 23:27:26 +08:00
public static function getFrameworkLink() {
2020-08-31 10:11:06 +08:00
return ZMConfig::get("global", "http_reverse_link");
2020-05-02 23:27:26 +08:00
}
2021-06-16 00:17:30 +08:00
/**
* 获取zm_data数据目录如果二级目录不为空则自动创建目录并返回
* @param string $second
* @return array|false|mixed|string|null
*/
public static function getDataFolder(string $second = '') {
if ($second !== '') {
@mkdir(ZM_DATA . $second);
if (!is_dir(ZM_DATA . $second)) return false;
return realpath(ZM_DATA . $second) . "/";
}
2020-05-02 23:27:26 +08:00
return ZM_DATA;
}
2021-06-16 00:17:30 +08:00
/**
* 将变量保存在zm_data下的数据目录传入数组
* @param $filename
* @param $file_array
* @return false|int
*/
public static function saveToJson($filename, $file_array) {
$path = ZMConfig::get("global", "config_dir");
$r = explode("/", $filename);
2021-06-16 00:17:30 +08:00
if (count($r) == 2) {
$path = $path . $r[0] . "/";
if (!is_dir($path)) mkdir($path);
$name = $r[1];
} elseif (count($r) != 1) {
2021-06-16 00:17:30 +08:00
Console::warning(zm_internal_errcode("E00057") . "存储失败,文件名只能有一级目录");
return false;
} else {
$name = $r[0];
}
2021-06-16 00:17:30 +08:00
return file_put_contents($path . $name . ".json", json_encode($file_array, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
2021-06-16 00:17:30 +08:00
/**
* 从json加载变量到内存
* @param $filename
* @return mixed|null
*/
public static function loadFromJson($filename) {
$path = ZMConfig::get("global", "config_dir");
2021-06-16 00:17:30 +08:00
if (file_exists($path . $filename . ".json")) {
return json_decode(file_get_contents($path . $filename . ".json"), true);
} else {
return null;
}
}
2021-06-16 00:17:30 +08:00
/**
* 递归或非递归扫描目录,可返回相对目录的文件列表或绝对目录的文件列表
* @param $dir
* @param bool $recursive
* @param bool|string $relative
* @return array|false
* @since 2.5
*/
public static function scanDirFiles($dir, bool $recursive = true, $relative = false) {
$dir = rtrim($dir, "/");
if (!is_dir($dir)) return false;
$r = scandir($dir);
if ($r === false) return false;
$list = [];
if ($relative === true) {
$relative = $dir;
}
foreach ($r as $v) {
if ($v == "." || $v == "..") continue;
$sub_file = $dir . "/" . $v;
if (is_dir($sub_file) && $recursive) {
$list = array_merge($list, self::scanDirFiles($sub_file, $recursive, $relative));
} elseif (is_file($sub_file)) {
if (is_string($relative) && mb_strpos($sub_file, $relative) === 0) {
$list [] = ltrim(mb_substr($sub_file, mb_strlen($relative)), "/");
} elseif ($relative === false) {
$list[] = $sub_file;
} else {
Console::warning(zm_internal_errcode("E00058") . "Relative path is not generated: wrong base directory ($relative)");
return false;
}
}
}
return $list;
}
/**
* 检查路径是否为相对路径(根据第一个字符是否为"/"来判断)
* @param $path
* @return bool
* @since 2.5
*/
public static function isRelativePath($path) {
return strlen($path) > 0 && $path[0] === '/';
}
2020-05-02 23:27:26 +08:00
}