From 5bee1b373384c3efe9c6ab06e22b2a30ce47eb42 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 31 Jan 2026 23:00:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(console):=20=E6=B7=BB=E5=8A=A0=20--force-f?= =?UTF-8?q?orce=20=E5=8F=82=E6=95=B0=E4=BB=A5=E8=B7=B3=E8=BF=87=E6=89=80?= =?UTF-8?q?=E6=9C=89=E4=BA=A4=E4=BA=92=E7=A1=AE=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/console/Input.php | 9 +++ app/common/console/Output.php | 9 +++ app/common/provider/Console.php | 9 +++ app/provider.php | 4 +- extend/base/common/console/InputBase.php | 34 +++++++++ extend/base/common/console/OutputBase.php | 35 +++++++++ extend/base/common/provider/ConsoleBase.php | 82 +++++++++++++++++++++ 7 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 app/common/console/Input.php create mode 100644 app/common/console/Output.php create mode 100644 app/common/provider/Console.php create mode 100644 extend/base/common/console/InputBase.php create mode 100644 extend/base/common/console/OutputBase.php create mode 100644 extend/base/common/provider/ConsoleBase.php diff --git a/app/common/console/Input.php b/app/common/console/Input.php new file mode 100644 index 0000000..8427e8b --- /dev/null +++ b/app/common/console/Input.php @@ -0,0 +1,9 @@ +Console::class ]; diff --git a/extend/base/common/console/InputBase.php b/extend/base/common/console/InputBase.php new file mode 100644 index 0000000..dadc7ea --- /dev/null +++ b/extend/base/common/console/InputBase.php @@ -0,0 +1,34 @@ +normalizeTokens($argv); + + parent::__construct($argv); + } + + protected function normalizeTokens(array $tokens): array + { + $result = []; + foreach ($tokens as $token) { + if ($token === '-ff') { + $result[] = '--force-force'; + continue; + } + $result[] = $token; + } + + return $result; + } +} diff --git a/extend/base/common/console/OutputBase.php b/extend/base/common/console/OutputBase.php new file mode 100644 index 0000000..59bf51c --- /dev/null +++ b/extend/base/common/console/OutputBase.php @@ -0,0 +1,35 @@ +appendForceForceTip($question); + + if ($this->isForceForceEnabled($input)) { + return true; + } + + return parent::confirm($input, $question, $default); + } + + protected function isForceForceEnabled(Input $input): bool + { + return (bool) $input->getOption('force-force'); + } + + protected function appendForceForceTip($question): string + { + $tip = '(您可以追加 --force-force 或 -ff 参数重新运行以跳过所有交互确认)'; + if (is_string($question) && $question !== '') { + return rtrim($question) . ' ' . $tip; + } + + return $tip; + } +} diff --git a/extend/base/common/provider/ConsoleBase.php b/extend/base/common/provider/ConsoleBase.php new file mode 100644 index 0000000..71e58c1 --- /dev/null +++ b/extend/base/common/provider/ConsoleBase.php @@ -0,0 +1,82 @@ +setCatchExceptions(false); + $this->find($command)->run($input, $output); + + return $output; + } + + public function run() + { + $input = new Input(); + $output = new Output(); + + $this->configureIO($input, $output); + + try { + $exitCode = $this->doRun($input, $output); + } catch (\Exception $e) { + if (!$this->catchExceptions) { + throw $e; + } + + $output->renderException($e); + + $exitCode = $e->getCode(); + if (is_numeric($exitCode)) { + $exitCode = (int) $exitCode; + if (0 === $exitCode) { + $exitCode = 1; + } + } else { + $exitCode = 1; + } + } + + if ($this->autoExit) { + if ($exitCode > 255) { + $exitCode = 255; + } + + exit($exitCode); + } + + return $exitCode; + } + + protected function getDefaultInputDefinition(): InputDefinition + { + $definition = new InputDefinition([ + new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'), + new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'), + new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this console version'), + new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'), + new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'), + new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'), + new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'), + new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'), + new InputOption('--force-force', 'ff', InputOption::VALUE_NONE, 'Skip all interactive confirmation prompts (can skip all confirmations)'), + ]); + + return $definition; + } + +}