diff --git a/system/Boot.php b/system/Boot.php index 19856e8c676b..d506fd97b9f6 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -428,17 +428,6 @@ protected static function initializeConsole(): Console protected static function runCommand(Console $console): int { - $exitCode = $console->initialize()->run(); - - if (! is_int($exitCode)) { - @trigger_error(sprintf( - 'Since v4.8.0, commands must return an integer exit code. Last command "%s" exited with %s. Defaulting to EXIT_SUCCESS.', - $console->getCommand(), - get_debug_type($exitCode), - ), E_USER_DEPRECATED); - $exitCode = EXIT_SUCCESS; - } - - return $exitCode; + return $console->initialize()->run(); } } diff --git a/system/CLI/BaseCommand.php b/system/CLI/BaseCommand.php index 6becf7f28435..5446b40c5906 100644 --- a/system/CLI/BaseCommand.php +++ b/system/CLI/BaseCommand.php @@ -101,7 +101,7 @@ public function __construct(LoggerInterface $logger, Commands $commands) * * @param array $params * - * @return int|void + * @return int|null */ abstract public function run(array $params); @@ -110,7 +110,7 @@ abstract public function run(array $params); * * @param array $params * - * @return int|void + * @return int|null * * @throws ReflectionException */ diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index 8f623d509c0a..bb628c262b4c 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -69,11 +69,20 @@ public function run(string $command, array $params) Events::trigger('pre_command'); - $exit = $class->run($params); + $exitCode = $class->run($params); Events::trigger('post_command'); - return $exit; + if (! is_int($exitCode)) { + @trigger_error(sprintf( + 'Since v4.8.0, commands must return an integer exit code. Last command "%s" exited with %s. Defaulting to EXIT_SUCCESS.', + $command, + get_debug_type($exitCode), + ), E_USER_DEPRECATED); + $exitCode = EXIT_SUCCESS; + } + + return $exitCode; } /** diff --git a/system/CLI/Console.php b/system/CLI/Console.php index f5473cfb5784..e0cba43adde9 100644 --- a/system/CLI/Console.php +++ b/system/CLI/Console.php @@ -39,7 +39,7 @@ class Console * * @param list $tokens * - * @return int|null Exit code or null for legacy commands that don't return an exit code. + * @return int Exit code */ public function run(array $tokens = []) { diff --git a/system/Commands/Cache/InfoCache.php b/system/Commands/Cache/InfoCache.php index abeabd7fed24..ff93eaf82287 100644 --- a/system/Commands/Cache/InfoCache.php +++ b/system/Commands/Cache/InfoCache.php @@ -63,7 +63,7 @@ public function run(array $params) if ($config->handler !== 'file') { CLI::error('This command only supports the file cache handler.'); - return; + return EXIT_ERROR; } $cache = CacheFactory::getHandler($config); @@ -87,5 +87,7 @@ public function run(array $params) ]; CLI::table($tbody, $thead); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Database/CreateDatabase.php b/system/Commands/Database/CreateDatabase.php index facd060abf6e..cfc5656617d3 100644 --- a/system/Commands/Database/CreateDatabase.php +++ b/system/Commands/Database/CreateDatabase.php @@ -115,7 +115,7 @@ public function run(array $params) CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red'); CLI::newLine(); - return; + return EXIT_ERROR; } unset($dbName); @@ -130,7 +130,7 @@ public function run(array $params) CLI::error('Database creation failed.', 'light_gray', 'red'); CLI::newLine(); - return; + return EXIT_ERROR; // @codeCoverageIgnoreEnd } } elseif (! Database::forge()->createDatabase($name)) { @@ -138,14 +138,18 @@ public function run(array $params) CLI::error('Database creation failed.', 'light_gray', 'red'); CLI::newLine(); - return; + return EXIT_ERROR; // @codeCoverageIgnoreEnd } CLI::write("Database \"{$name}\" successfully created.", 'green'); CLI::newLine(); + + return EXIT_SUCCESS; } catch (Throwable $e) { $this->showError($e); + + return EXIT_ERROR; } finally { Factories::reset('config'); Database::connect(null, false); diff --git a/system/Commands/Database/Migrate.php b/system/Commands/Database/Migrate.php index 9be1a894d4e1..50d120c1b6f6 100644 --- a/system/Commands/Database/Migrate.php +++ b/system/Commands/Database/Migrate.php @@ -99,9 +99,12 @@ public function run(array $params) CLI::write(lang('Migrations.migrated'), 'green'); + return EXIT_SUCCESS; // @codeCoverageIgnoreStart } catch (Throwable $e) { $this->showError($e); + + return EXIT_ERROR; // @codeCoverageIgnoreEnd } } diff --git a/system/Commands/Database/MigrateRefresh.php b/system/Commands/Database/MigrateRefresh.php index b7863a001438..9496d83062bb 100644 --- a/system/Commands/Database/MigrateRefresh.php +++ b/system/Commands/Database/MigrateRefresh.php @@ -79,16 +79,15 @@ public function run(array $params) $force = array_key_exists('f', $params) || CLI::getOption('f'); if (! $force && CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'n') { - return; + return EXIT_ERROR; } $params['f'] = null; // @codeCoverageIgnoreEnd } - $this->withSignalsBlocked(function () use ($params): void { - $this->call('migrate:rollback', $params); - $this->call('migrate', $params); - }); + return $this->withSignalsBlocked( + fn (): int => $this->call('migrate:rollback', $params) | $this->call('migrate', $params), + ); } } diff --git a/system/Commands/Database/MigrateRollback.php b/system/Commands/Database/MigrateRollback.php index 5fa6ac171bfb..d2ff23be36df 100644 --- a/system/Commands/Database/MigrateRollback.php +++ b/system/Commands/Database/MigrateRollback.php @@ -77,7 +77,7 @@ public function run(array $params) $force = array_key_exists('f', $params) || CLI::getOption('f'); if (! $force && CLI::prompt(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'n') { - return null; + return EXIT_ERROR; } // @codeCoverageIgnoreEnd } @@ -101,12 +101,20 @@ public function run(array $params) CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow'); - $this->withSignalsBlocked(static function () use ($runner, $batch): void { + $exit = $this->withSignalsBlocked(static function () use ($runner, $batch): int { if (! $runner->regress($batch)) { CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red'); // @codeCoverageIgnore + + return EXIT_ERROR; } + + return EXIT_SUCCESS; }); + if ($exit !== EXIT_SUCCESS) { + return $exit; + } + $messages = $runner->getCliMessages(); foreach ($messages as $message) { @@ -115,12 +123,13 @@ public function run(array $params) CLI::write('Done rolling back migrations.', 'green'); + return EXIT_SUCCESS; // @codeCoverageIgnoreStart } catch (Throwable $e) { $this->showError($e); + + return EXIT_ERROR; // @codeCoverageIgnoreEnd } - - return null; } } diff --git a/system/Commands/Database/MigrateStatus.php b/system/Commands/Database/MigrateStatus.php index 9506c2eae082..39d2564a7bf2 100644 --- a/system/Commands/Database/MigrateStatus.php +++ b/system/Commands/Database/MigrateStatus.php @@ -150,7 +150,7 @@ public function run(array $params) CLI::error(lang('Migrations.noneFound'), 'light_gray', 'red'); CLI::newLine(); - return; + return EXIT_ERROR; // @codeCoverageIgnoreEnd } @@ -164,5 +164,7 @@ public function run(array $params) ]; CLI::table($status, $headers); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Database/Seed.php b/system/Commands/Database/Seed.php index fc1c0f1ae62e..97b6f946310a 100644 --- a/system/Commands/Database/Seed.php +++ b/system/Commands/Database/Seed.php @@ -77,8 +77,12 @@ public function run(array $params) try { $seeder->call($seedName); + + return EXIT_SUCCESS; } catch (Throwable $e) { $this->showError($e); + + return EXIT_ERROR; } } } diff --git a/system/Commands/Encryption/GenerateKey.php b/system/Commands/Encryption/GenerateKey.php index 7a0d47d0d542..ec5007560ed0 100644 --- a/system/Commands/Encryption/GenerateKey.php +++ b/system/Commands/Encryption/GenerateKey.php @@ -89,14 +89,14 @@ public function run(array $params) CLI::write($encodedKey, 'yellow'); CLI::newLine(); - return; + return EXIT_SUCCESS; } if (! $this->setNewEncryptionKey($encodedKey, $params)) { CLI::write('Error in setting new encryption key to .env file.', 'light_gray', 'red'); CLI::newLine(); - return; + return EXIT_ERROR; } // force DotEnv to reload the new env vars @@ -107,6 +107,8 @@ public function run(array $params) CLI::write('Application\'s new encryption key was successfully set.', 'green'); CLI::newLine(); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/CellGenerator.php b/system/Commands/Generators/CellGenerator.php index 04c5bd8adf04..3580f48135ab 100644 --- a/system/Commands/Generators/CellGenerator.php +++ b/system/Commands/Generators/CellGenerator.php @@ -102,6 +102,6 @@ public function run(array $params) $this->generateView($namespace . $viewName, $params); - return 0; + return EXIT_SUCCESS; } } diff --git a/system/Commands/Generators/CommandGenerator.php b/system/Commands/Generators/CommandGenerator.php index a0872f63c2ba..93e1836b47a0 100644 --- a/system/Commands/Generators/CommandGenerator.php +++ b/system/Commands/Generators/CommandGenerator.php @@ -86,6 +86,8 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.command'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/ConfigGenerator.php b/system/Commands/Generators/ConfigGenerator.php index 7b1d5f21ff45..e62abbe69f4a 100644 --- a/system/Commands/Generators/ConfigGenerator.php +++ b/system/Commands/Generators/ConfigGenerator.php @@ -82,6 +82,8 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.config'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/ControllerGenerator.php b/system/Commands/Generators/ControllerGenerator.php index 287f92f87c30..672b344082ca 100644 --- a/system/Commands/Generators/ControllerGenerator.php +++ b/system/Commands/Generators/ControllerGenerator.php @@ -88,6 +88,8 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.controller'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/EntityGenerator.php b/system/Commands/Generators/EntityGenerator.php index 09c350560638..1040b33bbcff 100644 --- a/system/Commands/Generators/EntityGenerator.php +++ b/system/Commands/Generators/EntityGenerator.php @@ -82,5 +82,7 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.entity'; $this->generateClass($params); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Generators/FilterGenerator.php b/system/Commands/Generators/FilterGenerator.php index c723da3afab7..d13c0feff45a 100644 --- a/system/Commands/Generators/FilterGenerator.php +++ b/system/Commands/Generators/FilterGenerator.php @@ -82,5 +82,7 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.filter'; $this->generateClass($params); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Generators/MigrationGenerator.php b/system/Commands/Generators/MigrationGenerator.php index b5e64ec1bce1..f2caf53d93ed 100644 --- a/system/Commands/Generators/MigrationGenerator.php +++ b/system/Commands/Generators/MigrationGenerator.php @@ -93,6 +93,8 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.migration'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index 5450bda79b4c..8dfa26229028 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -86,6 +86,8 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.model'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/ScaffoldGenerator.php b/system/Commands/Generators/ScaffoldGenerator.php index 3b1ef7957c9b..727f19298731 100644 --- a/system/Commands/Generators/ScaffoldGenerator.php +++ b/system/Commands/Generators/ScaffoldGenerator.php @@ -115,9 +115,13 @@ public function run(array $params) $class = $params[0] ?? CLI::getSegment(2); // Call those commands! - $this->call('make:controller', array_merge([$class], $controllerOpts, $options)); - $this->call('make:model', array_merge([$class], $modelOpts, $options)); - $this->call('make:migration', array_merge([$class], $options)); - $this->call('make:seeder', array_merge([$class], $options)); + $exit1 = $this->call('make:controller', array_merge([$class], $controllerOpts, $options)); + $exit2 = $this->call('make:model', array_merge([$class], $modelOpts, $options)); + $exit3 = $this->call('make:migration', array_merge([$class], $options)); + $exit4 = $this->call('make:seeder', array_merge([$class], $options)); + + assert(is_int($exit1) && is_int($exit2) && is_int($exit3) && is_int($exit4)); + + return $exit1 | $exit2 | $exit3 | $exit4; } } diff --git a/system/Commands/Generators/SeederGenerator.php b/system/Commands/Generators/SeederGenerator.php index 0549ad840a2d..605bc2c16ead 100644 --- a/system/Commands/Generators/SeederGenerator.php +++ b/system/Commands/Generators/SeederGenerator.php @@ -82,5 +82,7 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.seeder'; $this->generateClass($params); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Generators/TestGenerator.php b/system/Commands/Generators/TestGenerator.php index 47f71294c823..3ff8e39d17a4 100644 --- a/system/Commands/Generators/TestGenerator.php +++ b/system/Commands/Generators/TestGenerator.php @@ -89,6 +89,8 @@ public function run(array $params) $autoload->addNamespace('Tests', ROOTPATH . 'tests'); $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/TransformerGenerator.php b/system/Commands/Generators/TransformerGenerator.php index 6e9143b5dc48..82837b6c1d87 100644 --- a/system/Commands/Generators/TransformerGenerator.php +++ b/system/Commands/Generators/TransformerGenerator.php @@ -82,6 +82,8 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.transformer'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Commands/Generators/ValidationGenerator.php b/system/Commands/Generators/ValidationGenerator.php index d9e3d495a769..64e0c4ff9a34 100644 --- a/system/Commands/Generators/ValidationGenerator.php +++ b/system/Commands/Generators/ValidationGenerator.php @@ -82,5 +82,7 @@ public function run(array $params) $this->classNameLang = 'CLI.generator.className.validation'; $this->generateClass($params); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Generators/Views/command.tpl.php b/system/Commands/Generators/Views/command.tpl.php index bfbdcc4a1741..840b645719d1 100644 --- a/system/Commands/Generators/Views/command.tpl.php +++ b/system/Commands/Generators/Views/command.tpl.php @@ -68,9 +68,11 @@ public function run(array $params) $this->directory = 'Commands'; $this->template = 'command.tpl.php'; - $this->execute($params); + $this->generateClass($params); - // + // your command logic here + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Help.php b/system/Commands/Help.php index 76913e8a33fc..84e50c0d5426 100644 --- a/system/Commands/Help.php +++ b/system/Commands/Help.php @@ -78,10 +78,12 @@ public function run(array $params) $commands = $this->commands->getCommands(); if (! $this->commands->verifyCommand($command, $commands)) { - return; + return EXIT_ERROR; } $class = new $commands[$command]['class']($this->logger, $this->commands); $class->showHelp(); + + return EXIT_SUCCESS; } } diff --git a/system/Commands/Server/Serve.php b/system/Commands/Server/Serve.php index 594e4e587e27..f02ec0c0933d 100644 --- a/system/Commands/Server/Serve.php +++ b/system/Commands/Server/Serve.php @@ -113,7 +113,9 @@ public function run(array $params) if ($status !== EXIT_SUCCESS && $this->portOffset < $this->tries) { $this->portOffset++; - $this->run($params); + return $this->run($params); } + + return $status; } } diff --git a/system/Commands/Utilities/Environment.php b/system/Commands/Utilities/Environment.php index 778e1833becb..44a0a6866ffe 100644 --- a/system/Commands/Utilities/Environment.php +++ b/system/Commands/Utilities/Environment.php @@ -89,7 +89,7 @@ public function run(array $params) CLI::write(sprintf('Your environment is currently set as %s.', CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'))); CLI::newLine(); - return EXIT_ERROR; + return EXIT_SUCCESS; } $env = strtolower(array_shift($params)); diff --git a/system/Commands/Utilities/Namespaces.php b/system/Commands/Utilities/Namespaces.php index 8dda8ce0732e..498e4deba0dd 100644 --- a/system/Commands/Utilities/Namespaces.php +++ b/system/Commands/Utilities/Namespaces.php @@ -89,6 +89,8 @@ public function run(array $params) ]; CLI::table($tbody, $thead); + + return EXIT_SUCCESS; } private function outputAllNamespaces(array $params): array diff --git a/system/Commands/Utilities/Publish.php b/system/Commands/Utilities/Publish.php index 13ffa96eafc6..0fc6ef44efc8 100644 --- a/system/Commands/Utilities/Publish.php +++ b/system/Commands/Utilities/Publish.php @@ -86,9 +86,11 @@ public function run(array $params) CLI::write(lang('Publisher.publishMissingNamespace', [$directory, $namespace])); } - return; + return EXIT_ERROR; } + $exit = EXIT_SUCCESS; + foreach ($publishers as $publisher) { if ($publisher->publish()) { CLI::write(lang('Publisher.publishSuccess', [ @@ -96,18 +98,24 @@ public function run(array $params) count($publisher->getPublished()), $publisher->getDestination(), ]), 'green'); - } else { - CLI::error(lang('Publisher.publishFailure', [ - $publisher::class, - $publisher->getDestination(), - ]), 'light_gray', 'red'); - foreach ($publisher->getErrors() as $file => $exception) { - CLI::write($file); - CLI::error($exception->getMessage()); - CLI::newLine(); - } + continue; } + + CLI::error(lang('Publisher.publishFailure', [ + $publisher::class, + $publisher->getDestination(), + ]), 'light_gray', 'red'); + + foreach ($publisher->getErrors() as $file => $exception) { + CLI::write($file); + CLI::error($exception->getMessage()); + CLI::newLine(); + } + + $exit = EXIT_ERROR; } + + return $exit; } } diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index 86f7bd74158f..b3fcdf37e6fa 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -202,10 +202,10 @@ public function run(array $params) CLI::table($tbody, $thead); - $this->showRequiredFilters(); + return $this->showRequiredFilters(); } - private function showRequiredFilters(): void + private function showRequiredFilters(): int { $filterCollector = new FilterCollector(); @@ -226,5 +226,7 @@ private function showRequiredFilters(): void } CLI::write(' Required After Filters: ' . implode(', ', $filters)); + + return EXIT_SUCCESS; } } diff --git a/tests/_support/Commands/AppInfo.php b/tests/_support/Commands/AppInfo.php index 94502e0706f5..80e080b8bc85 100644 --- a/tests/_support/Commands/AppInfo.php +++ b/tests/_support/Commands/AppInfo.php @@ -32,26 +32,24 @@ public function run(array $params): int { CLI::write(sprintf('CodeIgniter Version: %s', CodeIgniter::CI_VERSION)); - return 0; + return EXIT_SUCCESS; } public function bomb(): int { try { CLI::color('test', 'white', 'Background'); + + return EXIT_SUCCESS; } catch (RuntimeException $e) { $this->showError($e); - return 1; + return EXIT_ERROR; } - - return 0; } public function helpMe(): int { - $this->call('help'); - - return 0; + return $this->call('help'); } } diff --git a/tests/_support/Commands/InvalidCommand.php b/tests/_support/Commands/InvalidCommand.php index 5604ba19fd36..fcd30cd3befe 100644 --- a/tests/_support/Commands/InvalidCommand.php +++ b/tests/_support/Commands/InvalidCommand.php @@ -29,8 +29,10 @@ public function __construct() throw new ReflectionException(); } - public function run(array $params): void + public function run(array $params): int { CLI::write('CI Version: ' . CLI::color(CodeIgniter::CI_VERSION, 'red')); + + return EXIT_SUCCESS; } } diff --git a/tests/_support/Commands/LanguageCommand.php b/tests/_support/Commands/LanguageCommand.php index 17fba1881c94..dac34083708f 100644 --- a/tests/_support/Commands/LanguageCommand.php +++ b/tests/_support/Commands/LanguageCommand.php @@ -29,7 +29,7 @@ class LanguageCommand extends BaseCommand '--sort' => 'Turn on/off the sortImports flag.', ]; - public function run(array $params): void + public function run(array $params): int { $this->setHasClassName(false); $params[0] = 'Foobar'; @@ -42,6 +42,8 @@ public function run(array $params): void $this->setSortImports($sort); $this->generateClass($params); + + return EXIT_SUCCESS; } protected function prepare(string $class): string diff --git a/tests/_support/Commands/Unsuffixable.php b/tests/_support/Commands/Unsuffixable.php index d5cb501ec54b..48856c82c286 100644 --- a/tests/_support/Commands/Unsuffixable.php +++ b/tests/_support/Commands/Unsuffixable.php @@ -67,7 +67,7 @@ class Unsuffixable extends BaseCommand /** * Actually execute a command. */ - public function run(array $params): void + public function run(array $params): int { $this->component = 'Command'; $this->directory = 'Commands'; @@ -75,5 +75,7 @@ public function run(array $params): void $this->setEnabledSuffixing(false); $this->generateClass($params); + + return EXIT_SUCCESS; } }