ThinkAdmin/think
邹景立 3d4525e5a0 fix(命令行): 确保未捕获异常返回非零退出码
背景与目的:框架异常处理器可能将未捕获的初始化或命令异常处理为零退出码,导致调用方误判执行成功。

主要改动:入口捕获未处理的 Throwable;直接执行时清除全局异常处理器后重新抛出,由 PHP 返回失败状态。移除带 shebang 入口的 strict_types 声明,兼容旧版 PHP 的包含调用。

影响范围与兼容性:保留正常命令及显式退出码;被其他脚本包含时保持调用方异常处理器,并继续向上传递原异常。

验证入口及实际结果:临时目录复用 check-console.php,加载本项目实际入口与已安装依赖,直接执行、包含调用、关闭自动退出、异常传递及初始化失败场景全部通过;PHP 语法和差异检查通过。本次仅验证本机 PHP,未重跑 PHP 7.2 或其他版本矩阵。
2026-10-19 09:35:17 +08:00

40 lines
1.7 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.

#!/usr/bin/env php
<?php
/**
* // +----------------------------------------------------------------------
* // | Payment Plugin for ThinkAdmin
* // +----------------------------------------------------------------------
* // | 版权所有 2014~2026 ThinkAdmin [ thinkadmin.top ]
* // +----------------------------------------------------------------------
* // | 官方网站: https://thinkadmin.top
* // +----------------------------------------------------------------------
* // | 开源协议 ( https://mit-license.org )
* // | 免责声明 ( https://thinkadmin.top/disclaimer )
* // | 会员免费 ( https://thinkadmin.top/vip-introduce )
* // +----------------------------------------------------------------------
* // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
* // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
* // +----------------------------------------------------------------------
*/
use think\admin\service\RuntimeService;
// 带 shebang 的入口不声明 strict_types,兼容 PHP 7.2 的包含调用。
// 加载基础文件
require __DIR__ . '/vendor/autoload.php';
/**
* 初始化命令行应用。
* 直接执行时将未捕获异常交由 PHP 原生处理,确保失败以非零状态退出。
* 包含入口时保留调用方异常处理器,并向上传递原异常。
* @throws \Throwable 初始化或命令执行中未被控制台捕获的异常
*/
try {
RuntimeService::doConsoleInit();
} catch (\Throwable $error) {
if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true) && realpath($_SERVER['SCRIPT_FILENAME'] ?? '') === __FILE__) {
set_exception_handler(null);
}
throw $error;
}