Skip to content

Commit eea04fe

Browse files
authored
refactor: write the Console runner through its Output (#10713)
1 parent 0b98c7e commit eea04fe

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

‎system/CLI/Console.php‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function run(array $tokens = [])
5959
$this->setShellVerbosity($output->getVerbosity()->value);
6060

6161
try {
62-
$exitCode = $this->dispatch($arguments);
62+
$exitCode = $this->dispatch($output, $arguments);
6363
} catch (Throwable $e) {
6464
// The spark run keeps the level, so the exception handler renders the uncaught throwable at it.
6565
if (! $fromArgv) {
@@ -68,7 +68,11 @@ public function run(array $tokens = [])
6868

6969
throw $e;
7070
} finally {
71-
$this->restoreShellVerbosity($previousShellVerbosity, $previousEnvShellVerbosity, $previousServerShellVerbosity);
71+
$this->restoreShellVerbosity(
72+
$previousShellVerbosity,
73+
$previousEnvShellVerbosity,
74+
$previousServerShellVerbosity,
75+
);
7276
}
7377

7478
$output->setVerbosity($previousVerbosity);
@@ -103,18 +107,20 @@ public function showHeader(bool $suppress = false)
103107
return;
104108
}
105109

106-
CLI::write(sprintf(
110+
$output = CLI::getOutput();
111+
112+
$output->write(sprintf(
107113
'CodeIgniter v%s Command Line Tool - Server Time: %s',
108114
CodeIgniter::CI_VERSION,
109115
date('Y-m-d H:i:s \\U\\T\\CP'),
110116
), 'green');
111-
CLI::newLine();
117+
$output->newLine();
112118
}
113119

114120
/**
115121
* @param list<string> $arguments
116122
*/
117-
private function dispatch(array $arguments): int
123+
private function dispatch(Output $output, array $arguments): int
118124
{
119125
$noHeader = $this->hasParameterOption(['no-header']);
120126

@@ -139,14 +145,14 @@ private function dispatch(array $arguments): int
139145
$this->showHeader($noHeader || $commands->isHeaderlessCommand($this->command));
140146

141147
if (
142-
$this->isInteractive()
148+
$this->isInteractive($output)
143149
&& ! $commands->hasLegacyCommand($this->command)
144150
&& ! $commands->hasModernCommand($this->command)
145151
) {
146152
$alternatives = $commands->getCommandAlternatives($this->command);
147153

148154
if ($alternatives !== []) {
149-
$alternative = $this->chooseAlternative($alternatives);
155+
$alternative = $this->chooseAlternative($output, $alternatives);
150156

151157
if ($alternative === null) {
152158
return EXIT_ERROR;
@@ -163,7 +169,7 @@ private function dispatch(array $arguments): int
163169
return $commands->runLegacy($this->command, array_merge($arguments, $legacyOptions));
164170
}
165171

166-
return $commands->runCommand($this->command, $arguments, $this->normalizeVerbosityOptions(), CLI::getOutput());
172+
return $commands->runCommand($this->command, $arguments, $this->normalizeVerbosityOptions(), $output);
167173
}
168174

169175
/**
@@ -280,15 +286,18 @@ private function restoreShellVerbosity(false|string $process, mixed $env, array|
280286
*
281287
* @param list<string> $alternatives
282288
*/
283-
private function chooseAlternative(array $alternatives): ?string
289+
private function chooseAlternative(Output $output, array $alternatives): ?string
284290
{
285-
CLI::error(lang('CLI.commandNotFound', [$this->command]));
286-
CLI::newLine();
291+
$output->error(lang('CLI.commandNotFound', [$this->command]));
292+
$output->newLine();
287293

288-
$prompt = CLI::getPrompt();
294+
$prompt = new Prompt($output);
289295

290296
if (count($alternatives) === 1) {
291-
return $prompt->ask(lang('CLI.altCommandRun', [$alternatives[0]]), ['y', 'n']) === 'y' ? $alternatives[0] : null;
297+
return $prompt->ask(
298+
lang('CLI.altCommandRun', [$alternatives[0]]),
299+
['y', 'n'],
300+
) === 'y' ? $alternatives[0] : null;
292301
}
293302

294303
$chosen = $prompt->select(lang('CLI.altCommandSelect'), [...$alternatives, lang('CLI.altCommandNone')]);
@@ -312,11 +321,11 @@ private function hasParameterOption(array $options): bool
312321
return false;
313322
}
314323

315-
private function isInteractive(): bool
324+
private function isInteractive(Output $output): bool
316325
{
317326
return ! $this->hasParameterOption(['no-interaction', 'N'])
318-
&& ! CLI::getOutput()->isQuiet()
319-
&& ! CLI::getInputOutput() instanceof NullInputOutput
327+
&& ! $output->isQuiet()
328+
&& ! $output->getInputOutput() instanceof NullInputOutput
320329
&& defined('STDIN')
321330
&& CLI::streamSupports('stream_isatty', STDIN);
322331
}

0 commit comments

Comments
 (0)