Skip to content

Commit ebd25ec

Browse files
authored
refactor: write the Housekeeping, Cache, Server and Translation commands through $this->getOutput() (#10680)
1 parent 2556a57 commit ebd25ec

8 files changed

Lines changed: 60 additions & 55 deletions

File tree

‎system/Commands/Cache/ClearCache.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\Autoloader\FileLocatorCached;
1717
use CodeIgniter\CLI\AbstractCommand;
1818
use CodeIgniter\CLI\Attributes\Command;
19-
use CodeIgniter\CLI\CLI;
2019
use CodeIgniter\CLI\Input\Argument;
2120
use Config\Cache;
2221

@@ -39,17 +38,18 @@ protected function execute(array $arguments, array $options): int
3938
{
4039
$driver = $arguments['driver'];
4140
$config = config(Cache::class);
41+
$output = $this->getOutput();
4242

4343
if (! array_key_exists($driver, $config->validHandlers)) {
44-
CLI::error(lang('Cache.invalidHandler', [$driver]));
44+
$output->error(lang('Cache.invalidHandler', [$driver]));
4545

4646
return EXIT_ERROR;
4747
}
4848

4949
$config->handler = $driver;
5050

5151
if (! service('cache', $config)->clean()) {
52-
CLI::error(sprintf('Error occurred while clearing the cache using the "%s" driver.', $driver));
52+
$output->error(sprintf('Error occurred while clearing the cache using the "%s" driver.', $driver));
5353

5454
return EXIT_ERROR;
5555
}
@@ -60,7 +60,7 @@ protected function execute(array $arguments, array $options): int
6060
$locator->deleteCache();
6161
}
6262

63-
CLI::write(sprintf('Cache cleared using the "%s" driver.', $driver), 'green');
63+
$output->write(sprintf('Cache cleared using the "%s" driver.', $driver), 'green');
6464

6565
return EXIT_SUCCESS;
6666
}

‎system/Commands/Cache/InfoCache.php‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
18+
use CodeIgniter\CLI\Table;
1919
use CodeIgniter\I18n\Time;
2020
use Config\Cache;
2121

