goldrat/precommit.php
2025-10-13 09:04:00 +00:00

75 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
/**
* 项目提交前预处理脚本
* 用于安全备份并清理敏感配置字段
*/
$source = __DIR__ . '/config.php';
$backup = __DIR__ . '/config.backup.php';
// 1⃣ 检查文件是否存在
if (!file_exists($source)) {
echo "❌ 找不到 config.php跳过处理。\n";
exit(0);
}
// 2⃣ 备份
copy($source, $backup);
echo "✅ 已备份到 config.backup.php\n";
// 3⃣ 读取原配置
// 3⃣ 读取配置
$options = [];
require $source; // 直接导入 $options
if (!isset($options) || !is_array($options)) {
echo "❌ config.php 中未定义 \$options 数组。\n";
exit(1);
}
// 4⃣ 根据需求修改特定字段
// 举例清空日志路径、设置默认workers、隐藏api_key
$fieldsToReset = [
'redis' => [
'ip'=>'127.0.0.1',
'port'=>6379,
'password'=>'YOURPASSWARD',
],
'db' => [
'type' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
],
'apikey'=>'',
];
// 应用修改
foreach ($fieldsToReset as $key => $value) {
if (array_key_exists($key, $options)) {
$options[$key] = $value;
}
}
// 5⃣ 写回原文件(保持 $options = [...] 格式)
$export = var_export($options, true);
// 把 array(...) 替换成 [...]
$export = preg_replace(['/\barray \(/', '/\)(\s*[,;])?/'], ['[', ']$1'], $export);
// 构造完整内容
$content = <<<PHP
<?php
\$options = $export;
PHP;
// 写入文件
file_put_contents($source, $content);
echo "✅ config.php 已清理敏感信息,可安全提交。\n";