初始化
缺陷:主题配色需要优化整体的同风格
This commit is contained in:
64
scripts/db-check.php
Normal file
64
scripts/db-check.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 数据库连通性自检:只输出通/不通与来源文件名,绝不打印任何凭据。
|
||||
*
|
||||
* 环境重建后 mysql 容器换了名字,新后端 .env 里的账号密码可能已失效。
|
||||
* 这里依次拿几个项目的 .env 试连,用来定位到底哪套凭据还有效。
|
||||
*
|
||||
* 用法:php scripts/db-check.php
|
||||
*/
|
||||
$candidates = [
|
||||
'lgp-admin-plus-api/.env' => __DIR__ . '/../.env',
|
||||
'lgp-api/.env' => __DIR__ . '/../../lgp-api/.env',
|
||||
'lgp-wx-api/.env' => '/www/sites/lgp-wx-api/index/lgp-wx-api/.env',
|
||||
];
|
||||
|
||||
function readEnv(string $path): array
|
||||
{
|
||||
if (!is_file($path)) {
|
||||
return [];
|
||||
}
|
||||
$values = [];
|
||||
foreach (file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '' || str_starts_with($line, '#') || !str_contains($line, '=')) {
|
||||
continue;
|
||||
}
|
||||
[$key, $value] = explode('=', $line, 2);
|
||||
$values[trim($key)] = trim(trim($value), "\"'");
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
$target = $argv[1] ?? null;
|
||||
|
||||
// 环境重建后 host 也可能变了,所以每套凭据都对「自带 host」和「当前 mysql 容器」各试一次
|
||||
$hosts = ['mysql8'];
|
||||
|
||||
foreach ($candidates as $label => $path) {
|
||||
$env = readEnv($path);
|
||||
if (empty($env)) {
|
||||
echo str_pad($label, 32) . " 文件不存在\n";
|
||||
continue;
|
||||
}
|
||||
$envHost = $env['DB_HOST'] ?? '127.0.0.1';
|
||||
foreach (array_unique([$envHost, ...$hosts]) as $host) {
|
||||
$port = $env['DB_PORT'] ?? '3306';
|
||||
$db = $target ?: ($env['DB_DATABASE'] ?? '');
|
||||
$user = $env['DB_USERNAME'] ?? 'root';
|
||||
try {
|
||||
$pdo = new PDO(
|
||||
"mysql:host={$host};port={$port};dbname={$db};charset=utf8mb4",
|
||||
$user,
|
||||
$env['DB_PASSWORD'] ?? '',
|
||||
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 5]
|
||||
);
|
||||
$count = $pdo->query('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE()')->fetchColumn();
|
||||
echo str_pad($label, 32) . " 可连接 host={$host} db={$db} 表数={$count}\n";
|
||||
} catch (Throwable $e) {
|
||||
$reason = preg_replace('/using password: (YES|NO)/', 'using password: ***', $e->getMessage());
|
||||
echo str_pad($label, 32) . " 连不上 host={$host} " . $reason . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
43
scripts/lint-all.sh
Normal file
43
scripts/lint-all.sh
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
# Read-only syntax check: php -l over app/config/database/routes,
|
||||
# plus a guard for the historical return -> retun typo.
|
||||
#
|
||||
# Usage (inside an environment that has php, e.g. the 1Panel php-fpm container):
|
||||
# sh scripts/lint-all.sh /www/sites/lgp-api/index/lgp-admin-plus-api
|
||||
# This script never modifies files.
|
||||
set -eu
|
||||
|
||||
BASE=${1:-}
|
||||
if [ -z "$BASE" ]; then
|
||||
BASE=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
fi
|
||||
cd "$BASE"
|
||||
|
||||
TARGETS="app config database routes"
|
||||
|
||||
echo "== syntax =="
|
||||
fail=0
|
||||
count=0
|
||||
for f in $(find $TARGETS -name '*.php' | sort); do
|
||||
count=$((count + 1))
|
||||
out=$(php -l "$f" 2>&1 | grep -v swoole || true)
|
||||
case "$out" in
|
||||
*"No syntax errors"*) ;;
|
||||
*) echo "FAIL $f"; echo "$out"; fail=1 ;;
|
||||
esac
|
||||
done
|
||||
echo "checked $count files"
|
||||
|
||||
echo "== retun typo =="
|
||||
if grep -rn "retun" $TARGETS; then
|
||||
fail=1
|
||||
else
|
||||
echo "none"
|
||||
fi
|
||||
|
||||
if [ "$fail" -eq 0 ]; then
|
||||
echo "ALL OK"
|
||||
else
|
||||
echo "HAS ERRORS"
|
||||
exit 1
|
||||
fi
|
||||
41
scripts/lint.sh
Normal file
41
scripts/lint.sh
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# 语法检查 + 行尾规整
|
||||
#
|
||||
# 从 Windows 侧的 UNC 路径写文件会带 CRLF,PHP 能跑但和仓库里其余 LF 文件混排,
|
||||
# diff 会整片变红。这里统一转成 LF 再 php -l。
|
||||
#
|
||||
# 用法:bash scripts/lint.sh [目录或文件 ...] 不带参数则检查 app/ routes/ config/
|
||||
set -uo pipefail
|
||||
|
||||
cd "$(diname "$0")/.." || exit 1
|
||||
|
||||
TARGETS=("$@")
|
||||
if [ ${#TARGETS[@]} -eq 0 ]; then
|
||||
TARGETS=(app routes config database)
|
||||
fi
|
||||
|
||||
mapfile -t FILES < <(find "${TARGETS[@]}" -type f -name '*.php' 2>/dev/null | sort)
|
||||
|
||||
if [ ${#FILES[@]} -eq 0 ]; then
|
||||
echo "no php files under: ${TARGETS[*]}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
CRLF_FIXED=0
|
||||
for f in "${FILES[@]}"; do
|
||||
if grep -qU $'\r' "$f" 2>/dev/null; then
|
||||
perl -pi -e 's/\r\n/\n/' "$f"
|
||||
CRLF_FIXED=$((CRLF_FIXED + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
FAILED=0
|
||||
for f in "${FILES[@]}"; do
|
||||
if ! out=$(php -l "$f" 2>&1); then
|
||||
echo "$out"
|
||||
FAILED=$((FAILED + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
echo "checked=${#FILES[@]} crlf_fixed=${CRLF_FIXED} syntax_errors=${FAILED}"
|
||||
[ "$FAILED" -eq 0 ]
|
||||
Reference in New Issue
Block a user