@@ -28,9 +28,10 @@ class InfoCache extends AbstractCommand
2828
protected function execute(array $arguments, array $options): int
2929
{
3030
$config = config(Cache::class);
31+
$output = $this->getOutput();
3132

3233
if ($config->handler !== 'file') {
33-
CLI::error(sprintf(
34+
$output->error(sprintf(
3435
'This command only supports the file cache handler. The configured handler is "%s".',
3536
$config->handler,
3637
));
@@ -52,12 +53,12 @@ protected function execute(array $arguments, array $options): int
5253
];
5354
}
5455

55-
CLI::table($tbody, [
56-
CLI::color('Name', 'green'),
57-
CLI::color('Server Path', 'green'),
58-
CLI::color('Size', 'green'),
59-
CLI::color('Date', 'green'),
60-
]);
56+
$output->write((new Table($tbody, [
57+
$output->color('Name', 'green'),
58+
$output->color('Server Path', 'green'),
59+
$output->color('Size', 'green'),
60+
$output->color('Date', 'green'),
61+
]))->render());
6162

6263
return EXIT_SUCCESS;
6364
}

‎system/Commands/Housekeeping/ClearDebugbar.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918

2019
/**
2120
* Clears all debugbar JSON files.
@@ -27,15 +26,16 @@ protected function execute(array $arguments, array $options): int
2726
{
2827
helper('filesystem');
2928

30-
$path = clean_path(WRITEPATH . 'debugbar');
29+
$path = clean_path(WRITEPATH . 'debugbar');
30+
$output = $this->getOutput();
3131

3232
if (! delete_files(WRITEPATH . 'debugbar', htdocs: true)) {
33-
CLI::error(sprintf('Error deleting the debugbar JSON files in "%s".', $path));
33+
$output->error(sprintf('Error deleting the debugbar JSON files in "%s".', $path));
3434

3535
return EXIT_ERROR;
3636
}
3737

38-
CLI::write(sprintf('Cleared debugbar JSON files in "%s".', $path), 'green');
38+
$output->write(sprintf('Cleared debugbar JSON files in "%s".', $path), 'green');
3939

4040
return EXIT_SUCCESS;
4141
}

‎system/Commands/Housekeeping/ClearLogs.php‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
2019

2120
/**
@@ -39,7 +38,7 @@ protected function interact(array &$arguments, array &$options): void
3938
return;
4039
}
4140

42-
if (CLI::prompt(sprintf('Delete all log files in %s?', clean_path(WRITEPATH . 'logs')), ['n', 'y']) === 'n') {
41+
if ($this->getPrompt()->ask(sprintf('Delete all log files in %s?', clean_path(WRITEPATH . 'logs')), ['n', 'y']) === 'n') {
4342
return;
4443
}
4544

@@ -48,27 +47,29 @@ protected function interact(array &$arguments, array &$options): void
4847

4948
protected function execute(array $arguments, array $options): int
5049
{
50+
$output = $this->getOutput();
51+
5152
if ($options['force'] === false) {
5253
if ($this->isInteractive()) {
53-
CLI::write('Log deletion cancelled.', 'yellow');
54+
$output->write('Log deletion cancelled.', 'yellow');
5455

5556
return EXIT_SUCCESS;
5657
}
5758

58-
CLI::error('Log deletion aborted: pass --force to delete log files in non-interactive mode.');
59+
$output->error('Log deletion aborted: pass --force to delete log files in non-interactive mode.');
5960

6061
return EXIT_ERROR;
6162
}
6263

6364
helper('filesystem');
6465

6566
if (! delete_files(WRITEPATH . 'logs', htdocs: true)) {
66-
CLI::error(sprintf('Failed to delete log files in %s.', clean_path(WRITEPATH . 'logs')));
67+
$output->error(sprintf('Failed to delete log files in %s.', clean_path(WRITEPATH . 'logs')));
6768

6869
return EXIT_ERROR;
6970
}
7071

71-
CLI::write(sprintf('Log files in %s cleared.', clean_path(WRITEPATH . 'logs')), 'green');
72+
$output->write(sprintf('Log files in %s cleared.', clean_path(WRITEPATH . 'logs')), 'green');
7273

7374
return EXIT_SUCCESS;
7475
}

‎system/Commands/Server/Serve.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
2019

2120
/**
@@ -43,13 +42,14 @@ protected function execute(array $arguments, array $options): int
4342
{
4443
$basePort = (int) $options['port'];
4544
$status = EXIT_SUCCESS;
45+
$output = $this->getOutput();
4646

4747
for ($offset = 0; $offset <= self::RETRIES; $offset++) {
4848
$port = $basePort + $offset;
4949

50-
CLI::write(sprintf('CodeIgniter development server started on http://%s:%s', $options['host'], $port), 'green');
51-
CLI::write('Press Control-C to stop.');
52-
CLI::newLine();
50+
$output->write(sprintf('CodeIgniter development server started on http://%s:%s', $options['host'], $port), 'green');
51+
$output->write('Press Control-C to stop.');
52+
$output->newLine();
5353

5454
passthru(
5555
$this->buildServeCommand($options['php'], $options['host'], $port, FCPATH, SYSTEMPATH . 'rewrite.php'),
@@ -60,7 +60,7 @@ protected function execute(array $arguments, array $options): int
6060
return $status;
6161
}
6262

63-
CLI::newLine();
63+
$output->newLine();
6464
}
6565

6666
return $status;

‎system/Commands/Translation/LocalizationFinder.php‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
19+
use CodeIgniter\CLI\Output;
20+
use CodeIgniter\CLI\Table;
2021
use CodeIgniter\CLI\Verbosity;
2122
use CodeIgniter\Helpers\Array\ArrayHelper;
2223
use Config\App;
@@ -67,14 +68,15 @@ protected function execute(array $arguments, array $options): int
6768
assert(is_string($dir));
6869

6970
$currentLocale = Locale::getDefault();
71+
$output = $this->getOutput();
7072

7173
['currentDir' => $currentDir, 'languagePath' => $this->languagePath] = $this->resolvePaths();
7274

7375
if ($locale !== '') {
7476
$supportedLocales = config(App::class)->supportedLocales;
7577

7678
if (! in_array($locale, $supportedLocales, true)) {
77-
CLI::error(
79+
$output->error(
7880
sprintf(
7981
'Error: "%s" is not supported. Supported locales: %s',
8082
$locale,
@@ -94,23 +96,23 @@ protected function execute(array $arguments, array $options): int
9496
$tempCurrentDir = realpath($currentDir . $dir);
9597

9698
if ($tempCurrentDir === false) {
97-
CLI::error(sprintf('Error: Directory must be located in "%s"', $currentDir), 'light_gray', 'red');
99+
$output->error(sprintf('Error: Directory must be located in "%s"', $currentDir), 'light_gray', 'red');
98100

99101
return EXIT_USER_INPUT;
100102
}
101103

102104
if ($this->isSubdirectory($tempCurrentDir, $this->languagePath)) {
103-
CLI::error(sprintf('Error: Directory "%s" restricted to scan.', $this->languagePath), 'light_gray', 'red');
105+
$output->error(sprintf('Error: Directory "%s" restricted to scan.', $this->languagePath), 'light_gray', 'red');
104106

105107
return EXIT_USER_INPUT;
106108
}
107109

108110
$currentDir = $tempCurrentDir;
109111
}
110112

111-
$this->process($currentDir, $currentLocale);
113+
$this->process($output, $currentDir, $currentLocale);
112114

113-
CLI::write('All operations done!', 'green');
115+
$output->write('All operations done!', 'green');
114116

115117
return EXIT_SUCCESS;
116118
}
@@ -136,10 +138,9 @@ private function resolvePaths(): array
136138
];
137139
}
138140

139-
private function process(string $currentDir, string $currentLocale): void
141+
private function process(Output $output, string $currentDir, string $currentLocale): void
140142
{
141143
$showNew = $this->getValidatedOption('show-new') === true;
142-
$output = $this->getOutput();
143144

144145
$tableRows = [];
145146
$countNewKeys = 0;
@@ -152,7 +153,7 @@ private function process(string $currentDir, string $currentLocale): void
152153
'foundLanguageKeys' => $foundLanguageKeys,
153154
'badLanguageKeys' => $badLanguageKeys,
154155
'countFiles' => $countFiles,
155-
] = $this->findLanguageKeysInFiles($files);
156+
] = $this->findLanguageKeysInFiles($output, $files);
156157

157158
ksort($foundLanguageKeys);
158159

@@ -189,11 +190,11 @@ private function process(string $currentDir, string $currentLocale): void
189190

190191
if ($showNew && $tableRows !== []) {
191192
sort($tableRows);
192-
CLI::table($tableRows, ['File', 'Key']);
193+
$output->write((new Table($tableRows, ['File', 'Key']))->render());
193194
}
194195

195196
if (! $showNew && $countNewKeys > 0) {
196-
CLI::write('Note: You need to run your linting tool to fix coding standards issues.', 'white', 'red');
197+
$output->write('Note: You need to run your linting tool to fix coding standards issues.', 'white', 'red');
197198
}
198199

199200
$output->write(sprintf('Files found: %d', $countFiles), verbosity: Verbosity::Verbose);
@@ -209,7 +210,7 @@ private function process(string $currentDir, string $currentLocale): void
209210

210211
ArrayHelper::sortValuesByNatural($tableBadRows, 0);
211212

212-
CLI::table($tableBadRows, ['Bad Key', 'Filepath']);
213+
$output->write((new Table($tableBadRows, ['Bad Key', 'Filepath']))->render());
213214
}
214215
}
215216

@@ -423,12 +424,11 @@ private function isSubdirectory(string $directory, string $rootDirectory): bool
423424
*
424425
* @return array{'foundLanguageKeys': array<string, array<string, string>>, 'badLanguageKeys': array<int, array<int, string>>, 'countFiles': int}
425426
*/
426-
private function findLanguageKeysInFiles(array $files): array
427+
private function findLanguageKeysInFiles(Output $output, array $files): array
427428
{
428429
$foundLanguageKeys = [];
429430
$badLanguageKeys = [];
430431
$countFiles = 0;
431-
$output = $this->getOutput();
432432

433433
foreach ($files as $file) {
434434
if ($this->isIgnoredFile($file)) {

0 commit comments

Comments
 (0)