From 100f6f6a533037b4c1b3cc630ed03e267406ef59 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 30 Oct 2026 17:03:25 +0800 Subject: [PATCH] refactor: write the generator commands through `$this->getOutput()` --- system/CLI/AbstractGeneratorCommand.php | 26 ++++++++++--------- .../Commands/Generators/CommandGenerator.php | 3 +-- .../Generators/ControllerGenerator.php | 3 +-- .../Generators/MigrationGenerator.php | 6 ++--- system/Commands/Generators/ModelGenerator.php | 3 +-- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/system/CLI/AbstractGeneratorCommand.php b/system/CLI/AbstractGeneratorCommand.php index b09b5b2bde8a..57a1cc294810 100644 --- a/system/CLI/AbstractGeneratorCommand.php +++ b/system/CLI/AbstractGeneratorCommand.php @@ -278,7 +278,7 @@ protected function buildPath(string $class): string $basePath = $this->getBasePath($namespace); if ($basePath === null) { - CLI::error(lang('CLI.namespaceNotDefined', [$namespace])); + $this->getOutput()->error(lang('CLI.namespaceNotDefined', [$namespace])); return ''; } @@ -323,8 +323,10 @@ protected function getNamespace(): string */ private function generate(string $class): int { + $output = $this->getOutput(); + if (preg_match(self::CLASS_NAME_PATTERN, $class) !== 1) { - CLI::error(lang('CLI.generator.invalidClassName', [$class])); + $output->error(lang('CLI.generator.invalidClassName', [$class])); return EXIT_ERROR; } @@ -335,15 +337,15 @@ private function generate(string $class): int return EXIT_ERROR; } - return $this->generateFile($target, $this->buildContent($class)); + return $this->generateFile($output, $target, $this->buildContent($class)); } /** * Writes the generated file to disk with all the safety checks around that. */ - private function generateFile(string $target, string $content): int + private function generateFile(Output $output, string $target, string $content): int { - if (str_starts_with($target, SYSTEMPATH) && ! $this->confirmCodeIgniterNamespaceOnce()) { + if (str_starts_with($target, SYSTEMPATH) && ! $this->confirmCodeIgniterNamespaceOnce($output)) { return EXIT_SUCCESS; } @@ -351,7 +353,7 @@ private function generateFile(string $target, string $content): int $force = $this->hasOption('force') && $this->getValidatedOption('force') === true; if (! $force && $isFile) { - CLI::error(lang('CLI.generator.fileExist', [clean_path($target)])); + $output->error(lang('CLI.generator.fileExist', [clean_path($target)])); return EXIT_ERROR; } @@ -366,31 +368,31 @@ private function generateFile(string $target, string $content): int if (! write_file($target, $content)) { // @codeCoverageIgnoreStart - CLI::error(lang('CLI.generator.fileError', [clean_path($target)])); + $output->error(lang('CLI.generator.fileError', [clean_path($target)])); return EXIT_ERROR; // @codeCoverageIgnoreEnd } if ($isFile) { - CLI::write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow'); + $output->write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow'); } else { - CLI::write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green'); + $output->write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green'); } return EXIT_SUCCESS; } - private function confirmCodeIgniterNamespaceOnce(): bool + private function confirmCodeIgniterNamespaceOnce(Output $output): bool { if ($this->confirmedCodeIgniterNamespace !== null) { return $this->confirmedCodeIgniterNamespace; } - CLI::write(lang('CLI.generator.usingCINamespace'), 'yellow'); + $output->write(lang('CLI.generator.usingCINamespace'), 'yellow'); if ($this->isInteractive() && $this->getPrompt()->ask(lang('CLI.generator.confirmContinue'), ['y', 'n'], 'required') === 'n') { - CLI::write(lang('CLI.generator.cancelOperation'), 'yellow'); + $output->write(lang('CLI.generator.cancelOperation'), 'yellow'); return $this->confirmedCodeIgniterNamespace = false; } diff --git a/system/Commands/Generators/CommandGenerator.php b/system/Commands/Generators/CommandGenerator.php index 2c9a16e6dd55..07aa44f772a5 100644 --- a/system/Commands/Generators/CommandGenerator.php +++ b/system/Commands/Generators/CommandGenerator.php @@ -16,7 +16,6 @@ use CodeIgniter\CLI\AbstractGeneratorCommand; use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\Attributes\GeneratorCommand; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; #[Command(name: 'make:command', description: 'Generates a new spark command.', group: 'Generators')] @@ -75,7 +74,7 @@ protected function execute(array $arguments, array $options): int $type = $this->getValidatedOption('type'); if ($type !== 'basic' && $type !== 'generator') { - CLI::error(lang('CLI.generator.invalidCommandType', [$type])); + $this->getOutput()->error(lang('CLI.generator.invalidCommandType', [$type])); return EXIT_ERROR; } diff --git a/system/Commands/Generators/ControllerGenerator.php b/system/Commands/Generators/ControllerGenerator.php index a8b4301b94b7..172be8cd6e68 100644 --- a/system/Commands/Generators/ControllerGenerator.php +++ b/system/Commands/Generators/ControllerGenerator.php @@ -16,7 +16,6 @@ use CodeIgniter\CLI\AbstractGeneratorCommand; use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\Attributes\GeneratorCommand; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; use CodeIgniter\Controller; use CodeIgniter\RESTful\ResourceController; @@ -69,7 +68,7 @@ protected function execute(array $arguments, array $options): int $type = $this->getResourceType(); if (! in_array($type, [null, 'controller', 'presenter'], true)) { - CLI::error(lang('CLI.generator.invalidParentClass', [$type])); + $this->getOutput()->error(lang('CLI.generator.invalidParentClass', [$type])); return EXIT_ERROR; } diff --git a/system/Commands/Generators/MigrationGenerator.php b/system/Commands/Generators/MigrationGenerator.php index 81d4270b6beb..e5e012649a76 100644 --- a/system/Commands/Generators/MigrationGenerator.php +++ b/system/Commands/Generators/MigrationGenerator.php @@ -16,7 +16,6 @@ use CodeIgniter\CLI\AbstractGeneratorCommand; use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\Attributes\GeneratorCommand; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; use Config\Database; use Config\Migrations; @@ -78,15 +77,16 @@ protected function execute(array $arguments, array $options): int if ($this->getValidatedOption('session') === true) { $group = $this->getDatabaseGroup(); $driver = $this->getDatabaseDriver($group); + $output = $this->getOutput(); if ($driver === null) { - CLI::error(lang('CLI.generator.undefinedDatabaseGroup', [$group])); + $output->error(lang('CLI.generator.undefinedDatabaseGroup', [$group])); return EXIT_ERROR; } if ($driver !== 'MySQLi' && $driver !== 'Postgre') { - CLI::error(lang('CLI.generator.unsupportedSessionDriver', [$group, $driver])); + $output->error(lang('CLI.generator.unsupportedSessionDriver', [$group, $driver])); return EXIT_ERROR; } diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index b7d3e97b4c0d..f736a559537e 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -16,7 +16,6 @@ use CodeIgniter\CLI\AbstractGeneratorCommand; use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\Attributes\GeneratorCommand; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Option; #[Command(name: 'make:model', description: 'Generates a new model file.', group: 'Generators')] @@ -78,7 +77,7 @@ protected function execute(array $arguments, array $options): int $return = $this->getValidatedOption('return'); if (! in_array($return, self::RETURN_TYPES, true)) { - CLI::error(lang('CLI.generator.invalidReturnType', [$return])); + $this->getOutput()->error(lang('CLI.generator.invalidReturnType', [$return])); return EXIT_ERROR; }