ThinkAdmin/tests/path-examples.php
Anyon e7a8c05556 chore(repo): 统一 v8 仓库品牌名称
将 v8 重构分支中残留的 ThinkAdminDeveloper 文本统一调整为 ThinkAdmin,避免迁移到主仓库后继续暴露旧开发仓库名称。

主要内容:

- 更新 README 标题与项目描述。

- 统一 PHP 文件头注释中的项目标识。

- 同步调整测试、配置、插件与文档中的旧仓库名称文本。

- 保持旧包删除说明与架构边界测试语义不变,只清理品牌名称残留。
2026-05-08 16:15:24 +08:00

141 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* +----------------------------------------------------------------------
* | ThinkAdmin Plugin
* +----------------------------------------------------------------------
* | Copyright (c) 2014~2026 ThinkAdmin [ thinkadmin.top ]
* +----------------------------------------------------------------------
* | Official Website: https://thinkadmin.top
* +----------------------------------------------------------------------
* | Licensed: https://mit-license.org
* | Disclaimer: https://thinkadmin.top/disclaimer
* | Vip Rights: https://thinkadmin.top/vip-introduce
* +----------------------------------------------------------------------
* | Gitee Repository: https://gitee.com/zoujingli/ThinkAdmin
* | Github Repository: https://github.com/zoujingli/ThinkAdmin
* +----------------------------------------------------------------------
*/
// ============================================
// 场景 1加载 PHP 类文件(使用 syspath
// ============================================
require syspath('vendor/autoload.php');
require syspath('app/controller/User.php');
// ============================================
// 场景 2读取配置文件使用 syspath
// ============================================
$configFile = syspath('config/database.php');
if (is_file($configFile)) {
$config = include $configFile;
}
// ============================================
// 场景 3写入日志文件使用 runpath
// ============================================
$logFile = runpath('runtime/log/' . date('Ymd') . '.log');
file_put_contents($logFile, "日志内容\n", FILE_APPEND);
// ============================================
// 场景 4保存上传文件使用 runpath
// ============================================
$uploadDir = runpath('public/upload');
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$targetFile = $uploadDir . '/' . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $targetFile);
// ============================================
// 场景 5数据库文件存储使用 runpath
// ============================================
$dbFile = runpath('database/sqlite.db');
// PHAR 环境:/path/to/install/database/sqlite.db
// 普通环境:/project/database/sqlite.db
// ============================================
// 场景 6缓存文件使用 runpath
// ============================================
$cacheFile = runpath('runtime/cache/' . md5($key) . '.php');
file_put_contents($cacheFile, "<?php\nreturn " . var_export($data, true) . ';');
// ============================================
// 场景 7会话文件使用 runpath
// ============================================
$sessionPath = runpath('runtime/session');
session_save_path($sessionPath);
// ============================================
// 场景 8读取框架资源使用 syspath
// ============================================
$frameworkPath = syspath('vendor/topthink/framework');
$routeFile = syspath('vendor/topthink/framework/src/think/Route.php');
// ============================================
// 场景 9环境文件操作使用 runpath
// ============================================
$envFile = runpath('.env');
if (!is_file($envFile)) {
copy(syspath('.env.example'), $envFile);
}
// ============================================
// 场景 10检查文件是否存在注意路径选择
// ============================================
// ❌ 错误:在 PHAR 中syspath('runtime') 指向 phar:// 内部,文件不存在
if (is_file(syspath('runtime/cache/test.php'))) {
// 这段代码在 PHAR 中永远不会执行
}
// ✅ 正确:使用 runpath 访问可写路径
if (is_file(runpath('runtime/cache/test.php'))) {
// 可以正确访问到文件
$content = file_get_contents(runpath('runtime/cache/test.php'));
}
// ============================================
// 场景 11路径调试
// ============================================
if (is_phar()) {
echo "当前运行在 PHAR 环境\n";
echo '系统根目录:' . syspath() . "\n"; // phar:///path/to/admin.phar
echo '运行根目录:' . runpath() . "\n"; // /path/to/install
} else {
echo "当前运行在普通环境\n";
echo '系统根目录:' . syspath() . "\n"; // /project
echo '运行根目录:' . runpath() . "\n"; // /project
}
// ============================================
// 场景 12动态路径选择
// ============================================
function getConfigPath(): string
{
// 配置文件在代码目录中,使用 syspath
return syspath('config/app.php');
}
function getRuntimePath(): string
{
// 运行时数据在可写目录中,使用 runpath
return runpath('runtime');
}
// ============================================
// 快速参考表
// ============================================
/*
| 用途 | 使用函数 | PHAR 环境示例 | 普通环境示例 |
|----------------|-----------|----------------------------------|------------------------|
| 加载类文件 | syspath | phar:///admin.phar/app/User.php | /app/User.php |
| 读取配置 | syspath | phar:///admin.phar/config/db.php | /config/db.php |
| 框架代码 | syspath | phar:///admin.phar/vendor/... | /vendor/... |
| 写入日志 | runpath | /install/runtime/log/... | /project/runtime/log/ |
| 上传文件 | runpath | /install/public/upload/... | /project/public/upload |
| 缓存文件 | runpath | /install/runtime/cache/... | /project/runtime/cache |
| 数据库文件 | runpath | /install/database/... | /project/database |
| 会话文件 | runpath | /install/runtime/session/... | /project/runtime/sess |
| 环境文件 | runpath | /install/.env | /project/.env |
*/