mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-10 03:42:49 +08:00
feat(console): 添加 --force-force 参数以跳过所有交互确认
This commit is contained in:
9
app/common/console/Input.php
Normal file
9
app/common/console/Input.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\common\console;
|
||||||
|
|
||||||
|
use base\common\console\InputBase;
|
||||||
|
|
||||||
|
class Input extends InputBase
|
||||||
|
{
|
||||||
|
}
|
||||||
9
app/common/console/Output.php
Normal file
9
app/common/console/Output.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\common\console;
|
||||||
|
|
||||||
|
use base\common\console\OutputBase;
|
||||||
|
|
||||||
|
class Output extends OutputBase
|
||||||
|
{
|
||||||
|
}
|
||||||
9
app/common/provider/Console.php
Normal file
9
app/common/provider/Console.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\common\provider;
|
||||||
|
|
||||||
|
use base\common\provider\ConsoleBase;
|
||||||
|
|
||||||
|
class Console extends ConsoleBase
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
// 容器Provider定义文件
|
// 容器Provider定义文件
|
||||||
|
|
||||||
return [
|
use app\common\provider\Console;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'think\Console'=>Console::class
|
||||||
];
|
];
|
||||||
|
|||||||
34
extend/base/common/console/InputBase.php
Normal file
34
extend/base/common/console/InputBase.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace base\common\console;
|
||||||
|
|
||||||
|
use think\console\Input;
|
||||||
|
|
||||||
|
class InputBase extends Input
|
||||||
|
{
|
||||||
|
public function __construct($argv = null)
|
||||||
|
{
|
||||||
|
if (null === $argv) {
|
||||||
|
$argv = $_SERVER['argv'];
|
||||||
|
array_shift($argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
$argv = $this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
35
extend/base/common/console/OutputBase.php
Normal file
35
extend/base/common/console/OutputBase.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace base\common\console;
|
||||||
|
|
||||||
|
use think\console\Input;
|
||||||
|
use think\console\Output;
|
||||||
|
|
||||||
|
class OutputBase extends Output
|
||||||
|
{
|
||||||
|
public function confirm(Input $input, $question, $default = true)
|
||||||
|
{
|
||||||
|
$question = $this->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 = '<comment>(您可以追加 --force-force 或 -ff 参数重新运行以跳过所有交互确认)</comment>';
|
||||||
|
if (is_string($question) && $question !== '') {
|
||||||
|
return rtrim($question) . ' ' . $tip;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tip;
|
||||||
|
}
|
||||||
|
}
|
||||||
82
extend/base/common/provider/ConsoleBase.php
Normal file
82
extend/base/common/provider/ConsoleBase.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace base\common\provider;
|
||||||
|
|
||||||
|
use app\common\console\Input;
|
||||||
|
use app\common\console\Output;
|
||||||
|
use think\Console as ThinkConsole;
|
||||||
|
use think\console\input\Argument as InputArgument;
|
||||||
|
use think\console\input\Definition as InputDefinition;
|
||||||
|
use think\console\input\Option as InputOption;
|
||||||
|
|
||||||
|
class ConsoleBase extends ThinkConsole
|
||||||
|
{
|
||||||
|
public function call(string $command, array $parameters = [], string $driver = 'buffer')
|
||||||
|
{
|
||||||
|
array_unshift($parameters, $command);
|
||||||
|
|
||||||
|
$input = new Input($parameters);
|
||||||
|
$output = new Output($driver);
|
||||||
|
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user