From d6a06e889c130601d5375692a45b0393037c9484 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Tue, 30 Jun 2026 14:07:24 +0800 Subject: [PATCH] refactor: migrate `env` as a modern command --- system/Commands/Utilities/Environment.php | 94 ++++++------------- tests/system/CLI/ConsoleTest.php | 2 +- .../Utilities/EnvironmentCommandTest.php | 7 -- 3 files changed, 32 insertions(+), 71 deletions(-) diff --git a/system/Commands/Utilities/Environment.php b/system/Commands/Utilities/Environment.php index 44a0a6866ffe..74b94776a95d 100644 --- a/system/Commands/Utilities/Environment.php +++ b/system/Commands/Utilities/Environment.php @@ -13,8 +13,10 @@ namespace CodeIgniter\Commands\Utilities; -use CodeIgniter\CLI\BaseCommand; +use CodeIgniter\CLI\AbstractCommand; +use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\CLI; +use CodeIgniter\CLI\Input\Argument; use CodeIgniter\Config\DotEnv; use Config\Paths; @@ -22,53 +24,9 @@ * Command to display the current environment, * or set a new one in the `.env` file. */ -final class Environment extends BaseCommand +#[Command(name: 'env', description: 'Retrieves the current environment, or set a new one.', group: 'CodeIgniter')] +class Environment extends AbstractCommand { - /** - * The group the command is lumped under - * when listing commands. - * - * @var string - */ - protected $group = 'CodeIgniter'; - - /** - * The Command's name - * - * @var string - */ - protected $name = 'env'; - - /** - * The Command's short description - * - * @var string - */ - protected $description = 'Retrieves the current environment, or set a new one.'; - - /** - * The Command's usage - * - * @var string - */ - protected $usage = 'env []'; - - /** - * The Command's arguments - * - * @var array - */ - protected $arguments = [ - 'environment' => '[Optional] The new environment to set. If none is provided, this will print the current environment.', - ]; - - /** - * The Command's options - * - * @var array - */ - protected $options = []; - /** * Allowed values for environment. `testing` is excluded * since spark won't work on it. @@ -80,44 +38,56 @@ final class Environment extends BaseCommand 'development', ]; - /** - * @return int - */ - public function run(array $params) + protected function configure(): void { - if (! isset($params[0])) { - CLI::write(sprintf('Your environment is currently set as %s.', CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'))); - CLI::newLine(); + $this->addArgument(new Argument( + name: 'environment', + description: 'The new environment to set. If none is provided, the current environment is printed.', + default: '', + )); + } + + protected function execute(array $arguments, array $options): int + { + $env = $arguments['environment']; + assert(is_string($env)); + + if ($env === '') { + CLI::write(sprintf( + 'Your environment is currently set as %s.', + CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'), + )); return EXIT_SUCCESS; } - $env = strtolower(array_shift($params)); + $env = strtolower($env); if ($env === 'testing') { CLI::error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red'); CLI::error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red'); - CLI::newLine(); return EXIT_ERROR; } if (! in_array($env, self::$knownTypes, true)) { - CLI::error(sprintf('Invalid environment type "%s". Expected one of "%s".', $env, implode('" and "', self::$knownTypes)), 'light_gray', 'red'); - CLI::newLine(); + CLI::error(sprintf( + 'Invalid environment type "%s". Expected one of "%s".', + $env, + implode('" and "', self::$knownTypes), + ), 'light_gray', 'red'); return EXIT_ERROR; } if (! $this->writeNewEnvironmentToEnvFile($env)) { CLI::error('Error in writing new environment to .env file.', 'light_gray', 'red'); - CLI::newLine(); return EXIT_ERROR; } - // force DotEnv to reload the new environment - // however we cannot redefine the ENVIRONMENT constant + // Reload DotEnv with the new environment. The ENVIRONMENT constant + // only takes the new value on the next script execution. putenv('CI_ENVIRONMENT'); unset($_ENV['CI_ENVIRONMENT']); service('superglobals')->unsetServer('CI_ENVIRONMENT'); @@ -125,7 +95,6 @@ public function run(array $params) CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green'); CLI::write('The ENVIRONMENT constant will be changed in the next script execution.'); - CLI::newLine(); return EXIT_SUCCESS; } @@ -142,7 +111,6 @@ private function writeNewEnvironmentToEnvFile(string $newEnv): bool if (! is_file($baseEnv)) { CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow'); CLI::write('It is impossible to write the new environment type.', 'yellow'); - CLI::newLine(); return false; } diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index 1923d9629798..7f9c451fba31 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -136,7 +136,7 @@ public function testHelpCommandUsingHelpOption(): void $this->initializeConsole('env', '--help'); (new Console())->run(); - $this->assertStringContainsString('env []', $this->getStreamFilterBuffer()); + $this->assertStringContainsString('env [options] [--] []', $this->getStreamFilterBuffer()); $this->assertStringContainsString( 'Retrieves the current environment, or set a new one.', $this->getStreamFilterBuffer(), diff --git a/tests/system/Commands/Utilities/EnvironmentCommandTest.php b/tests/system/Commands/Utilities/EnvironmentCommandTest.php index 3513436fa9f9..ca5236f5ff5b 100644 --- a/tests/system/Commands/Utilities/EnvironmentCommandTest.php +++ b/tests/system/Commands/Utilities/EnvironmentCommandTest.php @@ -64,13 +64,6 @@ public function testUsingCommandWithNoArgumentsGivesCurrentEnvironment(): void $this->assertStringContainsString(ENVIRONMENT, $this->getStreamFilterBuffer()); } - public function testUsingCommandWithOptionsOnlyGivesCurrentEnvironment(): void - { - command('env --foo'); - $this->assertStringContainsString('testing', $this->getStreamFilterBuffer()); - $this->assertStringContainsString(ENVIRONMENT, $this->getStreamFilterBuffer()); - } - public function testProvidingTestingAsEnvGivesErrorMessage(): void { command('env testing');