diff --git a/system/Commands/Cache/ClearCache.php b/system/Commands/Cache/ClearCache.php index 0a684c5859f0..82876c44cca4 100644 --- a/system/Commands/Cache/ClearCache.php +++ b/system/Commands/Cache/ClearCache.php @@ -16,7 +16,6 @@ use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Argument; use Config\Cache; @@ -39,9 +38,10 @@ protected function execute(array $arguments, array $options): int { $driver = $arguments['driver']; $config = config(Cache::class); + $output = $this->getOutput(); if (! array_key_exists($driver, $config->validHandlers)) { - CLI::error(lang('Cache.invalidHandler', [$driver])); + $output->error(lang('Cache.invalidHandler', [$driver])); return EXIT_ERROR; } @@ -49,7 +49,7 @@ protected function execute(array $arguments, array $options): int $config->handler = $driver; if (! service('cache', $config)->clean()) { - CLI::error(sprintf('Error occurred while clearing the cache using the "%s" driver.', $driver)); + $output->error(sprintf('Error occurred while clearing the cache using the "%s" driver.', $driver)); return EXIT_ERROR; } @@ -60,7 +60,7 @@ protected function execute(array $arguments, array $options): int $locator->deleteCache(); } - CLI::write(sprintf('Cache cleared using the "%s" driver.', $driver), 'green'); + $output->write(sprintf('Cache cleared using the "%s" driver.', $driver), 'green'); return EXIT_SUCCESS; } diff --git a/system/Commands/Cache/InfoCache.php b/system/Commands/Cache/InfoCache.php index 9e49b45da4e3..7ff69727738d 100644 --- a/system/Commands/Cache/InfoCache.php +++ b/system/Commands/Cache/InfoCache.php @@ -15,7 +15,7 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; +use CodeIgniter\CLI\Table; use CodeIgniter\I18n\Time; use Config\Cache; @@ -28,9 +28,10 @@ class InfoCache extends AbstractCommand protected function execute(array $arguments, array $options): int { $config = config(Cache::class); + $output = $this->getOutput(); if ($config->handler !== 'file') { - CLI::error(sprintf( + $output->error(sprintf( 'This command only supports the file cache handler. The configured handler is "%s".', $config->handler, )); @@ -52,12 +53,12 @@ protected function execute(array $arguments, array $options): int ]; } - CLI::table($tbody, [ - CLI::color('Name', 'green'), - CLI::color('Server Path', 'green'), - CLI::color('Size', 'green'), - CLI::color('Date', 'green'), - ]); + $output->write((new Table($tbody, [ + $output->color('Name', 'green'), + $output->color('Server Path', 'green'), + $output->color('Size', 'green'), + $output->color('Date', 'green'), + ]))->render()); return EXIT_SUCCESS; } diff --git a/system/Commands/Housekeeping/ClearDebugbar.php b/system/Commands/Housekeeping/ClearDebugbar.php index 87a278291487..b0af07dd0803 100644 --- a/system/Commands/Housekeeping/ClearDebugbar.php +++ b/system/Commands/Housekeeping/ClearDebugbar.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; /** * Clears all debugbar JSON files. @@ -27,15 +26,16 @@ protected function execute(array $arguments, array $options): int { helper('filesystem'); - $path = clean_path(WRITEPATH . 'debugbar'); + $path = clean_path(WRITEPATH . 'debugbar'); + $output = $this->getOutput(); if (! delete_files(WRITEPATH . 'debugbar', htdocs: true)) { - CLI::error(sprintf('Error deleting the debugbar JSON files in "%s".', $path)); + $output->error(sprintf('Error deleting the debugbar JSON files in "%s".', $path)); return EXIT_ERROR; } - CLI::write(sprintf('Cleared debugbar JSON files in "%s".', $path), 'green'); + $output->write(sprintf('Cleared debugbar JSON files in "%s".', $path), 'green'); return EXIT_SUCCESS; } diff --git a/system/Commands/Housekeeping/ClearLogs.php b/system/Commands/Housekeeping/ClearLogs.php index 008cb630deac..6115d7d534f0 100644 --- a/system/Commands/Housekeeping/ClearLogs.php +++ b/system/Commands/Housekeeping/ClearLogs.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; /** @@ -39,7 +38,7 @@ protected function interact(array &$arguments, array &$options): void return; } - if (CLI::prompt(sprintf('Delete all log files in %s?', clean_path(WRITEPATH . 'logs')), ['n', 'y']) === 'n') { + if ($this->getPrompt()->ask(sprintf('Delete all log files in %s?', clean_path(WRITEPATH . 'logs')), ['n', 'y']) === 'n') { return; } @@ -48,14 +47,16 @@ protected function interact(array &$arguments, array &$options): void protected function execute(array $arguments, array $options): int { + $output = $this->getOutput(); + if ($options['force'] === false) { if ($this->isInteractive()) { - CLI::write('Log deletion cancelled.', 'yellow'); + $output->write('Log deletion cancelled.', 'yellow'); return EXIT_SUCCESS; } - CLI::error('Log deletion aborted: pass --force to delete log files in non-interactive mode.'); + $output->error('Log deletion aborted: pass --force to delete log files in non-interactive mode.'); return EXIT_ERROR; } @@ -63,12 +64,12 @@ protected function execute(array $arguments, array $options): int helper('filesystem'); if (! delete_files(WRITEPATH . 'logs', htdocs: true)) { - CLI::error(sprintf('Failed to delete log files in %s.', clean_path(WRITEPATH . 'logs'))); + $output->error(sprintf('Failed to delete log files in %s.', clean_path(WRITEPATH . 'logs'))); return EXIT_ERROR; } - CLI::write(sprintf('Log files in %s cleared.', clean_path(WRITEPATH . 'logs')), 'green'); + $output->write(sprintf('Log files in %s cleared.', clean_path(WRITEPATH . 'logs')), 'green'); return EXIT_SUCCESS; } diff --git a/system/Commands/Server/Serve.php b/system/Commands/Server/Serve.php index d7f7e9ee7fc4..a1417635ecc7 100644 --- a/system/Commands/Server/Serve.php +++ b/system/Commands/Server/Serve.php @@ -15,7 +15,6 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; /** @@ -43,13 +42,14 @@ protected function execute(array $arguments, array $options): int { $basePort = (int) $options['port']; $status = EXIT_SUCCESS; + $output = $this->getOutput(); for ($offset = 0; $offset <= self::RETRIES; $offset++) { $port = $basePort + $offset; - CLI::write(sprintf('CodeIgniter development server started on http://%s:%s', $options['host'], $port), 'green'); - CLI::write('Press Control-C to stop.'); - CLI::newLine(); + $output->write(sprintf('CodeIgniter development server started on http://%s:%s', $options['host'], $port), 'green'); + $output->write('Press Control-C to stop.'); + $output->newLine(); passthru( $this->buildServeCommand($options['php'], $options['host'], $port, FCPATH, SYSTEMPATH . 'rewrite.php'), @@ -60,7 +60,7 @@ protected function execute(array $arguments, array $options): int return $status; } - CLI::newLine(); + $output->newLine(); } return $status; diff --git a/system/Commands/Translation/LocalizationFinder.php b/system/Commands/Translation/LocalizationFinder.php index ed50881dbee8..ddab421d8ed6 100644 --- a/system/Commands/Translation/LocalizationFinder.php +++ b/system/Commands/Translation/LocalizationFinder.php @@ -15,8 +15,9 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; +use CodeIgniter\CLI\Output; +use CodeIgniter\CLI\Table; use CodeIgniter\CLI\Verbosity; use CodeIgniter\Helpers\Array\ArrayHelper; use Config\App; @@ -67,6 +68,7 @@ protected function execute(array $arguments, array $options): int assert(is_string($dir)); $currentLocale = Locale::getDefault(); + $output = $this->getOutput(); ['currentDir' => $currentDir, 'languagePath' => $this->languagePath] = $this->resolvePaths(); @@ -74,7 +76,7 @@ protected function execute(array $arguments, array $options): int $supportedLocales = config(App::class)->supportedLocales; if (! in_array($locale, $supportedLocales, true)) { - CLI::error( + $output->error( sprintf( 'Error: "%s" is not supported. Supported locales: %s', $locale, @@ -94,13 +96,13 @@ protected function execute(array $arguments, array $options): int $tempCurrentDir = realpath($currentDir . $dir); if ($tempCurrentDir === false) { - CLI::error(sprintf('Error: Directory must be located in "%s"', $currentDir), 'light_gray', 'red'); + $output->error(sprintf('Error: Directory must be located in "%s"', $currentDir), 'light_gray', 'red'); return EXIT_USER_INPUT; } if ($this->isSubdirectory($tempCurrentDir, $this->languagePath)) { - CLI::error(sprintf('Error: Directory "%s" restricted to scan.', $this->languagePath), 'light_gray', 'red'); + $output->error(sprintf('Error: Directory "%s" restricted to scan.', $this->languagePath), 'light_gray', 'red'); return EXIT_USER_INPUT; } @@ -108,9 +110,9 @@ protected function execute(array $arguments, array $options): int $currentDir = $tempCurrentDir; } - $this->process($currentDir, $currentLocale); + $this->process($output, $currentDir, $currentLocale); - CLI::write('All operations done!', 'green'); + $output->write('All operations done!', 'green'); return EXIT_SUCCESS; } @@ -136,10 +138,9 @@ private function resolvePaths(): array ]; } - private function process(string $currentDir, string $currentLocale): void + private function process(Output $output, string $currentDir, string $currentLocale): void { $showNew = $this->getValidatedOption('show-new') === true; - $output = $this->getOutput(); $tableRows = []; $countNewKeys = 0; @@ -152,7 +153,7 @@ private function process(string $currentDir, string $currentLocale): void 'foundLanguageKeys' => $foundLanguageKeys, 'badLanguageKeys' => $badLanguageKeys, 'countFiles' => $countFiles, - ] = $this->findLanguageKeysInFiles($files); + ] = $this->findLanguageKeysInFiles($output, $files); ksort($foundLanguageKeys); @@ -189,11 +190,11 @@ private function process(string $currentDir, string $currentLocale): void if ($showNew && $tableRows !== []) { sort($tableRows); - CLI::table($tableRows, ['File', 'Key']); + $output->write((new Table($tableRows, ['File', 'Key']))->render()); } if (! $showNew && $countNewKeys > 0) { - CLI::write('Note: You need to run your linting tool to fix coding standards issues.', 'white', 'red'); + $output->write('Note: You need to run your linting tool to fix coding standards issues.', 'white', 'red'); } $output->write(sprintf('Files found: %d', $countFiles), verbosity: Verbosity::Verbose); @@ -209,7 +210,7 @@ private function process(string $currentDir, string $currentLocale): void ArrayHelper::sortValuesByNatural($tableBadRows, 0); - CLI::table($tableBadRows, ['Bad Key', 'Filepath']); + $output->write((new Table($tableBadRows, ['Bad Key', 'Filepath']))->render()); } } @@ -423,12 +424,11 @@ private function isSubdirectory(string $directory, string $rootDirectory): bool * * @return array{'foundLanguageKeys': array>, 'badLanguageKeys': array>, 'countFiles': int} */ - private function findLanguageKeysInFiles(array $files): array + private function findLanguageKeysInFiles(Output $output, array $files): array { $foundLanguageKeys = []; $badLanguageKeys = []; $countFiles = 0; - $output = $this->getOutput(); foreach ($files as $file) { if ($this->isIgnoredFile($file)) { diff --git a/system/Commands/Translation/LocalizationSync.php b/system/Commands/Translation/LocalizationSync.php index 68dd1c09ccc9..3574c2e19482 100644 --- a/system/Commands/Translation/LocalizationSync.php +++ b/system/Commands/Translation/LocalizationSync.php @@ -15,8 +15,8 @@ use CodeIgniter\CLI\AbstractCommand; use CodeIgniter\CLI\Attributes\Command; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; +use CodeIgniter\CLI\Output; use CodeIgniter\Exceptions\LogicException; use Config\App; use ErrorException; @@ -65,17 +65,18 @@ protected function execute(array $arguments, array $options): int $this->languagePath = APPPATH . 'Language'; $supportedLocales = config(App::class)->supportedLocales; + $output = $this->getOutput(); if ($locale === '') { $locale = Locale::getDefault(); } if (! in_array($locale, $supportedLocales, true)) { - return $this->errorUnsupportedLocale($locale); + return $this->errorUnsupportedLocale($output, $locale); } if ($target === '') { - CLI::error( + $output->error( sprintf( 'Error: "--target" is not configured. Supported locales: %s', implode(', ', $supportedLocales), @@ -88,11 +89,11 @@ protected function execute(array $arguments, array $options): int } if (! in_array($target, $supportedLocales, true)) { - return $this->errorUnsupportedLocale($target); + return $this->errorUnsupportedLocale($output, $target); } if ($target === $locale) { - CLI::error('Error: You cannot have the same values for "--target" and "--locale".', 'light_gray', 'red'); + $output->error('Error: You cannot have the same values for "--target" and "--locale".', 'light_gray', 'red'); return EXIT_USER_INPUT; } @@ -101,11 +102,11 @@ protected function execute(array $arguments, array $options): int $this->languagePath = SUPPORTPATH . 'Language'; } - if ($this->process($locale, $target) === EXIT_ERROR) { + if ($this->process($output, $locale, $target) === EXIT_ERROR) { return EXIT_ERROR; } - CLI::write('All operations done!', 'green'); + $output->write('All operations done!', 'green'); return EXIT_SUCCESS; } @@ -113,9 +114,9 @@ protected function execute(array $arguments, array $options): int /** * Writes the unsupported-locale error and returns the user-input exit code. */ - private function errorUnsupportedLocale(string $locale): int + private function errorUnsupportedLocale(Output $output, string $locale): int { - CLI::error( + $output->error( sprintf( 'Error: "%s" is not supported. Supported locales: %s', $locale, @@ -128,13 +129,13 @@ private function errorUnsupportedLocale(string $locale): int return EXIT_USER_INPUT; } - private function process(string $originalLocale, string $targetLocale): int + private function process(Output $output, string $originalLocale, string $targetLocale): int { $originalLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $originalLocale; $targetLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $targetLocale; if (! is_dir($originalLocaleDir)) { - CLI::error( + $output->error( sprintf('Error: The "%s" directory was not found.', clean_path($originalLocaleDir)), 'light_gray', 'red', @@ -149,7 +150,7 @@ private function process(string $originalLocale, string $targetLocale): int throw new ErrorException(); } } catch (ErrorException $e) { - CLI::error( + $output->error( sprintf('Error: The target directory "%s" cannot be accessed.', clean_path($targetLocaleDir)), 'light_gray', 'red', diff --git a/tests/system/Commands/Translation/LocalizationSyncTest.php b/tests/system/Commands/Translation/LocalizationSyncTest.php index 515c42ac0f19..b0f7080464b8 100644 --- a/tests/system/Commands/Translation/LocalizationSyncTest.php +++ b/tests/system/Commands/Translation/LocalizationSyncTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Commands\Translation; +use CodeIgniter\CLI\Output; use CodeIgniter\Exceptions\LogicException; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\ReflectionHelper; @@ -225,14 +226,15 @@ public function testProcessWithInvalidOption(): void $command = new LocalizationSync(service('commands')); $this->setPrivateProperty($command, 'languagePath', $langPath); $runner = self::getPrivateMethodInvoker($command, 'process'); + $output = new Output(); - $status = $runner('de', 'jp'); + $status = $runner($output, 'de', 'jp'); $this->assertSame(EXIT_ERROR, $status); $this->assertStringContainsString('Error: The "ROOTPATH/tests/_support/Language/de" directory was not found.', $this->getStreamFilterBuffer()); chmod($langPath, 0544); - $status = $runner('en', 'jp'); + $status = $runner($output, 'en', 'jp'); chmod($langPath, 0775); $this->assertSame(EXIT_ERROR, $status);